Pagini recente » Cod sursa (job #600398) | Cod sursa (job #1858810) | Cod sursa (job #1802539) | Cod sursa (job #1892239) | Cod sursa (job #2562404)
#include <iostream>
#include <fstream>
#include <stack>
#include <cstring>
#define plus 2000000000
#define minus 2000000001
#define div 2000000002
#define mult 2000000003
#define paran 2000000004
#define stop 2000000005
using namespace std;
ifstream fin("evaluare.in");
ofstream fout("evaluare.out");
char s[100009];
int n;
int gt;
stack<int> S, Result;
void Read()
{
int i,j;
fin>>s;
n = strlen(s);
}
int Number(int &i)
{
int nr = 0;
int p = 1;
while(isdigit(s[i]))
{
nr = (s[i]-'0')*p + nr;
p*=10;
--i;
}
return nr;
}
int Eval()
{
int total = 0;
int x1,x2;
int op;
while(S.top()!=paran)
{
x1 = S.top();
S.pop();
op = S.top();
S.pop();
if(op == plus)
total += x1;
if(op == minus)
total -= x1;
if(op == mult)
{
x2 = S.top();
S.pop();
S.push(x1*x2);
}
if(op == div)
{
x2 = S.top();
S.pop();
S.push(x1/x2);
}
if(op == paran)
{
total += x1;
break;
}
}
return total;
}
void Do()
{
int i,j,x;
int x1,x2;
int op;
int total = 0;
i = n-1;
S.push(stop);
for(i = n-1; i>0; --i)
{
if(isdigit(s[i]))
{
x = Number(i);
S.push(x);
}
if(s[i] == ')')
S.push(paran);
if(s[i] == '/')
S.push(div);
if(s[i] == '*')
S.push(mult);
if(s[i] == '+')
S.push(plus);
if(s[i] == '-')
S.push(minus);
if(s[i] == '(')
S.push(Eval());
}
while(S.top()!=stop)
{
x1 = S.top();
S.pop();
op = S.top();
S.pop();
if(op == plus)
total += x1;
if(op == minus)
total -= x1;
if(op == mult)
{
x2 = S.top();
S.pop();
S.push(x1*x2);
}
if(op == div)
{
x2 = S.top();
S.pop();
S.push(x1/x2);
}
if(op == stop)
{
total += x1;
break;
}
}
fout<<total;
}
int main()
{
Read();
Do();
return 0;
}