Pagini recente » Cod sursa (job #1098539) | Cod sursa (job #1125372) | Cod sursa (job #1076076) | Cod sursa (job #1210664) | Cod sursa (job #2539743)
#include <bits/stdc++.h>
#define DimMax 100005
using namespace std;
ifstream fin("evaluare.in");
ofstream fout("evaluare.out");
char c;
char v[DimMax * 2]; int lg;
char stiva[DimMax]; int vf;
long long st[DimMax * 2], i;
bool Eoperand(char c)
{
if(c >= '0' && c <= '9') return true;
return false;
}
int ierarhie(char c)
{
if(c == '(') return 1;
else if(c == ')') return 2;
else if(c == '+' || c == '-') return 3;
else return 4;
}
int main()
{
while(1)
{
fin.get(c);
if(c == '\n') break;
if(Eoperand(c))
{
do
{
v[lg++] = c;
fin.get(c);
}while(Eoperand(c));
if(c == '\n') break;
v[lg++] = ' ';
}
if(c == '(')
{
stiva[++vf] = '(';
}
else if(c == ')')
{
while(stiva[vf] != '(' && vf > 0)
{
v[lg++] = stiva[vf]; v[lg++] = ' ';
vf--;
}
vf--;
}
else
{
while(ierarhie(stiva[vf]) > ierarhie(c) && vf > 0)
{
v[lg++] = stiva[vf]; v[lg++] = ' ';
vf--;
}
stiva[++vf] = c;
}
}
if(vf > 0)
{
v[lg++] = ' ';
while(vf > 0)
{
v[lg++] = stiva[vf]; v[lg++] = ' ';
vf--;
}
}
i = 0;
vf = 0;
while(i < lg)
{
if(v[i] != ' ')
{
if(Eoperand(v[i]))
{
long long nr = 0;
while(Eoperand(v[i]))
{
nr = nr * 10 + (v[i] - '0');
i++;
}
st[++vf] = nr;
}
else
{
long long op1 = st[vf]; vf--;
long long op2 = st[vf]; vf--;
if(v[i] == '+') st[++vf] = op2 + op1;
else if(v[i] == '-') st[++vf] = op2 - op1;
else if(v[i] == '*') st[++vf] = op2 * op1;
else st[++vf] = op2 / op1;
}
}
i++;
}
fout<<st[vf];
return 0;
}