栈应用——中缀转后缀+后缀计算

中缀表达式(infix expression)即  平时生活中大家对于算式的书写格式( eg: 6*((5+(2+3)*8)+3)  );

后缀表达式(post expression)即  把数字和运算符分开,把运算符的优先级运算内涵到后缀式的数字和运算符的顺序中(故其优点就是,没有必要知道任何优先的规则),一个运算符只对其前边的两个数字起作用( eg: 652 3+8*+3+*);


中缀转后缀:

·读入表达式,若是数字放入输出字符串;

·若是运算符,则先与栈顶元素比较:

-栈空,直接压入栈;

-否则比较优先级:(优先级顺序:当前(> *、/> +、-> 栈中 (>  ) )

~ 当前=栈顶:出栈后压入当前;

~ 当前<栈顶:出栈   至  当前=栈顶 进行处理 / 出栈,当前不进栈直接放入输出字符串;

~ 当前>栈顶:压入栈;

~ 若是 )     :出栈 至 ( 为止,但是(和)不放入输出字符串。

时间复杂度: O(N)


代码如下:(智障的我只想到了各种if,然后括号只有 () ,运算符只有 +-*/ )


string infixTOpostfix(const string s)
{
	string post_s;
	stack
     
     
      
       mark;
	for (size_t i = 0; i < s.length(); ++i) {
		if (isalnum(s[i]))
			post_s.push_back(s[i]);
		else {
			if (mark.empty()) {
				mark.push(s[i]);
			}
			else
				switch (s[i]) {
				case '+':
				case '-':
					if (mark.top() == '*' || mark.top() == '/') {
						while (mark.top() == '*' || mark.top() == '/') {
							post_s.push_back(mark.top());
							mark.pop();
						}
						if (mark.top() == '+' || mark.top() == '-') {
							post_s.push_back(mark.top());
							mark.pop();
							mark.push(s[i]);
						}
						else
							mark.push(s[i]);
					}
					else if (mark.top() == '+' || mark.top() == '-') {
						post_s.push_back(mark.top());
						mark.pop();
						mark.push(s[i]);
					}
					else mark.push(s[i]);
					break;
				case '*':
				case '/':
					if (mark.top() == '*' || mark.top() == '/') {
						post_s.push_back(mark.top());
						mark.pop();
					}
					mark.push(s[i]);
					break;
				case '(':
					mark.push(s[i]);
					break;
				case ')':
					while (mark.top() != '(' && !mark.empty()) {
						post_s.push_back(mark.top());
						mark.pop();
					}
					mark.pop();
					break;
				default:
				    break;
				}
		}
	}
	while (!mark.empty()) {
		post_s.push_back(mark.top());
		mark.pop();
	}
	return post_s;
}
     
     


后缀计算:

·见到数字时,压入栈;

·见到运算符时,从栈中弹出两个数  second 运算符 first,后再压入栈;


时间复杂度: O(N)


代码如下:(开始总不对,才意识到,char的数字转换成int,是需要  - ‘0’,得到的时和0,相差的ascii码数值)


int postfixCompute(const string s)
{
	int temp1, temp2;
	stack
     
     
      
       result;
	for (size_t i = 0; i < s.length(); ++i) {
		if (isdigit(s[i]))//与0的ascll码运算
			result.push(s[i] - '0');
		else {
			temp1 = result.top();
			result.pop();
			temp2 = result.top();
			result.pop();
			switch (s[i]) {
			case '+':
				result.push(temp1 + temp2);
				break;
			case '-':
				result.push(temp1 - temp2);
				break;
			case '*':
				result.push(temp1 * temp2);
				break;
			case '/':
				result.push(temp1 / temp2);
				break;
			default:
				break;
			}
		}
	}
	return result.top();
}
     
     


这两个是套装,故奉上main()函数,可以一试~:

//6*((5+(2+3)*8)+3)
int main()
{
	string s, result;
	char c;
	while (cin >> c)
		s.push_back(c);
	result = infixTOpostfix(s);
	cout << result << " = " << postfixCompute(result);
	getchar();
}



  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值