So Easy!!! (栈模拟)

So Easy!!!

Problem Description

yizhen has no girlfriend due to his stupid brain that he even can’t solve a simple arithmetic roblem. Can you help him? If you solve it and tell him the result, then he can find his lovers! So beautiful!

Input

The input consists of multiple test cases. Each test case contains some integers (every number is smaller than 2^31-1)and operator on a single line (operator including *,+,-,/ )  Process to end of file.

There are no space between number and operator and I promise that all those results of A/B is integer.

Output

For each test case, output the result of the arithmetic problem.( All those results isn’t decimal number)

Sample Input

1+2+3

1*2+2+4-5

Sample Output

6

3

//题意:

给你一串数学计算公式,只有+,-,*,/,四种运算符,问最终结果是多少?

//思路:

直接栈模拟

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<stack>
using namespace std;
typedef long long LL;
char s[1000010];
bool is_diget(int x)
{
	if(x >= '0' && x <= '9')return true;
	else return false;
}
int main()
{
	while(~scanf("%s", s))
	{
		stack<LL>S;
		LL temp, temp1;
		for(int i = 0; s[i];)
		{
			if(is_diget(s[i]))
			{
				 temp = 0;
				while(is_diget(s[i]))
				{
					temp = temp * 10 + s[i] - '0';
					i++;
				}
				S.push(temp);
			}
			else
			{
				switch(s[i])
				{
					case '+':
						i++;
						break;
					case '-':
						i++;
						temp = 0;
						while(is_diget(s[i]))
						{
							temp = temp * 10 + s[i] - '0';
							i++;
						}
						temp = -temp; 
						S.push(temp);
						break;
					case '*':
						i++;
						temp = 0;
						while(is_diget(s[i]))
						{
							temp = temp * 10 + s[i] - '0';
							i++;
						}
						temp1 = S.top();
						S.pop();
						S.push(temp1 * temp);
						break;
					case '/':
						i++;
						temp = 0;
						while(is_diget(s[i]))
						{
							temp = temp * 10 + s[i] - '0';
							i++;
						}
						temp1 = S.top();
						S.pop();
						S.push(temp1 / temp);
						break;
				}
			}
		}
		temp = 0;
		while(!S.empty())
		{
			temp += S.top();
			S.pop();
		}
		printf("%lld\n",temp);
	}
	return 0;
}


 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值