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

自己写的程序的收获:

#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include<stack>
#include<iostream>
#include<string>

using namespace std;
bool judgeOperator(char c);
string Operate(char c, string s1, string s2);
/*疑惑问题:怎么区分+-*和数字呢*/
int main()
{
    stack<string>s;
    string str;
    int num = 0;
    while (cin.peek() != -1)/*输入没结束*/
    {
        if (cin.peek() != ' ')
        {
            str += cin.get();
        }
        else
        {
            cin.get();
            s.push(str);
            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<stdio.h>
#include<stack> 
#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
float operate(float num1, float num2, char c);
int main()
{
	string expression;
	getline(cin, expression);
	stack<float>s;
	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()读取一行,我浪费时间的一个重要原因是错以为里面会有表达式错误
,对前缀表达式如何计算搞不清楚

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值