Expression//表达式求值

Expression//表达式求值

#include<iostream>
#include<algorithm>
#include<stack>
#include<stdlib.h>
#include<vector>
#include<string>
using namespace std;
string format(string str) //字符串预处理 主要是处理-2+3 1+(-3-3)
{
	for (int i = 0; i < str.length(); i++) 
	{
		if (str[i] == '-') 
		{
			if (i == 0) 
			{
				str.insert(0, 1, '0');
			}
			else if (str[i - 1] == '(') 
			{
				str.insert(i, 1, '0'); 
			}
		}
	}
	return str;
}
int prior(char c) //确定操作符的优先级
{
	if (c == '+' || c == '-')
	{
		return 1;
	}
	else if (c == '*' || c == '/') 
	{
		return 2;
	}
	else 
	{
		return 0;
	}
}
vector<string> rpn(string str) //将中缀转为后缀
{
	vector<string> vs; //记录数字
	stack<char> st; //记录符号栈
	for (int i = 0; i < str.length(); i++) 
	{
		string tmp = "";
		switch (str[i]) 
		{
		case '+':
		case '-':
		case '*':
		case '/':
			if (st.empty() || st.top() == '(') 
			{
				st.push(str[i]);
			}
			else
			{
				while (!st.empty() && prior(st.top()) >= prior(str[i])) 
				{
					tmp += st.top();
					vs.push_back(tmp);
					st.pop();
					tmp = "";
				}
				st.push(str[i]);
			}
			break;
		case '(':
			st.push(str[i]);
			break;
		case ')':
			while (st.top() != '(') 
			{
				tmp += st.top();
				vs.push_back(tmp);
				st.pop();
				tmp = "";
			}
			st.pop();
			break;
		default:
			if ((str[i] >= '0' && str[i] <= '9')) 
			{
				tmp += str[i];
				while (i + 1 < str.size() && str[i + 1] >= '0' && str[i + 1] <= '9' || str[i + 1] == '.')//处理多位数或是小数点
				{
					tmp += str[i + 1];
					++i;
				}
				vs.push_back(tmp);
			}
		}
	}
	while (!st.empty()) 
	{
		string tmp = "";
		tmp += st.top();
		vs.push_back(tmp);
		st.pop();
	}
	return vs;
}
double result(vector<string> vv)
{
	stack<double> st;
	double num, op1, op2;
	for (int i = 0; i < vv.size(); i++) 
	{
		string tmp = vv[i];
		if (tmp[0] >= '0' && tmp[0] <= '9') 
		{
			num = atof(tmp.c_str());//将字符串转换为浮点数
			st.push(num);
		}
		else if (vv[i] == "+")
		{
			op2 = st.top();
			st.pop();
			op1 = st.top();
			st.pop();
			st.push(op1 + op2);
		}
		else if (vv[i] == "-")
		{
			op2 = st.top();
			st.pop();
			op1 = st.top();
			st.pop();
			st.push(op1 - op2);
		}
		else if (vv[i] == "*")
		{
			op2 = st.top();
			st.pop();
			op1 = st.top();
			st.pop();
			st.push(op1 * op2);
		}
		else if (vv[i] == "/")
		{
			op2 = st.top();
			st.pop();
			op1 = st.top();
			st.pop();
			st.push(op1 / op2);
		}
	}
	return st.top();
}
void solve(string str) 
{
	str = format(str);
	vector<string> vv = rpn(str);
	double k = result(vv);
	if ((int)k == k) 
	{
		cout << k << endl;
	}
	else 
	{
		printf("%.4f", k);
		cout << endl;
	}
}
int main() 
{
	string str;
	while (getline(cin, str)) 
	{
		solve(str);
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值