C++中缀表达式转后缀表达式并计算

关于后缀表达式的计算,参考了此链接,https://blog.csdn.net/creativele/article/details/81710049

栈的应用——四则运算表达式求值。

后缀表达式的优点是所有的符号都是在要运算数字的后面出现,例如简单表达式“1+2”,后缀表达式即为12+。
再如表达式“(3-1)x2”,其后缀表达式为31-2*。
再如表达式“9-3x2”,其后缀表达式为932*-。

中缀表达式转后缀表达式

规则:从左到右遍历中缀表达式的每个数字和符号,若是数字就输出,即成为后缀表达式的一部分(在此为压入操作数vector);若是符号,则判断其余栈顶符号的优先级,是右括号或优先级低于栈顶符号(乘除优先加减)则栈顶元素依次出栈并输出(此为压入符号栈),并将当前符号进栈,一直到结束。

括号无视一切运算符,即一对括号内的所有运算符,在遇到右括号时,均需出栈。

后缀表达式

规则:从左到右遍历表达式的每个数字和符号,遇到是数字就进栈,遇到是符号,就将处于栈顶两个数字出栈,进行运算,然后别忘记,运算结果进栈

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

using namespace std;

class InfixSuffixes
{
public:
	void convertToPostfix(string exp);
private:
	stack<char>syStack;//运算符栈
	stack<int>fixStack;//
	vector<string>postfix;//存储后缀表达式
	bool isOperator(char c);
	int getPriority(char c);
	int calculate(int i1, int i2, char c);
	int result;
};

//将其他类型的变量转换成为string类型的变量
template<class T>
string otherToString(const T& other) {
	stringstream stream;
	stream << other;
	string str = stream.str();
	return str;
}

template<class T>
T stringToOther(const string& str) {
	istringstream istream(str);
	T other;
	istream >> other;
	return other;
}

//非运算符直接压入vector
//如果栈为空,则第一个运算符入栈,
//如果当前字符为)则无视运算符优先级,一直pop直到遇到左括号。
void InfixSuffixes::convertToPostfix(string exp)//"8+(4-2)*4+6/3"
{
	for (auto c : exp) {
		if (isOperator(c)) 
			postfix.push_back(otherToString<char>(c));
		//代表运算符
		else if (syStack.empty()||c=='(') 
			syStack.push(c);
		else {
			while (!syStack.empty()) {
				if (c == ')') {
					if (syStack.top() == '(') {
						syStack.pop();
						break;
					}
					else {
						postfix.push_back(otherToString<char>(syStack.top()));
						syStack.pop();
					}
				}
				else if ((getPriority(c) < getPriority(syStack.top()))&&syStack.top()!='(') {
					while (!syStack.empty() ) {
						postfix.push_back(otherToString<char>(syStack.top()));
						syStack.pop();
					}
				}
				else
					break;
			}
			if (c != ')' ) 
				syStack.push(c);		
		}
	}
	while (!syStack.empty()) {
		postfix.push_back(otherToString<char>(syStack.top()));
		syStack.pop();
	}
	vector<string>::iterator it;
	for (it = postfix.begin(); it != postfix.end(); it++) {
		cout << *it;//输出后缀表达式
		//计算后缀表达式
		if (isOperator(stringToOther<char>(*it)))
			fixStack.push(stringToOther<int>(*it));
		else if (fixStack.size() >= 2) {
			int i = fixStack.top();
			fixStack.pop();
			int j = fixStack.top();
			fixStack.pop();
			result = calculate(j, i, stringToOther<char>(*it));
			fixStack.push(result);
		}
	}		
	cout << endl;
	cout << "表达式结果:" << result << endl;	
}


//操作数为真
bool InfixSuffixes::isOperator(char c)
{
	if (c != '+' && c != '-' && c != '*' && c != '/' && c != '('&&c != ')')
		return true;
	else 
		return false;
}

int InfixSuffixes::getPriority(char c)
{
	if (c == '(')
		return 1;
	else if (c == '+' || c == '-')
		return 2;
	else if (c == '*' || c == '/')
		return 3;
	else if (c == ')')
		return 4;
	else
	return -1;
}

int InfixSuffixes::calculate(int i1, int i2, char c)
{
	int result;
	switch (c)
	{
	case '+':
		result = i1 + i2;
		break;
	case'-':
		result = i1 - i2;
		break;
	case'*':
		result = i1*i2;
		break;
	case'/':
		result = i1 / i2;
		break;
	default:result= 0;
		break;
	}
	return result;
}




int main() {
	string expr = "8+(4-2)*4+6/3";
	InfixSuffixes ifs;
	ifs.convertToPostfix(expr);
	system("pause");
	return 0;
}
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
中缀后缀表达式的步骤如下: 1. 创建一个空的栈和一个空的输出队列。 2. 从左到右遍历中缀表达式的每个元素。 3. 如果遇到操作数(数字),则直接将其添加到输出队列中。 4. 如果遇到左括号,则将其压入栈中。 5. 如果遇到右括号,则将栈中的元素弹出,直到遇到左括号为止,并将弹出的操作符添加到输出队列中。注意,左括号不会添加到输出队列中。 6. 如果遇到操作符,则检查栈顶的操作符。如果栈顶的操作符优先级大于或等于当前操作符的优先级,则将栈顶的操作符弹出,并将其添加到输出队列中。重复此步骤直到栈顶的操作符优先级小于当前操作符的优先级,或者栈为空。然后将当前操作符压入栈中。 7. 遍历完中缀表达式后,如果栈不为空,则将栈中的操作符依次弹出,并添加到输出队列中。 8. 输出队列中的元素就是后缀表达式。 接下来,对后缀表达式进行求值的步骤如下: 1. 创建一个空的栈。 2. 从左到右遍历后缀表达式的每个元素。 3. 如果遇到操作数(数字),则将其压入栈中。 4. 如果遇到操作符,则从栈中弹出两个操作数,并根据操作符进行计算,然后将计算结果压入栈中。 5. 遍历完后缀表达式后,栈中的最后一个元素就是表达式的求值结果。 需要注意的是,对于复杂的表达式,必须按照运算符的优先级和结合性正确地进行计算
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值