2 solutions

  • 0
    @ 2022-2-10 14:26:18

    这种运算题的基本原理和中缀表达式转换后缀表达式是一样的,但是各种细节很需要注意一下。(什么叫代码10分钟,bug一小时啊)

    思路: 开两个栈,一个储存数字,一个储存计算符,遍历表达式,如果是数字就直接入数字栈,如果是计算符,就判断符号栈中已有符号的优先级,将所有优先级比其高或者一样的符号出栈,每出栈一个符号,就拿数字栈的2个栈顶元素来进行计算,然后答案再入栈,直到空栈或者符号栈顶元素优先级比其低,再将这个计算符入栈。遍历完表达式后,如果符号栈还没空,就挨个出栈,再利用数字栈计算:取两个栈顶元素出来,再塞个答案回去。直到栈空,数字栈的唯一一个元素就是答案了。

    #include <bits/stdc++.h>
    #include <stack>
    using namespace std;
    
    
    int main() {	
    	int t;
    	cin >> t;
    	getchar();
    	while (t--) {
    		stack<double> S1;//数字栈
    		stack<char> S2;//符号栈
    		string s;
    		double u = 0;
    		getline(cin, s);
    		int len = s.length();
    		for (int i = 0; i < len; i++) {
    			if (s[i] - '0' >= 0 && s[i] - '0' <= 9) { // 注意读取字符串数字的方式
    				u = 0;
    				while (s[i] - '0' >= 0 && s[i] - '0' <= 9) {
    					u = u * 10 + (s[i] - '0');
    					i++;
    				}
    				S1.push(u);
    			}		
    			else if (s[i] == '+' || s[i] == '-') {
    				while (!S2.empty()) {
    					double x, y;
    					y = S1.top();
    					S1.pop();
    					x = S1.top();
    					S1.pop();
    					if (S2.top() == '+') S1.push(x + y);
    					else if (S2.top() == '-') S1.push(x - y);
    					else if (S2.top() == '*') S1.push(x * y);
    					else if (S2.top() == '/') S1.push(x / y);
    					S2.pop();
    				}
    				S2.push(s[i]);
    			}
    			else if (s[i] == '*' || s[i] == '/') {
    				while (!S2.empty()) {
    					double x, y;
    					if (S2.top() == '+' || S2.top() == '-') break; //break一定要在数字栈元素出栈之前
    					y = S1.top();
    					S1.pop();
    					x = S1.top();
    					S1.pop();
    					if (S2.top() == '*') S1.push(x * y);
    					else if (S2.top() == '/') S1.push(x / y);
    					S2.pop();
    				}
    				S2.push(s[i]);
    			}
    	 }
    		while (!S2.empty()) {
    					double x, y;
    					y = S1.top();
    					S1.pop();
    					x = S1.top();
    					S1.pop();
    					if (S2.top() == '+') S1.push(x + y);
    					else if (S2.top() == '-') S1.push(x - y);
    					else if (S2.top() == '*') S1.push(x * y);
    					else if (S2.top() == '/') S1.push(x / y);
    					S2.pop();	
    	    }
    	printf("%.2lf\n",S1.top());
        }
        return 0;
    }
    
    • 0
      @ 2022-1-6 15:52:34

      参考刘龙浩学长代码,注意本题中有除法和减法,因此要注意运算顺序

      #include <bits/stdc++.h>
      using namespace std;
      double a,b;
      map<char,int>ma;
      stack<double>num;
      stack<char>op;
      int main(){
      	int n;
      	cin>>n;
      	getchar();
      	while(n--){
      	ma['+']=ma['-']=1;
      	ma['*']=ma['/']=2;
      	string s;
      	getline(cin,s);
      	int len=s.size();
      	for(int i=0;i<len;i++){
      		if(s[i]==' ') continue;
      		if(s[i]>='0'&&s[i]<='9'){
      			int x=0;
      			while(s[i]>='0'&&s[i]<='9'){
      				x=x*10+s[i]-'0';
      				i++;
      			}
      			i--;
      			num.push(x);
      		}
      		else{
      			if(op.empty()) op.push(s[i]);
      			else{
      				while(!op.empty()&&ma[op.top()]>=ma[s[i]]){
      					if(op.top()=='+'){
      						a=num.top();
      						num.pop();
      						b=num.top();
      						num.pop();
      						num.push(a+b);
      					}
      					else if(op.top()=='*'){
      						a=num.top();
      						num.pop();
      						b=num.top();
      						num.pop();
      						num.push(a*b);
      					}
      					else if(op.top()=='-'){
      						a=num.top();
      						num.pop();
      						b=num.top();
      						num.pop();
      						num.push(b-a);//注意顺序
      					}
      					else if(op.top()=='/'){
      						a=num.top();
      						num.pop();
      						b=num.top();
      						num.pop();
      						num.push(b/a);//注意顺序
      					}
      					op.pop();
      				}
      				op.push(s[i]);
      			}
      		}
      	}
      		double ans=0;
      		while(num.size()>=2){
      			a=num.top();
      			num.pop();
      			b=num.top();
      			num.pop();
      			if(op.top()=='+'){
      				num.push(a+b);
      				op.pop();
      			}
      		 	else if(op.top()=='*'){
      				num.push(a*b);
      				op.pop();
      			}
      			else if(op.top()=='-'){//注意顺序
      				num.push(b-a);
      				op.pop();
      			}
      			else if(op.top()=='/'){//注意顺序
      				num.push(b/a);
      				op.pop();
      			}
      		}
      		ans=num.top();
      		printf("%.2f\n",ans);
      		while(!num.empty()) num.pop();
      		while(!op.empty()) op.pop();
      	}
      		return 0;
      }
      
      • 1

      Information

      ID
      1526
      Time
      1000ms
      Memory
      256MiB
      Difficulty
      5
      Tags
      # Submissions
      80
      Accepted
      17
      Uploaded By