Cod sursa(job #2896947)

Utilizator MihneaCadar101Cadar Mihnea MihneaCadar101 Data 1 mai 2022 16:49:25
Problema Evaluarea unei expresii Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.23 kb
#include <bits/stdc++.h>
using namespace std;

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

const int nmax = 1e5 + 4;

char in[nmax], post[nmax];
char *infix, *postfix, *postfix2;
stack <char> stiva;
stack <int> stiva2;

bool isoperator (char ch) {
    if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
        return true;

    return false;
}

int priority(char ch) {
    if (ch == '+' || ch == '-')
        return 0;

    return 1;
}

void in_post() {
    while (*infix) {
        if (isdigit(*infix)) {
            while (isdigit(*infix)) {
                *postfix = *infix;
                ++postfix;
                ++infix;

            }

            *postfix = ' ';
            ++postfix;
        }

        if (*infix == '(') {
            stiva.push(*infix);
            ++infix;
        }

        if (*infix == ')') {
            char calcul = stiva.top();
            stiva.pop();
            while (calcul != '(') {
                *postfix = calcul;
                ++postfix;
                *postfix = ' ';
                ++postfix;
                calcul = stiva.top();
                stiva.pop();
            }

            ++infix;
        }

        if (isoperator(*infix)) {
            if (stiva.empty())
                stiva.push(*infix);
            else {
                while (!stiva.empty() && stiva.top() != '(' && priority(stiva.top()) >= priority(*infix)) {
                    char calcul = stiva.top();
                    stiva.pop();
                    *postfix = calcul;
                    ++postfix;
                    *postfix = ' ';
                    ++postfix;
                }

                stiva.push(*infix);
            }

            ++infix;
        }
    }
}

void creare() {
    while(!stiva.empty()) {
        *postfix = stiva.top();
        stiva.pop();
        ++postfix;
        *postfix = ' ';
        ++postfix;
    }

    *postfix = '\0';
}

int finall() {
    int result = 0, x, y;
    while(*postfix2) {
        while (*postfix2 == ' ')
            ++postfix2;

        if (isdigit(*postfix2)) {
            int nr = 0;
            while(isdigit(*postfix2)) {
                nr = nr * 10 + (*postfix2 - '0');
                ++postfix2;
            }

            stiva2.push(nr);
            ++postfix2;
        }

        if (isoperator(*postfix2)) {
            y = stiva2.top();
            stiva2.pop();
            x = stiva2.top();
            stiva2.pop();
            switch(*postfix2) {
                case '+':
                    result = x + y;
                    break;

                case '-':
                    result = x - y;
                    break;

                case '*':
                    result = x * y;
                    break;

                case '/':
                    result = x / y;
                    break;
            }

            stiva2.push(result);
            ++postfix2;
        }
    }

    result = stiva2.top();
    stiva2.pop();
    return result;
}

int main()
{
    fin >> in;
    infix = in;
    postfix = post;
    in_post();
    creare();
    postfix2 = post;
    fout << finall();
    return 0;
}