Pagini recente » Cod sursa (job #1077775) | Cod sursa (job #1809680) | Cod sursa (job #147806) | Cod sursa (job #1511003) | Cod sursa (job #1831268)
#include <fstream>
#include <string>
#include <iostream>
std::fstream f("evaluare.in",std::ios::in);
std::ofstream g("evaluare.out");
std::string st;
int index = 0;
int solve();
int tr();
int fn();
int main()
{
int nr;
f>>st;
nr = solve();
g<<nr;
return 0;
}
int solve()
{
int x = tr();
while(st[index] == '+' || st[index] == '-')
{
if(st[index] == '+')
{
++index;
x+=tr();
}
if(st[index] == '-')
{
++index;
x-=tr();
}
}
return x;
}
int tr()
{
int x = fn();
while(st[index] == '*' || st[index] == '/')
{
if(st[index] == '*')
{
++index;
x*=fn();
}
if(st[index] == '/')
{
++index;
x/=fn();
}
}
return x;
}
int fn()
{
int x = 0;
if(st[index] =='(')
{
++index;
x = solve();
++index;
}
else
{
while(st[index]>='0' && st[index]<'9')
{
x = x*10+ int(st[index]-'0');
++index;
}
}
return x;
}