7-21 求前缀表达式的值 (25 分)

7-21 求前缀表达式的值 (25 分),第1张

7-21 求前缀表达式的值 (25 分)

自己写的程序的收获:

#define _CRT_SECURE_NO_WARNINGS
#include 
#include
#include
#include

using namespace std;
bool judgeOperator(char c);
string Operate(char c, string s1, string s2);

int main()
{
    stacks;
    string str;
    int num = 0;
    while (cin.peek() != -1)
    {
        if (cin.peek() != ' ')
        {
            str += cin.get();
        }
        else
        {
            cin.get();
            s.push(str);
            str = "";
            while (s.size() >= 3)
            {
                string s1 = s.top();
                s.pop();
                string s2 = s.top();
                s.pop();
                string s3 = s.top();
                s.pop();

                if (judgeOperator(s3[0]) && (!judgeOperator(s2[0]) || s2.length() >= 2) && (!judgeOperator(s1[0]) || s1.length() >= 2))
                {
                    string result = Operate(s3[0], s1, s2);
                    if (result == "")
                    {
                        printf("ERROR");
                        return 0;
                    }
                    s.push(result);
                }
                else
                {
                    s.push(s3);
                    s.push(s2);
                    s.push(s1);
                    break;
                }
            }
        }
    }
    s.push(str);
    while (s.size() >= 3)
    {
        string s1 = s.top();
        s.pop();
        string s2 = s.top();
        s.pop();
        string s3 = s.top();
        s.pop();
        if (judgeOperator(s3[0]) && (!judgeOperator(s2[0]) || s2.length() >= 2) && (!judgeOperator(s1[0]) || s1.length() >= 2))
        {
            string result = Operate(s3[0], s1, s2);
            if (result == "")
            {
                printf("ERROR");
                return 0;
            }
            s.push(result);
        }
        else
        {
            s.push(s3);
            s.push(s2);
            s.push(s1);
            break;
        }
    }
    if (s.size() == 1)
    {
        printf("%.1f", stof(s.top()));
    }
    else {
        printf("ERROR");
    }
}
bool judgeOperator(char c)
{
    if (c == '+' || c == '-' || c == '*' || c == '/')
    {
        return true;
    }
    else
    {
        return false;
    }
}
string Operate(char c, string s1, string s2)
{
    string result;
    char buf[30];
    if (c == '+')
    {
        sprintf(buf, "%f", stof(s2) + stof(s1));
        result = buf;
    }
    else if (c == '-')
    {
        sprintf(buf, "%f", stof(s2) - stof(s1));
        result = buf;


    }
    else if (c == '*')
    {
        sprintf(buf, "%f", stof(s2) * stof(s1));
        result = buf;


    }
    else
    {
        if (stof(s1))
        {
            sprintf(buf, "%f", stof(s2) / stof(s1));
            result = buf;
        }
        else
        {
            result = "";
        }


    }

    return result;
}

stof 把string转化为float
把数字转化为字符串:sprintf(buf,"%f",num);
除法除以0判断一下
cin.peek()看看输入输出是啥,到结尾返回EOF
cin.get()取出字符
看别人的收获:

#include
#include 
#include
#include
#include
using namespace std;
float operate(float num1, float num2, char c);
int main()
{
	string expression;
	getline(cin, expression);
	stacks;
	for (int i = expression.length() - 1; i >= 0; i--)
	{
		if (expression[i] == '+' || expression[i] == '-'||expression[i] =='*'||expression[i] =='/')
		{
			if (s.size() >= 2)
			{
				float num1 = s.top();
				s.pop();
				float num2 = s.top();
				s.pop();
				if (expression[i] == '/' && num2 == 0)
				{
					printf("ERROR");
					return 0;
				}
				else
				{
					s.push(operate(num2, num1, expression[i]));
				}
			}
			else
			{
				printf("ERROR");
				return 0;
			}
		}
		else
		{
			if (expression[i] != ' ')//为数字
			{
				string str;
				while (expression[i] != ' '&&i>=0)
				{
					str = expression[i] + str;
					i--;
				}
				s.push(stof(str));
			}
		}
	}

		printf("%.1f", s.top());
	

}
float operate(float num1,float num2,char c)
{
	if (c == '+')
	{
		return num2 + num1;
	}
	else if (c == '-')
	{
		return num2 - num1;
	}
	else if (c == '*')
	{
		return num2 * num1;
	}
	else
	{
		return num2 / num1;
	}
}

getline()读取一行,我浪费时间的一个重要原因是错以为里面会有表达式错误
,对前缀表达式如何计算搞不清楚

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5715242.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存