简单计算器算法实现

#pragma once
#include <string>

class Calculator {
public:
	bool Exec(const std::string& strInput, int& result);

private:
	bool IsNumber(char c);
	bool IsOperator(char c);
	int GetPriority(char c);
};

#include "Calculator.h"
#include <stack>
using namespace std;

bool Calculator::IsNumber(char c) {
	return (c >= '0' && c <= '9');
}

bool Calculator::IsOperator(char c) {
	return (c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')');
}

int Calculator::GetPriority(char c) {
	switch (c)
	{
	case '+':
	case '-':
		{
			return 1;
		}
	case '*':
	case '/':
		{
			return 2;
		}
	default:
		return 0;
	}
}

bool Calculator::Exec(const string& strInput, int& result)
{
	int index = 0;
	int len = strInput.length();

	//有效性检查
	while (index < len)
	{
		if (!IsNumber(strInput[index]) && !IsOperator(strInput[index]))
			return false;
		else
			index++;
	}

	//中缀表达式转为后缀表达式
	stack<char> operatorStack;
	string strOutput;
	index = 0;
	len = strInput.length();
	while (index < len)
	{
		if (IsNumber(strInput[index]))
		{
			do
			{
				strOutput += strInput[index++];
			} while (index < len && IsNumber(strInput[index]));
			strOutput += ' ';
		}
		else if (strInput[index] == '(')
		{
			operatorStack.push(strInput[index++]);
		}
		else if (strInput[index] == ')')
		{
			while (!operatorStack.empty())
			{//弹出直到遇到左括号
				char last = operatorStack.top();
				operatorStack.pop();
				if (last == '(')
					break;
				else
					strOutput += last;
			}
			index++;
		}
		else
		{
			if (operatorStack.empty())
			{//栈为空,压入
				operatorStack.push(strInput[index]);
			}
			else
			{
				while (!operatorStack.empty() && GetPriority(strInput[index]) <= GetPriority(operatorStack.top()))
				{//优先级比栈顶元素小,则弹出(符合优先级和从左到右计算规则)
					strOutput += operatorStack.top();
					operatorStack.pop();
				}

				operatorStack.push(strInput[index]);
			}
			index++;
		}
	}

	//弹出剩余的元素
	while (!operatorStack.empty())
	{
		strOutput += operatorStack.top();
		operatorStack.pop();
	}

	//计算结果
	stack<int> resultStack;
	string strTemp;
	index = 0;
	len = strOutput.length();
	while (index < len)
	{
		if (IsNumber(strOutput[index]))
		{
			strTemp.clear();
			do
			{
				strTemp += strOutput[index++];
			} while (index < len && strOutput[index] != ' ');
			resultStack.push(atoi(strTemp.c_str()));
		}
		else 
		{
			int number1 = 0;
			int number2 = 0;
			if (resultStack.size() >= 2)
			{
				number1 = resultStack.top();
				resultStack.pop();
			}
			else if (strOutput[index] == '+' || strOutput[index] == '-')
			{//兼容正负数表示法,-1=0-1
				number1 = 0;
			}
			else
			{
				return false;
			}

			number2 = resultStack.top();
			resultStack.pop();

			if (strOutput[index] == '+')
				resultStack.push(number1 + number2);
			else if (strOutput[index] == '-')
				resultStack.push(number1 - number2);
			else if (strOutput[index] == '*')
				resultStack.push(number1 * number2);
			else if (strOutput[index] == '/')
				resultStack.push(number1 / number2);
		}
		index++;
	}

	if (resultStack.empty()) 
	{
		return false;
	}
	else 
	{
		result = resultStack.top();
		return true;
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值