Cod sursa(job #2761539)
| Utilizator | Data | 2 iulie 2021 16:54:55 | |
|---|---|---|---|
| Problema | Evaluarea unei expresii | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 2.27 kb |
#include <bits/stdc++.h>
#define paranteza 1000000001
#define inmultire 1000000002
#define scadere 1000000003
#define impartire 1000000004
using namespace std;
ifstream fin("evaluare.in");
ofstream fout("evaluare.out");
char a[100005];
int st[100005];
int top, x;
int main()
{
fin >> a;
for(int i=0; a[i]!=0;)
{
if(a[i]=='(')
{
st[++top]=paranteza;
i++;
}
if(a[i]=='+')
{
i++;
}
if(a[i]=='-')
{
st[++top]=scadere;
i++;
}
if(a[i]=='*')
{
st[++top]=inmultire;
i++;
}
if(a[i]=='/')
{
st[++top]=impartire;
i++;
}
if(isdigit(a[i]))
{
x=0;
while(isdigit(a[i]))
{
x=x*10+(a[i]-'0');
i++;
}
if(st[top]==scadere)
{
x=-x;
top--;
}
while(st[top]==inmultire && top>0)
{
top--;
x=st[top]*x;
top--;
}
while(st[top]==impartire && top>0)
{
top--;
x=st[top]/x;
top--;
}
st[++top]=x;
}
if(a[i]==')')
{
x=0;
while(st[top]!=paranteza && top>0)
{
x+=st[top];
top--;
}
top--; // stergerea parantezei deschise din stiva
// calcularea numerelor din spatele parantezei
while(st[top]==inmultire && top>0)
{
top--;
x=st[top]*x;
top--;
}
while(st[top]==impartire && top>0)
{
top--;
x=st[top]/x;
top--;
}
if(st[top]==scadere)
{
x=-x;
top--;
}
st[++top]=x;
i++;
}
}
x=0;
for(int i=1; i<=top; i++)
{
x+=st[i];
}
fout << x;
return 0;
}