3-7 Find the value of the prefix expression (25 points)

Arithmetic expressions have the form of prefix notation, infix notation and postfix notation. The prefix expression refers to the binary operator before the two operands. For example, the prefix expression of 2+3*(7-4)+8/4 is: + + 2 * 3-7 4/8 4. Please design a program to calculate the result value of the prefix expression.

Input format:
Enter a prefix expression with no more than 30 characters in one line, only containing +, -, *, / and operands, and different objects (operands, operands) are separated by spaces.

Output format:
output the calculation result of the prefix expression, with one decimal place reserved, or the error message ERROR.

Insert picture description here

Insert picture description here
Question idea: Regardless of the prefix or suffix, examine the basic application of the stack.

#include<iostream>
#include<cctype>
#include<stack>
#include<string>
using namespace std;

int main()
{
    
    	
	stack<double> st;		//定义一个栈,用于中缀计算
	string s;
	getline(cin, s);		//string头文件里的函数,把表达式用字符串存起来

	for (int i = s.size() - 1; i >= 0; i--)
	{
    
    			
		if (isdigit(s[i]))			//如果第i+1个元素是一个数
		{
    
    
			double mul = 10, num = s[i] - '0';		    //把第i+1位置这个数值取出来
			for (i--; i >= 0; i--)						//判断第i个位置是什么
			{
    
    
				if (isdigit(s[i]))						//如果是一个数
				{
    
    
					num += mul * (s[i] - '0');			//那么这个数就是十位数
					mul *= 10;							//更新mul,因为下第i-1位置上可能还是一个数,也就是百位
				}
				else if (s[i] == '.')					//如果第i位置上是小数点
				{
    
    
					num /= mul;							//方才第i+1位置上的数属于小数点的位置
					mul = 1;							//同样更新mul
				}
				else if (s[i] == '-')					//如果第i位置上是负号
				{
    
    
					num = -num;							//将该数设置为负数
				}
				else
					break;
			}
			st.push(num);								//将最终的算数压入栈中
		}
		else if (s[i] != ' ')							//遇到运算符
		{
    
    
			double s1, s2, sum;
			s1 = st.top();								//后入栈的元素(入栈时从右向左)
			st.pop();
			s2 = st.top();								//先入栈的元素
			st.pop();	
			switch (s[i])								//进行运算
			{
    
    
			case'+':
				sum = s1 + s2;
				break;
			case'-':
				sum = s1 - s2;
				break;
			case '*':
				sum = s1 * s2;
				break;
			case'/':
			{
    
    
				if (s2 == 0)						//特别考虑除法的分布为0的情况
				{
    
    
					cout << "ERROR";
					return 0;
				}
				sum = s1 / s2;
			}
			break;
			}
			st.push(sum);							//运算好的结构在此压入栈
		}
	}

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


}


Guess you like

Origin blog.csdn.net/xdg15294969271/article/details/113877794