Pagini recente » Cod sursa (job #3030676) | Cod sursa (job #1856600) | Cod sursa (job #740301) | Cod sursa (job #1955267) | Cod sursa (job #3263939)
#include <fstream>
#include <string>
using namespace std;
ofstream cout ("evaluare.out");
ifstream cin ("evaluare.in");
string exp;
int pos=0;
int p1(int& pos);
int p2(int& pos);
int p3(int& pos);
int main(){
cin>>exp;
cout<<p3(pos);
}
int p1(int& pos)
{
int total=0;
if(exp[pos]=='(')
{
total+=p3(++pos);
pos++;
}
while(exp[pos]>='0' and exp[pos]<='9')
{
total*=10;
total+=exp[pos++]-'0';
}
return total;
}
int p2(int& pos)
{
int total=p1(pos);
while(exp[pos]=='*' or exp[pos]=='/')
if(exp[pos]=='*')
total=total*p1(++pos);
else
total=total/p1(++pos);
return total;
}
int p3(int& pos)
{
int total=p2(pos);
while(exp[pos]=='+' or exp[pos]=='-')
if(exp[pos]=='+')
total+=p2(++pos);
else
total-=p2(++pos);
return total;
}