Pagini recente » Cod sursa (job #1081326) | Cod sursa (job #3274650) | Cod sursa (job #60469) | Cod sursa (job #919343) | Cod sursa (job #3286128)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("evaluare.in");
ofstream fout("evaluare.out");
string op;
int stiva[100005];
const int inmultire = 1e9+1, parant=1e9+2, scad=1e9+3, impart=1e9+5;
int main()
{
int top=0;
getline(fin, op);
for(int i=0; i < op.size(); i++)
{
if(isdigit(op[i]))
{
int nr=0;
while(i < op.size() && isdigit(op[i]))
{
nr=nr*10+op[i]-'0';
i++;
}
i--;
if(top==0)
stiva[++top]=nr;
else if(stiva[top]==inmultire)
stiva[--top]*=nr;
else if(stiva[top]==impart)
stiva[--top]/=nr;
else if(stiva[top]==scad)
stiva[top]=-nr;
else
stiva[++top]=nr;
}
else if(op[i]=='*')
stiva[++top]=inmultire;
else if(op[i]=='-')
stiva[++top]=scad;
else if(op[i]=='+')
continue;
else if(op[i]=='(')
stiva[++top]=parant;
else if(op[i]=='/')
stiva[++top]=impart;
else
{
int nr=0;
while(stiva[top]!=parant)
nr+=stiva[top--];
top--;
if(top==0)
stiva[++top]=nr;
else if(stiva[top]==inmultire)
stiva[--top]*=nr;
else if(stiva[top]==impart)
stiva[--top]/=nr;
else if(stiva[top]==scad)
stiva[top]=-nr;
else
stiva[++top]=nr;
}
}
int rez=0;
for(int i=1; i <= top; i++)
{
rez+=stiva[i];
}
fout << rez;
return 0;
}