Pagini recente » Cod sursa (job #863075) | Cod sursa (job #2238748) | Cod sursa (job #2170354) | Cod sursa (job #1924921) | Cod sursa (job #1863949)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("evaluare.in"); ofstream g("evaluare.out");
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()
{
f>>s;
s+=')';
g<<eval();
return 0;
}