Cod sursa(job #2396802)

Utilizator Briana_NeaguNeagu Briana Briana_Neagu Data 3 aprilie 2019 20:34:54
Problema Evaluarea unei expresii Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.11 kb
#include <iostream>
#include <fstream>
#define maxim 100090
#include <stack>
#include <cstring>

using namespace std;

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


char v[maxim],x,k;
stack <char> st;
stack <long long> stiva;
int j,p;
  char s[100090];

int main()
{

    s[0]='(';
    char x;
    int i=1;
    while (f>>x)
    {
        s[i++]=x;
    }
    s[i]=')';
    s[i+1]=NULL;


    int l=strlen(s)-1;
    for ( i=0; i<=l; i++)
    {
        if (s[i]>='0' && s[i]<='9')
        {
            x=0;
            for ( j=i; s[j]>='0' && s[j]<= '9'; j++)
                // x=x*10+(s[j]-'0');
                v[++k]=s[j];
            i=j-1;
            v[++k]='.';
        }
        else if (s[i]=='-' || s[i]=='+')
        {
            while (!st.empty() &&(st.top()=='*' || st.top()=='/' ) )
            {
                v[++k]=st.top();
                st.pop();

            }
            st.push(s[i]);
        }
        else if (s[i]=='*' || s[i]=='/')
        {
            st.push(s[i]);
        }
        else if (s[i]=='(')
            st.push(s[i]);
        else
        {
            while (!st.empty() && st.top()!='(')
            {
                v[++k]=st.top();
                st.pop();

            }
            st.pop();
        }//cout<<s[i]<<" ";


    }
//      for (int i=1; i<=k; i++)
//        cout<<v[i]<<" ";
//      cout<<endl;


    for (int i=1; i<=k; i++)
    {
        if (v[i]>='0' && v[j]<='9')
        {
            p=0;
            while (v[i]!='.')
            {
                p=p*10+(v[i]-'0');
                i++;
            }

            stiva.push(p);
        }
        else
        {
            long long a,b;
            b=stiva.top();
            stiva.pop();
            a=stiva.top();

            stiva.pop();
            if (v[i]=='-')
                stiva.push(a-b);
            else if(v[i]=='+')
                stiva.push(a+b);
            else if (v[i]=='/')
                stiva.push(a/b);
            else stiva.push(a*b);

        }
    }
    g<<stiva.top();



}