逆波兰表达式C++实现

/*
This programe was designed to calculate a fomulation;
Editor:Xiangyu LV
Edit Time:2018年3月13日05:11:44
E-mail::lvxiangyu11@gamail.com
ALL RIGHT RESERVED
*/
#define text

#include<iostream>
#include<string>
#include<vector>
#include<stack>


using namespace std;

stack<char> CharStory;//标识符暂存栈
stack<long> CalculateStory;//标识符弹栈暂存栈
vector<string> PostFix;//存储后缀表达式;

void init()
{
	char tempc = cin.peek();
	bool IsNumCarry = false;//判断整数是否录入完
	while (cin.peek() != '\n')//如果没有读完
	{
		cin.get(tempc);
		switch (tempc)
		{
		case '+':case  '-'://以上字符读入时立即入栈
		{
					 if (CharStory.size() > 0&&CharStory.top()!='('){
						 PostFix.push_back("");
						 PostFix[PostFix.size() - 1] +=CharStory.top();
						 CharStory.pop();
					 }
					 IsNumCarry = false;
					 CharStory.push(tempc);
		}
			break;
		case '*':case  '/'://以上字符读入时入栈
		{
					 if (CharStory.size()<1 || CharStory.top() == '+' || CharStory.top() == '-'&&CharStory.top() != '(')//如果当前优先级大于前一个优先级入栈
					 {
						 IsNumCarry = false;
						 CharStory.push(tempc);
					 }
					 else{
						 if (CharStory.size() > 0){
							 PostFix.push_back("");
							 PostFix[PostFix.size() - 1] += CharStory.top();
							 CharStory.pop();
							 IsNumCarry = false;
							 CharStory.push(tempc);
						 }
					 }
		}
			break;
		case '^'://以上字符读入时入栈
		{
					 if (CharStory.size()<1 || CharStory.top() == '+' || CharStory.top() == '-'&&CharStory.top() != '(' || CharStory.top() == '*' || CharStory.top() == '/')//如果当前优先级大于前一个优先级入栈
					 {
						 IsNumCarry = false;
						 CharStory.push(tempc);
					 }
					 else{
						 if (CharStory.size() > 0){
							 PostFix.push_back("");
							 PostFix[PostFix.size() - 1] += CharStory.top();
							 CharStory.pop();
							 IsNumCarry = false;
							 CharStory.push(tempc);
						 }
					 }
		}
			break;


		case '1':case '2':case '3':case'4':case '5':case '6':case'7':case'8':case'9':case'0'://读入数字立即写出
		{

					 if (IsNumCarry)
						 PostFix[PostFix.size() - 1] += tempc;
					 else
					 {
						 PostFix.push_back("");
						 PostFix[PostFix.size() - 1] += tempc;
					 }
					 IsNumCarry = true;
		}
			break;
		case '('://(直接压栈
		{
					 IsNumCarry = false;
			CharStory.push(tempc);
		}break;

		case ')':{//读入)全部输出直到(
					 { IsNumCarry = false;
						 while (CharStory.size()>0&&CharStory.top() != '(')
						 {
							 PostFix.push_back("");
							 PostFix[PostFix.size() - 1] += CharStory.top();
							 CharStory.pop();
						 }
						 CharStory.pop();//弹出(
					 }
		}
			break;
		default:
			break;
		}
	}
	while (CharStory.size() > 0)
	{
		PostFix.push_back("");
		PostFix[PostFix.size() - 1] += CharStory.top();
		CharStory.pop();
	}
#ifdef text
	for (int i = 0; i < PostFix.size(); i++)
	{
		cout << PostFix[i];
	}
#endif

}

long ChangToLongInt(string temp)
{
	long back=0;
	for (int i = 0; i < temp.size(); i++)
	{
		back << 1;
		back += temp[i] - '0';
	}
	return back;
}
void count()
{
	long temp;
	for (auto i:PostFix)
	{
		if (i != "+" &&i != "-" &&i != "*" &&i != "/" &&i != "^")
		{
			CalculateStory.push(ChangToLongInt(i));
		}
		else
		{
			if (i == "*")
			{
				temp = CalculateStory.top();
				CalculateStory.pop();
				temp *= CalculateStory.top();
				CalculateStory.pop();
			}
			if (i == "/")
			{
				temp = CalculateStory.top();
				CalculateStory.pop();
				temp /= CalculateStory.top();
				CalculateStory.pop();
			}
			if (i == "+")
			{
				temp = CalculateStory.top();
				CalculateStory.pop();
				temp += CalculateStory.top();
				CalculateStory.pop();
			}
			if (i == "-")
			{
				temp = CalculateStory.top();
				CalculateStory.pop();
				temp -= CalculateStory.top();
				CalculateStory.pop();
			}
			if (i == "^")
			{
				temp = CalculateStory.top();
				CalculateStory.pop();
				temp = pow(temp,CalculateStory.top());
				CalculateStory.pop();
			}
			CalculateStory.push(temp);
		}
	}
	cout << temp;

}

int main()
{
	cout << "Plase enter the fomulation:" << endl;
	init();
	cout << "=";
	count();
#ifdef text
	system("pause");
#endif
	return 0;
}



阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lvxiangyu11/article/details/79959731
个人分类: 算法
上一篇UVa524
下一篇uva11292 Dragon of Loowater (水题)
想对作者说点什么? 我来说一句

逆波兰表达式C++实现

2011年01月11日 3KB 下载

没有更多推荐了,返回首页

关闭
关闭