Cod sursa(job #2875419)

Utilizator mihairazvan03Dana Mihai mihairazvan03 Data 21 martie 2022 17:11:00
Problema Evaluarea unei expresii Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.96 kb
#include <iostream>
#include <fstream>
#include <stack>
#include <string.h>

using namespace std;
ifstream in("evaluare.in");
ofstream out("evaluare.out");

int i, j, nr, nr2, suma;
char s[100100], actual[100];
stack<string> stiva;

int main()
{
    cin>>s;

    for(i = 0; s[i]; i++)
    {

        if(s[i] >= '0' && s[i] <= '9')
        {
            nr = 0;
            while(s[i] && s[i] >= '0' && s[i] <= '9')
            {
                nr = nr * 10 + (s[i] - '0');
                i++;
            }

            i--;

            if(stiva.empty() == false)
            {
                if(stiva.top() == "*")
                {
                    stiva.pop();
                    nr2 = 0;
                    string elemAnt = stiva.top();
                    for(j = 0; elemAnt[j]; j++)
                        nr2 = nr2 * 10 + (elemAnt[j] - '0');

                    stiva.pop();
                    nr = nr * nr2;
                }
                else if(stiva.top() == "/")
                {
                    stiva.pop();
                    nr2 = 0;
                    string elemAnt = stiva.top();
                    for(j = 0; elemAnt[j]; j++)
                        nr2 = nr2 * 10 + (elemAnt[j] - '0');

                    stiva.pop();
                    nr = nr2 / nr;
                }
            }

            stiva.push(to_string(nr));
        }
        else if(s[i] == '(')
        {
            stiva.push("(");
        }
        else if(s[i] == ')')
        {
            suma = 0;
            while(stiva.top() != "(")
            {
                string elem = stiva.top();
                stiva.pop();
                nr = 0;
                for(j = 0; elem[j]; j++)
                    nr = nr * 10 + (elem[j] - '0');

                if(stiva.top() == "-")
                {
                    suma -= nr;
                    stiva.pop();
                }
                else if(stiva.top() == "+")
                {
                    suma += nr;
                    stiva.pop();
                }
                else if(stiva.top() == "(")
                    suma += nr;
            }

            stiva.pop();   //pop la (


            if(stiva.empty() == false)
            {
                if(stiva.top() == "*")
                {
                    stiva.pop();
                    nr2 = 0;
                    string elemAnt = stiva.top();
                    for(j = 0; elemAnt[j]; j++)
                        nr2 = nr2 * 10 + (elemAnt[j] - '0');

                    stiva.pop();
                    suma = suma * nr2;
                }
                else if(stiva.top() == "/")
                {
                    stiva.pop();
                    nr2 = 0;
                    string elemAnt = stiva.top();
                    for(j = 0; elemAnt[j]; j++)
                        nr2 = nr2 * 10 + (elemAnt[j] - '0');

                    stiva.pop();
                    suma = nr2 / suma;
                }
            }

            stiva.push(to_string(suma));

        }
        else if(s[i] == '+')
            stiva.push("+");
        else if(s[i] == '-')
            stiva.push("-");
        else if(s[i] == '*')
            stiva.push("*");
        else if(s[i] == '/')
            stiva.push("/");
    }


    suma = 0;
    while(stiva.empty() == false)
    {
        string elem = stiva.top();
        stiva.pop();
        nr = 0;
        for(j = 0; elem[j]; j++)
            nr = nr * 10 + (elem[j] - '0');

        if(stiva.empty() == true)
            suma += nr;
        else if(stiva.top() == "-")
        {
            suma -= nr;
            stiva.pop();
        }
        else if(stiva.top() == "+")
        {
            suma += nr;
            stiva.pop();
        }
    }

    cout<<suma;

    /*while(stiva.empty() == false)
    {
        cout<<stiva.top()<<" ";
        stiva.pop();
    }*/

    return 0;
}