zoukankan      html  css  js  c++  java
  • 中缀式转后缀式求表达式结果

    中缀式对于人来说很好计算,但对于计算机来说就很麻烦了。

    统计计算机算,考研将中缀式转换为后缀式来计算。

    比如中缀式:(1+2)*3-4

    转换为后缀式:12+3*4-

    后缀式的计算:从左到有遍历,遇见运算符式,将前面的两个值进行计算。

    以(1+2)*3-4为例,它的后缀式是:12+3*4-

    1、第一个运算符是- 前面的两个值分别是1和2,1+2=3,所以式子变成了33*4-

    2、现在第一个运算符是*,3*3=9,变成了94-

    3、现在第一个运算符是-,9-4=5,变成了5

    所以最终的结果是5.

    将中缀式转变成后缀式代码:

    使用一个stack和一个vector。分下面几种情况,运算符优先级按 () < -+ < */ 

    1、遇见 ( 时,直接将其压进栈里。

    2、遇见 ) 时,将栈里的运算符依次弹出来,压进vector里,直到遇见 ( ,将 ( 弹出不压进vector里。

    3、遇见数字时,直接将其压进vector

    4、遇见 - 或 + 时,将栈里优先级大于等于 - + 的弹出压进vector里,然后将这个运算符压入栈里

    5、遇见 * 或 / 时,将栈里优先级大于等于 * / 的弹出压进vector里,然后将这个运算符压入栈里。

    6、最后将栈里的运算符依次弹出压进vector里。

    void suffix() {
    	for(int i = 0; str[i]; i ++) {
    		if(str[i] == '(') {
    			st1.push('(');
    		}else if(str[i] == ')') {
    			while(!st1.empty()&&st1.top()!='(') {
    				vs.push_back(st1.top());
    				st1.pop();
    			}
    			st1.pop();
    		}else if(str[i] == '+' || str[i] == '-') {
    			while(!st1.empty()&&st1.top() != '('){
    				vs.push_back(st1.top());
    				st1.pop();
    			}
    			st1.push(str[i]);
    		}else if(str[i] == '*' || str[i] == '/') {
    			while(!st1.empty()&&(st1.top()=='/' || st1.top()=='*')) {
    				vs.push_back(st1.top());
    				st1.pop();
    			}
    			st1.push(str[i]);
    		}else if(str[i] != ' '){
    			while(str[i] >= '0' && str[i] <= '9') {
    				vs.push_back(str[i]);
    				i++;
    			}
    			i--;
    			vs.push_back('#');
    		}
    	}
    	while(!st1.empty()) {
    		vs.push_back(st1.top());
    		st1.pop();
    	}
    }
    

      

    中缀式转后缀式+计算:

    #include <iostream>
    #include <vector>
    #include <stdio.h>
    #include <cstring>
    #include <stack>
    using namespace std;
    char str[310];
    stack<char> st1;
    vector<char> vs;
    
    void suffix() {
    	for(int i = 0; str[i]; i ++) {
    		if(str[i] == '(') {
    			st1.push('(');
    		}else if(str[i] == ')') {
    			while(!st1.empty()&&st1.top()!='(') {
    				vs.push_back(st1.top());
    				st1.pop();
    			}
    			st1.pop();
    		}else if(str[i] == '+' || str[i] == '-') {
    			while(!st1.empty()&&st1.top() != '('){
    				vs.push_back(st1.top());
    				st1.pop();
    			}
    			st1.push(str[i]);
    		}else if(str[i] == '*' || str[i] == '/') {
    			while(!st1.empty()&&(st1.top()=='/' || st1.top()=='*')) {
    				vs.push_back(st1.top());
    				st1.pop();
    			}
    			st1.push(str[i]);
    		}else if(str[i] != ' '){
    			while(str[i] >= '0' && str[i] <= '9') {
    				vs.push_back(str[i]);
    				i++;
    			}
    			i--;
    			vs.push_back('#');
    		}
    	}
    	while(!st1.empty()) {
    		vs.push_back(st1.top());
    		st1.pop();
    	}
    }
    double cal() {
    	double tmp[110];
    	int top = -1;
    	for(int i = 0; i < vs.size(); i ++) {
    		if(vs[i] == '+') {
    			tmp[top-1] += tmp[top];
    			top--;
    		}else if(vs[i] == '-') {
    			tmp[top-1] -= tmp[top];
    			top--;
    		}else if(vs[i] == '*') {
    			tmp[top-1] *= tmp[top];
    			top--;
    		}else if(vs[i] == '/') {
    			tmp[top-1] /= tmp[top];
    			top--;
    		}else {
    			int s = 0;
    			while(vs[i] >= '0' && vs[i] <= '9') {
    				s = s*10 + vs[i] - '0';
    				i++;
    			}
    			tmp[++top] = 1.0*s;
    		}
    	}
    	return tmp[0];
    }
    
    int main() {
    	gets(str);
    	suffix();
    	for(int i = 0; i < vs.size(); i ++) {
    		if(vs[i]!='#')
    		cout << vs[i];
    	}
    	cout << endl;
    	double ans = cal();
    	printf("%.2lf
    ",ans);
    	return 0;
    }
    

      

  • 相关阅读:
    jenkins+jmeter结合使用
    Bean的前身今世&处理器&Aware
    Spring的profile属性
    XML的验证模式
    org.springframework.beans包
    packge-info.java
    字节码解释执行引擎
    invokedynamic指令
    多态方法调用的解析和分派
    运行时栈帧结构
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/8761310.html
Copyright © 2011-2022 走看看