3-8 Find the value of the prefix expression (20 points) (detailed analysis)

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, operators) are separated by spaces.

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

Input sample:

    • 2 * 3 - 7 4 / 8 4

Sample output:

13.0

For the same type of questions, the test points are basically the same, and the link is attached

1-3 Expression conversion (25 points) (My idol is a big brother)

The initial processing ideas are basically the same as the questions on the link, but it is not easy to write when writing, especially when there are negative signs and decimal points. Later, after thinking about it, I can intercept the string directly according to the space. Already, this is a good way. After the interception, traverse, if the current string is a symbol, judge whether the stack size is greater than or equal to 2, if it is, continue, if not, change the value of the identification variable, output ERROR (because the operator is encountered, it means that two If it is not an operator, it means it is a number. Use the stof function directly to convert the string to a floating-point type and put it on the stack (the conversion was written by myself at the beginning. There are many problems that need attention. For example, if the first character is a minus sign, if there is a decimal point, you need to perform two-part arithmetic, which is a bit troublesome, but it can be done, and interested friends can try). If the flag is still true, there is no error and output directly The value at the top of the stack is the result of the operation. When calculating, pay attention to who is in front of the two numbers and who is behind (refers to subtraction and division operations, the other two have no effect)
#include <iostream>
#include <stack>
#include <vector>
#include <string>
using namespace std;

int main(){
    
    
	stack<double> st;
	string s,ss;
	getline(cin,s);
	vector<string> v;
	bool flag = true;
	for(int i = 0;i<s.length();i++){
    
    
		int j = i;
		while(s[j]!=' '&&j<s.length())
			j++;
		ss = s.substr(i,j-i);
		v.push_back(ss);
		i = j; 
	}
	for(int i = v.size()-1;i>=0;i--){
    
    
		ss = v[i];
		if(ss=="+"||ss=="-"||ss=="*"||ss=="/"){
    
    
			if(st.size()>=2){
    
    
				double a = st.top();
				st.pop();
				double b = st.top();
				st.pop();
				if(ss=="/"&&b==0){
    
    
					cout << "ERROR";
					flag = false;
				}
				else{
    
    
					if(ss=="+")
						st.push(a+b);
					else if(ss=="-")
						st.push(a-b);
					else if(ss=="*")
						st.push(a*b);
					else
						st.push(a/b);
				}
			}
			else{
    
    
				cout << "ERROR";
				flag = false;
				break;
			}
		}
		else{
    
    
			st.push(stof(ss));
		}
	}
	if(flag)
		printf("%.1lf",st.top());
	return 0;
} 

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45845039/article/details/109137724