C++表达式求解


#include"stack"
#include<string>
using namespace std;

stack<char> Symbol;
stack<double>number;
double Calcul(const string &s);//传表达式进这个函数可以返回运算的值,这里运算只包括加减乘除和括号
void divit()
{
	if (!Symbol.empty())
	{
		if (Symbol.top() == '/')
		{
			Symbol.pop();
			double x = number.top();
			number.pop();
			divit();
			number.push(1.0 / x);
			Symbol.push('*');
		}
	}
}
double Calcul(const string &s)
{
	string str=s;
	
	int flag = 0;
	for (int i = 0; i < (str.length()); i++)
	{
		if (str[i] == (wchar_t)'(')
			flag++;
		if (str[i] == (wchar_t)')')
			flag--;
	}
	if (flag != 0)
	{
		double result = 0.0;
		return result;
	}
	else
	{//100+20*10-55^2
		int strat = 0, end = 0;
		bool ifstart = false;
		for (int i = 0; i < (str.length()); i++)
		{
			double now = 0.0;//记录数字
			double r = 1.0;//基

			if (str[i] == '(' || str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/')
			{
				if (Symbol.empty() == true)
				{
					Symbol.push(str[i]);
				}
				else
				{
					if (str[i] == '+' || str[i] == '-')
					{
						while (!Symbol.empty())
						{
							if ((Symbol.top() == '*' || Symbol.top() == '/'))
							{
								char sym = Symbol.top();
								Symbol.pop();
								double a = 0.0, b = 0.0;
								b = number.top();
								number.pop();
								if (!Symbol.empty())
								{
									if (Symbol.top() == '/')
										divit();
								}
								a = number.top();
								number.pop();
								if (sym == '*')
									a = a*b;
								if (sym == '/')
									a = a / b;
								number.push(a);
							}
							else
								break;
						}
					}
					Symbol.push(str[i]);
				}
			}
			else if (str[i] == ')')
			{
				while (Symbol.top() != '(')
				{
					char sym = Symbol.top();
					Symbol.pop();
					double a, b;
					a = number.top();
					number.pop();
					b = number.top();
					number.pop();
					if (sym == '-')
						number.push(b - a);
					if (sym == '+')
						number.push(b + a);
					if (sym == '*')
						number.push(b*a);
					if (sym == '/')
						number.push(b / a);
				}
				if (Symbol.top() == '(')
					Symbol.pop();
			}
			else if (str[i] >= '0'&&str[i] <= '9' || str[i] == '.')
			{
				if (ifstart == false)
				{
					ifstart = true;
					strat = i;
				}
				if (i == str.length() || ((str[i + 1]<'0' || str[i + 1]>'9') && str[i + 1] != '.'))
				{
					end = i;
					ifstart = false;

					for (int j = end; j >= strat; j--)
					{

						if (str[j] == '.')
						{
							now = now / (r);
							r = 1.0;
						}
						else
						{
							now += r*(str[j] - '0');
							r *= 10.0;
						}
					}
					number.push(now);
				}
			}

		}
	}
	double result = 0.0;
	while (!Symbol.empty())
	{
		double a, b;
		char sym = Symbol.top();
		Symbol.pop();
		b = number.top();
		number.pop();
		a = number.top();
		number.pop();
		if (sym == '+')
		{
			number.push(a + b);
		}
		if (sym == '-')
		{
			number.push(a - b);
		}
		if (sym == '*')
		{
			number.push(a * b);
		}
		if (sym == '/')
		{
			number.push(a / b);
		}
	}
	result = number.top();
	return result;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值