Cod sursa(job #2653236)

Utilizator Dragono63Stanciu Rares Stefan Dragono63 Data 27 septembrie 2020 13:51:04
Problema Evaluarea unei expresii Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.87 kb
#include <bits/stdc++.h>

using namespace std;

/************************************************/
///INPUT / OUTPUT
ifstream f("evaluare.in");
ofstream g("evaluare.out");
/************************************************/
/// GLOBAL DECLARATIONS
long long operatie[100001],q[100001],n,numIndex,opIndex,number;
char s[100001];
/************************************************/
/// FUNCTIONS

//main() -> Solution()
//Solution() -> Main Logic
//readInput() -> Reads Input
//Output() -> Displays Output
/************************************************/
///---------------------------------------------------------------------
inline void readInput()
{
    f.getline(s, 100001);
    n = strlen(s);
}
///---------------------------------------------------------------------
int grad(char c)
{
    if(c == '+' || c == '-') return 1;
    if(c == '*' || c == '/') return 2;
    return 0;
}
///---------------------------------------------------------------------
void calculeaza(int a,int b)
{
    if(operatie[b]=='+')
    {
        q[a-1]+=q[a];
    }
    if(operatie[b]=='-')
    {
        q[a-1]-=q[a];
    }
    if(operatie[b]=='*')
    {
        q[a-1]*=q[a];
    }
    if(operatie[b]=='/')
    {
        q[a-1]/=q[a];
    }
    return;
}
///---------------------------------------------------------------------
inline void Solution()
{
    for(int i = 0 ; i < n ; ++ i)
    {
        if(isdigit(s[i]))
        {
            number = 0;
            while(isdigit(s[i]))
            {
                number *= 10;
                number += (s[i] - '0');
                i++;
            }
            i--;
            q[++numIndex] = number;
        }
        else
        {
            if(opIndex == 0 || s[i] == '(' || grad(operatie[opIndex]) < grad(s[i]))
            {
                operatie[++ opIndex] = s[i];
            }
            else if(s[i] == ')')
            {
                while(operatie[opIndex] != '(')
                {
                    calculeaza(numIndex, opIndex);
                    numIndex --;
                    opIndex --;
                }
                opIndex --;
            }
            else
            {
                while(grad(s[i]) <= grad(operatie[opIndex]))
                {
                    calculeaza(numIndex, opIndex);
                    numIndex --;
                    opIndex --;
                }
                operatie[++ opIndex] = s[i];
            }
        }

    }
    while(opIndex)
    {
        calculeaza(numIndex, opIndex);
        numIndex --;
        opIndex --;
    }
}
///---------------------------------------------------------------------
inline void Output()
{
    g << q[1];
}
///---------------------------------------------------------------------
int main()
{
    readInput();
    Solution();
    Output();
    return 0;
}