1356:计算(calc)

1356:计算(calc)
时间限制: 1000 ms         内存限制: 65536 KB
【题目描述】
小明在你的帮助下,破密了Ferrari设的密码门,正要往前走,突然又出现了一个密码门,门上有一个算式,其中只有“(”,“)”,“0-9”,“+”,“-”,“*”,“/”,“^”,求出的值就是密码。小明数学学得不好,还需你帮他的忙。(“/”用整数除法)
【输入】
共1行,为一个算式。
【输出】
共1行,就是密码。
【输入样例】
1+(3+2)*(7^2+6*9)/(2)
【输出样例】
258

//注意测试点有 +-*/^()0~9 以外的字符 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <stack>
#include <cmath>
#include <cstdlib>
#include <vector>
using namespace std;
int get_fast(string op)//运算符的优先级 
{
	int ans=-1;
	switch(op[0]){
		case '+':case '-': ans=1;break;
		case '*':case '/': ans=2;break;
		case '^': ans=3;break;	
	}
	return ans;
}
vector<string> scaner(string s)//将字符串解析到vector 
{
	vector<string> ans;
	string ops="+-*/^()";
	string t="";
	for(int i=0;i<s.size();i++){
		if(s[i]>='0'&&s[i]<='9') t+=s[i];
		else if(ops.find(s[i])!=string::npos) {//注意有特殊字符 
			if(t.size()>0)ans.push_back(t);
			t=s[i];
			ans.push_back(t);
			t="";
		}
	}
	if(t.size())ans.push_back(t);//将最后一个数字放入vector 
	//for(int i=0;i<ans.size();i++)cout<<ans[i];
	//cout<<endl; 
	return ans;
}
vector<string> infix_suffix(vector<string> exp)//中缀表达式转后缀表达式 
{
	vector<string> ans;
	stack<string> st;
	string ops="+-*/^";
	for(int i=0;i<exp.size();i++){
		if(exp[i]=="(")//1、"("直接进栈 
			st.push(exp[i]);
		else if(exp[i]==")"){//2、")"在找到"("之前连续出栈运算符  
			while(st.top()!="("){
				ans.push_back(st.top());
				st.pop();
			}
			st.pop();//弹出"("	
		}
		else if(ops.find(exp[i])!=string::npos){//3、确定是运算符 
			while(st.size()&&get_fast(exp[i])<=get_fast(st.top())){
				ans.push_back(st.top());
				st.pop();//如果栈顶的运算符优先级大于等于当前运算符 栈顶的运算符被连续弹出 
			}
			st.push(exp[i]);//当前运算符进栈 
		}
		else ans.push_back(exp[i]);//4、数字直接进栈 
		
	}
	while(st.size()) {ans.push_back(st.top());st.pop();}//5、栈内剩余运算符被连续弹出 
	//for(int i=0;i<ans.size();i++)cout<<ans[i]<<endl;
	return ans;
}
int suffix_calc(vector<string> exp)//后缀表达式计算结果 
{
	stack<int> num;
	string ops="+-*/^";
	for(int i=0;i<exp.size();i++){
		if(ops.find(exp[i])!=string::npos){//是运算符,弹出两个数字运算并入栈 
			int a=num.top();num.pop();
			int b=num.top();num.pop();
			if(exp[i]=="+") num.push(b+a);
			else if(exp[i]=="-") num.push(b-a);
			else if(exp[i]=="*") num.push(b*a);
			else if(exp[i]=="/") num.push(b/a);
			else if(exp[i]=="^") num.push(pow(b*1.0,a));
		}
		else {//字符串是数字,转换成整数并入栈 
			num.push(atoi(exp[i].c_str()));
		}
	}
	//cout<<num.top()<<endl;
	return num.top();
}
int main(int argc, char *argv[])
{
	string st;
	getline(cin,st);//读取字符串 
	vector<string> vs=scaner(st);//将字符串解析到vector 
	vs=infix_suffix(vs);//中缀表达式转后缀表达式 
	cout<<suffix_calc(vs)<<endl;//输出后缀表达式计算结果 
	return 0;
}
#include <iostream>
#include <cmath>
#include <cctype>
#include <string>
#include <vector>
#include <stack>
#include <cstdio>
#include <cstdlib>
using namespace std;
int get_level(char op)
{
	int ans=-1;
	switch(op){
		case '+':case '-': ans=1;break; 
		case '*':case '/': ans=2;break;
		case '^': ans=3;break;
	}
	return ans;
}
int calc_value(int a,int b,char op)
{
	int ans;
	switch(op){
		case '+': ans=a+b;break;
		case '-': ans=a-b;break;
		case '*': ans=a*b;break;
		case '/': ans=a/b;break;
		case '^': ans=(int)pow(1.0*a,b);break;
	}
	return ans;
}
vector<string> scanner(string exp,char end_ch='\0')
{
	unsigned int i=0;
	vector<string> ans;
	string num;
	string ops="+-*/^()";
	while(exp[i]!=end_ch && i<exp.size()){
		if(num.size()==0 && exp[i]=='-' && (i==0 || exp[i-1]!=')')) num+="-";
		else if(isdigit(exp[i])) num+=exp[i];
		else if(ops.find(exp[i])!=string::npos){
			if(num.size()){
				ans.push_back(num);num="";
			}
			ans.push_back(string(1,exp[i]));
		}
		i++;
	}
	if(num.size()) ans.push_back(num);
	return ans;
}
vector<string> infix_to_suffix(const vector<string> &exp)
{
	stack<string> stk;
	vector<string> ans;
	string ops="+-*/^()";
	for(unsigned int i=0;i<exp.size();i++){
		if(exp[i]=="(") stk.push(exp[i]);
		else if(exp[i]==")"){
			while(stk.size()&&stk.top()!="("){
				ans.push_back(stk.top());stk.pop();
			}
			stk.pop();
		}
		else{
			if(exp[i].size()==1 && ops.find(exp[i])!=string::npos){
				while(stk.size() && get_level(stk.top()[0])>=get_level(exp[i][0])){
					ans.push_back(stk.top());stk.pop();
				}
				stk.push(exp[i]);
			}
			else ans.push_back(exp[i]);
		}
	} 
	while(stk.size()){
		ans.push_back(stk.top());stk.pop(); 
	}
	return ans;
}
int calc_suffix(const vector<string> &exp)
{
	int ans,a,b;
	stack<int> stk;
	string ops="+-*/^()";
	for(unsigned int i=0;i<exp.size();i++){
		if(exp[i].size()==1 && ops.find(exp[i])!=string::npos){
			b=stk.top();stk.pop();
			a=stk.top();stk.pop();
			ans=calc_value(a,b,exp[i][0]);
			stk.push(ans);
		}
		else stk.push(atoi(exp[i].c_str()));
	}
	return stk.top();
}
int main(int argc, char *argv[])
{
	vector<string> vs,ans;
	string exp;
	int ret;
	cin>>exp;
	vs=scanner(exp);
	ans=infix_to_suffix(vs);
	ret=calc_suffix(ans);
	cout<<ret<<endl;	
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宏阳李老师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值