Pagini recente » Cod sursa (job #2296175) | Cod sursa (job #1095849) | Cod sursa (job #2860985) | Cod sursa (job #931589) | Cod sursa (job #1862291)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("evaluare.in"); ofstream g("evaluare.out");
string s; int ch;
int termen()
{
int num=0;
if(s[ch]=='(') ++ch , return eval();
while(s[ch]>='0' && s[ch]<='9')
{
num=num*10+s[ch]-'0';
++ch;
}
return num;
}
int factor()
{
int x=termen();
while(true)
{
if(s[ch]=='*') ++ch, x*=termen();
else
{
if(s[ch]=='/') ++ch, x/=termen();
else return x;
}
}
}
int eval()
{
int rez=0;
while(true)
{
if(s[ch]==')') ++ch, return rez;
if(s[ch]=='-') ++ch, return rez-=factor();
else
{
if(s[ch]=='+') ++ch;
return rez+=factor();
}
}
}
int main()
{
f>>s;
s+=')';
g<<eval();
return 0;
}