Cod sursa(job #2721368)

Utilizator MihaiZ777MihaiZ MihaiZ777 Data 11 martie 2021 18:57:52
Problema Evaluarea unei expresii Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.37 kb
#include <iostream>
#include <fstream>
#include <string>
using namespace std;


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

string str;
int index = 0;


int firstPriority();
int secondPriority();
int thirdPriority();


// ()
int firstPriority()
{
    int nr = 0;

    if (str[index] == '(')
    {
        index++;
        nr += thirdPriority();
        index++;
    }
    else
    {
        while(isdigit(str[index]))
        {
            nr *= 10;
            nr += str[index++] - '0';
        }
    }

    return nr;
}

// * /
int secondPriority()
{
    int nr = firstPriority();

    while (str[index] == '*' || str[index] == '/')
    {
        if (str[index] == '*')
        {
            index++;
            nr *= firstPriority();
        }
        else
        {
            index++;
            nr /= firstPriority();
        }
    }


    return nr;
}


// + -
int thirdPriority()
{
    int nr = secondPriority();

    while (str[index] == '+' || str[index] == '-')
    {
        if (str[index] == '+')
        {
            index++;
            nr += secondPriority();
        }
        else
        {
            index++;
            nr -= secondPriority();
        }
    }



    return nr;
}


int main()
{
    fin >> str;

    fout << thirdPriority();

    return 0;
}