简单计算器,支持加减乘除括号乘方复杂表达式

结构体Node存储操作符或者操作数,依靠flag区分。
使用unordered_map映射操作符到int,方便后续操作。initializeMap来初始化该map。
然后使用getRpm得到后缀表达式。
先使用栈转成后缀表达式,放入队列中。
函数stringToDouble将string类型数组转成double类型。
函数cal完成最后一步计算,利用res栈完成后缀表达式的结果计算

#include
#include
#include
#include
#include
#include
#include
using namespace std;

struct Node
{
	int flag;
	double num;
	int pri;	
};
unordered_map<char, int> m;
stack<int> s;
queue<Node> q;
stack<double> res;
void initializeMap();
double stringToDouble(string str);
void getRpm(string input);
void print();
double cal();
int main()
{
	initializeMap();
	string expression;
	cout << "input an expression:" << endl;
	getline(cin, expression);
	getRpm(expression);
//	print();
	cout << endl<<"result: " << cal() << endl;
	return 0;
}
bool isDigit(string::iterator it)
{
	return m.find(*it) == m.end();
}
void getRpm(string input)
{
	string::iterator it = input.begin();

	while (it != input.end())
	{
		if(isDigit(it))
		{
			string temp;
			while (it!=input.end()&&isDigit(it)) //是操作数
			{
				temp += (*it);
				it++;
			}
			q.push(Node{1,stringToDouble(temp) ,0});
		}
		else
		{
			int operationNum = m[*it];
			int calPriority = abs(operationNum);
			
			if (s.empty())s.push(operationNum);//空栈直接push
			else {
				if (calPriority == 7)      //  ')'
				{
					while (!s.empty()&&s.top() != 6)
					{
						q.push(Node{ 0, 0, s.top() });
						s.pop();
					}
					if(!s.empty())s.pop();
				}
				else    
				{
					if (calPriority > abs(s.top()))s.push(operationNum);
					else if (s.top()==6)
						s.push(operationNum);
						else {
							while (!s.empty()&&calPriority <= abs(s.top())&&s.top()!=6)
							{
								q.push(Node{ 0, 0, s.top() });
								s.pop();
							}
							s.push(operationNum);
						}
				}
			}
			if(it!=input.end())it++;
		}
		
	}
	while (!s.empty())
	{
		q.push(Node{ 0,0,s.top() });
		s.pop();
	}
}
double stringToDouble(string str)
{
	
	string::iterator it = str.begin();
	while (str.length() > 0 && *it == '0')
		it = str.erase(it);
	if (str.length() == 0)return 0;
	double temp = 0;
	while (it!=str.end()&&*it != '.')
	{
		temp = temp * 10 + (*it - '0');
		it++;
	}
	if (it == str.end())return temp;
	it++; 
	int a = -1;
	while (it != str.end())
	{
		temp += (*it - '0')*pow(10, a);
		it++; a--;
	}
	return temp;
}
double cal()
{
	double d = 0;
	while (!q.empty())
	{
		if (q.front().flag == 1)
			res.push(q.front().num);
		else {
			int op = q.front().pri;
			double a, b,resd;
			a = res.top(); res.pop();
			b = res.top(); res.pop();
			switch (op)
			{
				case 1:resd = b + a; break;
				case 2:resd = b * a; break;
				case 3:resd = pow(b, a); break;
				case -1:resd = b - a; break;
				case -2:resd = b / a; break;
			}
			res.push(resd);
		}
		q.pop();
	}
	if (!res.empty())d = res.top();
	return d;
}
void print()
{
	cout << q.size() << endl;
	while (!q.empty())
	{
		if (q.front().flag)
			cout << q.front().num;
		else cout << q.front().pri;
		cout << " ";
		q.pop();
	}
}
void initializeMap()
{
	m.insert({ pair<char,int>('+',1), pair<char, int>('-', -1),
		pair<char, int>('*',2), pair<char, int>('/', -2),
		pair<char, int>('^',3), pair<char, int>('(',6) ,
		pair<char, int>(')',7) });
};

你可能感兴趣的:(我的初尝试)