0
点赞
收藏
分享

微信扫一扫

中缀表达式转换后缀并求值

代码小姐 2022-01-15 阅读 53

不能输入两位数,以后再改进

#include<iostream>
#include<string>
using namespace std;

template <typename T>
struct Node
{
	T ch;
	Node<T>* next;
};

template <typename T>
class Stack
{
private:
	Node<T>* top;
public:
	Stack(): top(nullptr) {}
	~Stack();
	void Push(T&);
	T Gettop();
	T Pop();
	bool Isempty();
	void PrintStr();
};

template <typename T>
Stack<T>::~Stack()
{
	while (!Isempty())
	{
		Pop();
	}
}

template <typename T>
bool Stack<T>::Isempty()
{
	if (this->top==nullptr)
		return true;
	else
		return false;
}

template <typename T>
void Stack<T>::Push(T& s)
{
	Node<T>* temp;
	if (Isempty())
	{
		temp = new Node<T>;
		temp->ch = s;
		temp->next = nullptr;
		top = temp;
	}
	else
	{
		temp = new Node<T>;
		temp->ch = s;
		temp->next = top;
		top = temp;
	}
}

template <typename T>
T Stack<T>::Pop()
{
	if (Isempty())
	{
		cout << "栈已空!" << endl;
		return 0;
	}
	else
	{
		Node<T>* temp = top;
		T save = top->ch;
		top = top->next;
		delete temp;
		return save;
	}
}

template <typename T>
T Stack<T>::Gettop()
{
	if (Isempty())
	{
		cout << "栈已空!" << endl;
		return 0;
	}
	else
		return top->ch;
}

template <typename T>
void Stack<T>::PrintStr()
{
	if (Isempty())
	{
		cout << "栈已空!" << endl;
		return;
	}
	else
	{
		Node* temp = top;
		while (temp != nullptr)
		{
			cout << temp->ch << ' ';
			temp = temp->next;
		}
	}
}
bool Isoperator(char ch)
{
	switch (ch)
	{
	case '+':
	case '-':
	case '*':
	case '/':
		return true;
		break;
	default:
		return false;
		break;
	}
}

int Priority(char ch)
{
	switch(ch)
	{
		case '*':
		case '/':
			return 2;
			break;
		case '+':
		case '-':
			return 1;
			break;
		default:
			return 0;
			break;
	}
}
bool Isdigital(char ch)
{
	if (ch >= '0' && ch <= '9')
		return true;
	else
		return false;
}
string Mid_convert_Rear(string& str)
{
	string saveone;
	int num = 0;
	Stack<char> exp;
	for (num = 0; num < str.length(); num++)
	{
		if (Isdigital(str[num]))//是数字直接进输入序列
			saveone += str[num];
		else if (str[num] == '(')//左括号直接入栈
			exp.Push(str[num]);
		else if (str[num] == ')')//右括号一直出栈直到遇到左括号
		{
			while (exp.Gettop() != '(')
			{
				saveone += exp.Pop();
			}
			if (exp.Gettop() == '(')
				exp.Pop();
		}
		else if (Isoperator(str[num]))//如果是加减乘除符号(最麻烦的/(ㄒoㄒ)/~~)
		{
			while (!exp.Isempty() && exp.Gettop() != '(' && Priority(str[num]) <= Priority(exp.Gettop()))
			{
				saveone += exp.Pop();
			}
			/*读到加减乘除符号时,只会进行两种操作:(读到的符号比当前栈顶优先级高、栈顶为左括号、
			栈为空,入栈)\(读到的符号不满足要求,一直弹出栈顶元素直到满足要求入栈)*/
			if (exp.Isempty() || exp.Gettop() == '(' || Priority(str[num]) > Priority(exp.Gettop()))
				exp.Push(str[num]);
		}
	}
	while (!exp.Isempty())
		saveone += exp.Pop();
	return saveone;
}

int Calculate(string& str)
{
	Stack<int> one;
	int num = 0, numen = 0;
	int res = 0;
	for (int i = 0; i < str.length(); i++)
	{
		if (Isdigital(str[i]))
		{
			res = str[i] - '0';
			one.Push(res);
		}
		else if (Isoperator(str[i]))
		{
			num = one.Pop();
			numen = one.Pop();
			switch (str[i])
			{
			case '+':
				res = numen + num;
				one.Push(res);
				break;
			case '-':
				res = numen - num;
				one.Push(res);
				break;
			case '*':
				res = numen * num;
				one.Push(res);
				break;
			case '/':
				res = numen / num;
				one.Push(res);
				break;
			}
		}
	}
	return one.Gettop();
}
int main()
{
	string Inputstr;
	cout << "请输入表达式:" << endl;
	cin >> Inputstr;
	string result = Mid_convert_Rear(Inputstr);
	int res = Calculate(result);
	cout << "转换的后缀表达式为:" << result<< endl;
	cout << "计算的结果为:" << res;
	return 0;
}
举报

相关推荐

0 条评论