Cod sursa(job #3263939)

Utilizator TeogaloiuMatei Ionescu Teogaloiu Data 17 decembrie 2024 10:44:03
Problema Evaluarea unei expresii Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.87 kb
#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;
}