Cod sursa(job #1840907)

Utilizator bt.panteaPantea Beniamin bt.pantea Data 4 ianuarie 2017 23:02:06
Problema Evaluarea unei expresii Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 3.45 kb
#include <iostream>
#include <fstream>
#include <stack>
#define DMAX 100010

using namespace std;

ifstream f ("evaluare.in");
ofstream g ("evaluare.out");

char s[DMAX];
struct Element
{
    int value;
    bool isOperator;
    char op;
};

typedef stack<char> OpStack;
typedef stack<Element> FormPol;

int Result(FormPol & f) // postfix form to tree
{
    Element el = f.top();
    f.pop();
    if (!el.isOperator)
        return el.value;
    char op = el.op;
    int right = Result(f);
    int left = Result(f);
    if (op == '+')
        return left + right;
    if (op == '-')
        return left - right;
    if (op == '*')
        return left * right;
    if (op == '/')
        return left / right;
    return 0;
}

void Write(FormPol form)
{
    if (!form.empty())
    {
        Element el = form.top();
        form.pop();
        Write(form);
        if (el.isOperator == true)
            cout << el.op << ' ';
        else
            cout << el.value << ' ';
    }
}

inline bool isDigit(char c)
{
    if (c >= '0' && c <= '9')
        return true;
    else return false;
}

int Number(char * & c)
{
    int rez = 0;
    while (isDigit(*c))
    {
        rez *= 10;
        rez += *c - 48;
        c++;
    }
    return rez;
}

inline int Priority(char c)
{
    if (c == '*' || c == '/')
        return 2;
    else if (c == '+' || c == '-')
        return 1;
    return 0;
}

bool isValidCharacter(char c)
{
    if (c >= '0' && c <= '9')
        return true;
    if (c == '+' || c == '-' || c == '*' || c == '/')
        return true;
    if (c == '(' || c== ')')
        return true;
    return false;
}

int Expresion(char * & c)
{
    //cout << *c;
    // infix form in postfix form

    FormPol formPol;
    OpStack op;
    while (*c != ')' && isValidCharacter(*c))
    {
        cout << *c;
        if (*c == '(')
        {
            c++;
            Element el;
            el.isOperator = false;
            el.value = Expresion(c);
            formPol.push(el);
        }
        else if (isDigit(*c))
        {
            Element el;
            el.value = Number(c);
            //cout<<Number(c);
            el.isOperator = false;
            formPol.push(el);
        }
        else if (Priority(*c) == 2)
        {

            if (!op.empty() && Priority(op.top()) == 2)
            {
                Element el;
                el.isOperator = true;
                el.op = op.top();
                op.pop();
                formPol.push(el);
            }
            op.push(*c);
            c++;
        }
        else if (Priority(*c) == 1)
        {
            Element el;
            el.value = 0;
            el.isOperator = true;
            while (!op.empty())
            {
                el.op = op.top();
                op.pop();
                formPol.push(el);
            }
            op.push(*c);
            c++;
        }
        //else cout<<(int)*c << ' ' << *c;
            //c++;
    }

    Element el;
    el.isOperator = true;
    while (!op.empty())
    {
        el.op = op.top();
        op.pop();
        formPol.push(el);
    }

    if (*c == ')')
        c++;

    //Write(formPol);

    return Result(formPol); // postfix form to tree
    //return 0;
}

int main()
{
    char * c;
    f >> s;
    c = s;
    int result = Expresion(c);
    g << result;
    cout << result;
    return 0;
}