Pagini recente » Cod sursa (job #2288630) | Cod sursa (job #1499146) | Cod sursa (job #2187683) | Cod sursa (job #1348939) | Cod sursa (job #1838766)
#include <iostream>
#include <fstream>
using namespace std;
string s;
int ch=0;
int eval();
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++;rez-=factor();}
else {if(s[ch]=='+')ch++;rez+=factor();}
}
}
int main()
{
ifstream f("evaluare.in");
ofstream g("evaluare.out");
f>>s;
s+=')';
g<<eval();
return 0;
}