逆波蘭表達式C++實現

/*
This programe was designed to calculate a fomulation;
Editor:Xiangyu LV
Edit Time:2018年3月13日05:11:44
E-mail::[email protected]
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;
}


点赞