• 中缀式转后缀式求表达式结果


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

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

    比如中缀式:(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;
    }
    

      

  • 相关阅读:
    python拆分pubchem SDF文件
    zlib压缩爬虫采集到的网页源码保存到mongodb减少存储空间
    openresty (lua-nginx_static_merger)合并css js文件 减少请求次数,提升页面速度
    scrapy采集—爬取中文乱码,gb2312转为utf-8
    不写代码的爬虫
    爬虫如何发现更多的url呢,怎么动态收集新的url连接
    python 将GIF拆分成图片方法
    Linux shell循环遍历
    iOS 11 导航栏放置UISearchBar 导航栏高度变高解决办法 (iPhone X出现的情况)
    隐藏UISearchBar中的删除按钮
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/8761310.html
Copyright © 2020-2023  润新知