后缀运算

#include "stdafx.h"
#include <iostream>
#include<string>
#include <stdlib.h>
#include<stack>
using namespace std;

int main()
{
	stack<int> stk;
	string str;
	cin >> str;
	char h;
	int sum, b;
	for (int i = 0; i < str.length(); i++) {
		if (str[i] != '+' && str[i] != '-' && str[i] != '*') {
			if (str[i] >= 'A'&&str[i] <= 'F') 
				stk.push(str[i] - 'A'+10); 
			else
			    stk.push(str[i] - '0');		
		}
		else {
			sum = stk.top();
			stk.pop();
			b = stk.top();
			stk.pop();
			h = str[i];
			switch (h)
			{
			    case '+':sum = sum + b ; stk.push(sum); break;  
				case '-':sum = sum - b; stk.push(sum); break;
				case '*':sum = sum * b; stk.push(sum); break;
			}
		}
	}
	cout << stk.top();
	while (1);
}

  

posted @ 2017-03-17 21:24  lineaar  阅读(262)  评论(0编辑  收藏  举报