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;
    }
    

      

  • 相关阅读:
    【原】从/dev/null重新打开标准输出
    Go 接口转换的一个例子
    关于软件编译安装的出错处理
    【原】GO 语言常见错误
    HP平台由于变量声明冲突导致程序退出时的core
    动态链接库加载出错:cannot restore segment prot after reloc: Permission denied
    Windows VC++常见问题汇总
    .net:System.Web.Mail vs System.Net.Mail应该用哪个
    网络管理的功能
    Hello World! — 用 Groovy 编写的 Java 程序
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/8761310.html
Copyright © 2011-2022 走看看