Cod sursa(job #3294117)

Utilizator RaresVilcuVilcu Rares Andrei RaresVilcu Data 15 aprilie 2025 21:55:03
Problema Evaluarea unei expresii Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.28 kb
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

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

string s;
int x = 0;

int adunscad();
int inmimp();

int numere()
{
    if(s[x] == '(')
    {
        x++;
        int rez = adunscad();
        x++;
        return rez;
    }
    else
    {
        int nr = 0;
        while (x < s.size() && isdigit(s[x]))
            nr = nr * 10 + (s[x++] - '0');
        return nr;
    }
}

int adunscad()
{
    int rez = inmimp();
    while (x < s.size() && (s[x] == '+' || s[x] == '-'))
    {
        char op = s[x++];
        int next = inmimp();
        if(op == '+')
                rez += next;
        else rez -= next;

        }
    return rez;
}

int inmimp()
{
    int rez = numere();
    while(x < s.size() && (s[x] == '*' || s[x] == '/'))
    {
        char op = s[x++];
        int next = numere();
        if(op == '*')
            rez *= next;
        else
        {
            if (next == 0)
            {
                fout<< "impartire la 0";
                exit(1);
            }
            else rez /= next;
        }
    }
    return rez;
}

int main()
{
    fin>>s;
    int rez = adunscad();
    fout << rez;
    return 0;
}