数据结构作业—表达式求值

嘛...就是转换成反波兰式嘛......

#include <iostream>
#include <stack>
#include <cmath>
#include <string>
#include <sstream>
#include <algorithm>

using namespace std;

string changeToReversePolish(string& expression);
double calculateByReversePolish(string reversePolish);
// return true if stackTop is higher or equal than readIn
bool isHigh(char stackTop, char readIn);
bool isOperator(char readIn);

int main()
{
	string expression = "";
	int num = 0;
	cin >> num;
	
	int count = 0;

	while (count < num)
	{
		cin >> expression;
		cout << calculateByReversePolish(changeToReversePolish(expression)) << endl;

		count++;
	}

	return 0;
}

bool isHigh(char stackTop, char readIn)
{
	if ((stackTop == '+') && (readIn == '+'))
		return true;
	if ((stackTop == '+') && (readIn == '-'))
		return true;
	if ((stackTop == '-') && (readIn == '+'))
		return true;
	if ((stackTop == '-') && (readIn == '-'))
		return true;
	if ((stackTop == '*') && (readIn == '*'))
		return true;
	if ((stackTop == '*') && (readIn == '/'))
		return true;
	if ((stackTop == '/') && (readIn == '*'))
		return true;
	if ((stackTop == '/') && (readIn == '/'))
		return true;
	if (readIn == ')')
		return true;

	return false;
}

bool isOperator(char readIn)
{
	if (readIn == '+' || readIn == '-' || readIn == '*' || readIn == '/')
		return true;
	return false;
}

string changeToReversePolish(string& expression)
{
	stack<char> operandStack;
	string result;
	char operand = NULL;
	istringstream iss(expression);

	while (iss >> operand)
	{
		if (isdigit(operand) || operand == '.')
		{
			result.push_back(operand);
		}
		if (operand == '(')
		{
			operandStack.push(operand);
		}
		else
		{
			if (isOperator(operand))
			{
				result.push_back(' ');
				if (operandStack.empty())
				{
					operandStack.push(operand);
				}
				else if (operandStack.top() != '(' && isHigh(operandStack.top(), operand))
				{
					result.push_back(operandStack.top());
					operandStack.pop();
				}
				else
				{
					operandStack.push(operand);
				}
			}
			else if (operand == ')')
			{
				while (!operandStack.empty() && operandStack.top() != '(')
				{
					result.push_back(operandStack.top());
					operandStack.pop();
				}
				if (operandStack.top() == '(')
					operandStack.pop();
			}
			
		}
	}
	
	while (!operandStack.empty())
	{
		result.push_back(operandStack.top());
		operandStack.pop();
	}

	return result;
}

double calculateByReversePolish(string reversePolish)
{
	stack<double> digitStack;
	string tempDigit = ""; 

	for (int i = 0; i < reversePolish.length(); i++)
	{
		if (isdigit(reversePolish[i]))
		{
			tempDigit.push_back(reversePolish[i++]);
			while ((isdigit(reversePolish[i]) || reversePolish[i] == '.') && i < reversePolish.length())
			{
				tempDigit.push_back(reversePolish[i]);
				i++;
			}
			i--;
			double digit = strtod(tempDigit.c_str(), NULL);
			digitStack.push(digit);
			tempDigit.clear();
		}
		else if(reversePolish[i] == '+' && !digitStack.empty())
		{
			double rightOperand = digitStack.top();
			digitStack.pop();
			
			double leftOperand = digitStack.top();
			digitStack.pop();

			double result = leftOperand + rightOperand;
			digitStack.push(result);
		}
		else if(reversePolish[i] == '-' && !digitStack.empty())
		{
			double rightOperand = digitStack.top();
			digitStack.pop();
			
			double leftOperand = digitStack.top();
			digitStack.pop();

			double result = leftOperand - rightOperand;
			digitStack.push(result);
		}
		else if(reversePolish[i] == '*' && !digitStack.empty())
		{
			double rightOperand = digitStack.top();
			digitStack.pop();
			
			double leftOperand = digitStack.top();
			digitStack.pop();

			double result = leftOperand * rightOperand;
			digitStack.push(result);
		}
		else if(reversePolish[i] == '/' && !digitStack.empty())
		{
			double rightOperand = digitStack.top();
			digitStack.pop();
			
			double leftOperand = digitStack.top();
			digitStack.pop();

			double result = leftOperand / rightOperand;
			digitStack.push(result);
		}

	}

	return digitStack.top();

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值