Cod sursa(job #2292220)
| Utilizator | Data | 29 noiembrie 2018 10:09:25 | |
|---|---|---|---|
| Problema | Evaluarea unei expresii | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 2.05 kb |
#include <bits/stdc++.h>
#define par 1000001
#define stea 1000002
#define slash 1000003
#define scad 1000004
using namespace std;
ifstream fin("evaluare.in");
ofstream fout("evaluare.out");
char s[100025];
int st[100004],top;
int main()
{
int i, x;
fin.getline(s,100025);
for(i=0 ;s[i]; i++)
{
if(s[i] == '+') ;
else if(s[i] == '(') st[++top] = par;
else if(s[i] >='0' && s[i] <='9')
{
x = 0;
while(s[i] >='0' && s[i] <='9')
{
x = x * 10 + (s[i] - '0');
i++;
}
i--;
if(top == 0) st[++top] = x;
else
{
if(st[top] == scad)
st[top] = -x;
else if(st[top] == stea)
{
top--;
st[top] *= x;
}
else if(st[top] == slash)
{
top--;
st[top] /= x;
}
else st[++top] = x;
}
}
else if(s[i] == '-') st[++top] = scad;
else if(s[i] == '*') st[++top] = stea;
else if(s[i] == '/') st[++top] = slash;
else
{
x = 0;
while(st[top] != par)
{
x += st[top];
top--;
}
st[top] = x;
top--;
if(top == 0) st[++top] = x;
else
{
if(st[top] == scad)
st[top] = -x;
else if(st[top] == stea)
{
top--;
st[top] *= x;
}
else if(st[top] == slash)
{
top--;
st[top] /= x;
}
else st[++top] = x;
}
}
}
x = 0;
for(i=1 ;i<=top; i++)
x+= st[i];
fout << x << "\n";
return 0;
}
