Cod sursa(job #3263935)

Utilizator deliaandreeaddelia andreea deliaandreead Data 17 decembrie 2024 10:38:03
Problema Evaluarea unei expresii Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.18 kb
#include <iostream>
#include <fostream>

ifstream fin("evaluare.in");
ofstream fout("evaluare.out");

using namespace std;

int p1(string &sir, int &poz);

int p2(string &sir, int &poz){
    int total=p1(sir,poz);
    while(sir[poz]=='*' || sir[poz]=='/'){
        if(sir[poz]=='*'){
            poz++;
            total*=p1(sir,poz);
        }
        else{
            poz++;
            total/=p1(sir,poz);
        }
    }
    return total;
}

int p3(string &sir, int &poz){
    int total=p2(sir,poz);
    while(sir[poz]=='+' || sir[poz]=='-'){
        if(sir[poz]=='+'){
            poz++;
            total+=p2(sir,poz);
        }
        else{
            poz++;
            total-=p2(sir,poz);
        }
    }
    return total;
}

int p1(string &sir, int &poz){
    int total=0;
    if(sir[poz]=='('){
        poz++;
        total+=p3(sir,poz);
        poz++;
    }
    else{
        while(sir[poz]>='0' && sir[poz]<='9'){
            total*=10;
            total+=sir[poz]-'0';
            poz++;
        }
    }
    return total;
}

int main()
{
    string sir;
    fin>>sir;
    int poz=0;
    fout<<p3(sir,poz);
    return 0;
}