欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

表达式求值

程序员文章站 2022-03-31 19:46:08
...

有人问为什么一般转后缀表达式求值不转前缀,因为后缀求值的时候可以递推,前缀求值的时候要递归,递推效率显然比递归效率高

#include <iostream>
#include <stack>
using namespace std;

double toNum(char*s, int &k)
{
	int flag = 0;
	double x = 0.0, y = 0.1;
	while (s[k] >= '0'&&s[k] <= '9' || s[k] == '.')
	{
		if (s[k] >= '0'&&s[k] <= '9')
		{
			if (flag == 0)
				x = x * 10 + s[k] - '0';
			else
			{
				x = x + y*(s[k] - '0');
				y = y*0.1;
			}
		}
		else
		{
			flag = 1;
		}
		k = k + 1;
	}
	return x;
}

int priority(char c)
{
	int k;
	switch (c)
	{
	case '*':k = 2; break;
	case '/':k = 2; break;
	case '+':k = 1; break;
	case '-':k = 1; break;
	case '(':k = 0; break;
	case ')':k = 0; break;
	default:k = -1; break;
	}
	return k;
}

int main()
{
	stack<double> sv;
	stack<char> sp;
	char c;
	int k = 0, flag = 1;
	double x, y;
	sp.push('\0');
	char *s = new char[100];
	cout << "Input:" << endl;
	cin >> s;
	c = s[k];
	while (flag)
	{
		if (c >= '0'&&c <= '9' || c == '.')
		{
			sv.push(toNum(s, k));
		}
		else if (c == '\0'&& sp.top() == '\0')
		{
			flag = 0;
		}
		else if (c == '(' || (priority(c) > priority(sp.top())))
		{
			sp.push(c);
			k++;
		}
		else if (c == ')'&& sp.top() == '(')
		{
			sp.pop();
			k++;
		}
		else if (priority(c) <= priority(sp.top()))
		{
			x = sv.top();
			sv.pop();
			y = sv.top();
			sv.pop();
			c = sp.top();
			sp.pop();
			switch (c)
			{
			case '+':y = x + y; break;
			case '-':y = y - x; break;
			case '*':y = x*y; break;
			case '/':y = y / x; break;
			}
			sv.push(y);
		}
		c = s[k];
	}
	cout << sv.top() << endl;
	return 0;
}