Cod sursa(job #1463919)

Utilizator BogdanisarBurcea Bogdan Madalin Bogdanisar Data 21 iulie 2015 20:16:19
Problema Evaluarea unei expresii Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 3.44 kb
#include<iostream>
#include<fstream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<bitset>
#include<cstring>
#include<queue>

#define ull unsigned long long
#define ll long long
#define pb push_back
#define FOR(a,b,c) for (int a=b;a<=c; ++a)
#define ROF(a,b,c) for (int a=b;a>=c; --a)

using namespace std;
ifstream f("evaluare.in");
ofstream g("evaluare.out");
int N,rez,dimpolpost;
int prio[50];
char sir[100010];

struct pol {
    int nr;
    char c;
    bool e_nr;
};
pol polpost[100010]; // polpost[i]=fiecare element al formei poloneze postfixate. daca e_nr=true => avem un operand else avem un operator

struct stiva { // stiva alocata dinamic
    char val;
    stiva* next=NULL;
};
stiva* stiv;

struct stiva2 { // stiva alocata dinamic
    int val;
    stiva2* next=NULL;
};
stiva2* aux;

bool gol();
char top();
void pop();
void push(char);
bool gol2();
int top2();
void pop2();
void push2(int);
void determina_nr(int&,int&);

int main()
{
    f>>sir;
    N=strlen(sir);
    prio['(']=1;
    prio[')']=2;
    prio['+']=prio['-']=3;
    prio['*']=prio['/']=4;
    stiv=new stiva;
    for (int i=0;i<N;++i) {
        if (sir[i]>=48 && sir[i]<=57) {
            int nr=0;
            determina_nr(nr,i);
            polpost[++dimpolpost].nr=nr;
            polpost[dimpolpost].e_nr=true;
            --i;
        }
        else if (sir[i]=='(') {
            push('(');
        }
        else if (sir[i]==')') {
            while (top()!='(') {
                polpost[++dimpolpost].c=top();
                polpost[dimpolpost].e_nr=false;
                pop();
            }
            pop();
        }
        else {
            while (!gol() && prio[top()]>=prio[sir[i]]) {
                polpost[++dimpolpost].c=top();
                polpost[dimpolpost].e_nr=false;
                pop();
            }
            push(sir[i]);
        }
    }
    while (!gol()) {
        polpost[++dimpolpost].c=top();
        polpost[dimpolpost].e_nr=false;
        pop();
    }
    aux=new stiva2;
    FOR (i,1,dimpolpost) {
        if (polpost[i].e_nr) {
            push2(polpost[i].nr);
        }
        else {
            int nr1=top2();
            pop2();
            int nr2=top2();
            pop2();
            if (polpost[i].c=='+') {
                push2(nr2+nr1);
            }
            else if (polpost[i].c=='-') {
                push2(nr2-nr1);
            }
            else if (polpost[i].c=='*') {
                push2(nr2*nr1);
            }
            else if (polpost[i].c=='/') {
                push2(nr2/nr1);
            }
        }
    }
    g<<top2();
    f.close();g.close();
    return 0;
}

bool gol() {
    if (stiv->next==NULL) {
        return true;
    }
    return false;
}

char top() {
    return stiv->val;
}

void pop() {
    stiva* p=stiv;
    stiv=stiv->next;
    delete p;
}

void push(char x) {
    stiva* p=new stiva;
    p->val=x;
    p->next=stiv;
    stiv=p;
}

bool gol2() {
    if (aux->next==NULL) {
        return true;
    }
    return false;
}
int top2() {
    return aux->val;
}
void pop2() {
    stiva2* p=aux;
    aux=aux->next;
    delete p;
}
void push2(int x) {
    stiva2* p=new stiva2;
    p->val=x;
    p->next=aux;
    aux=p;
}

void determina_nr(int& nr,int& i) {
    while (sir[i]>=48 && sir[i]<=57) {
        nr=nr*10+sir[i]-48;
        ++i;
    }
}