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;
}