Cod sursa(job #2376158)

Utilizator KaruschyAlex Caraman Karuschy Data 8 martie 2019 13:52:27
Problema Evaluarea unei expresii Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.34 kb
#include <iostream>
#include <cstring>
#include <fstream>
#include <stdlib.h>
using namespace std;
ifstream f("date.in");
ofstream g("date.out");
char s,fp[100][100],st1[100];
int st2[100],n,k,j,x;
int ierarhie(char s)
{
    if(s=='*' || s=='/')
        return 2;
    return 1;
}
int main()
{
    n=1;
    ////////////////creez forma poloneza
    while(f>>s)
    {
        if(isdigit(s))
        {
            fp[n][j]=s;
            j++;
        }
        else
        {
            n++;
            j=0;
            if(s=='(')
            {
                k++;
                st1[k]=s;
            }
            else if(s==')')
            {
                while(st1[k]!='(')
                {
                    n++;
                    fp[n][0]=st1[k];
                    k--;
                }
                k--;
            }
            else if (s=='+' || s=='*' || s=='-' || s=='/' )
            {
                while((ierarhie(st1[k])>=ierarhie(s)) && (st1[k]=='+' ||st1[k]=='-' || st1[k]=='*' || st1[k]=='/'))
                {
                    n++;
                    fp[n][0]=st1[k];
                    k--;
                }
                k++;
                st1[k]=s;
            }
        }
    }
    for(int i=k; i>0; i--)
    {
        n++;
        fp[n][0]=st1[i];
    }

////////////////////////////interpretarea  formei poloneze
    for(int i=1; i<=n; i++)
    {
        cout<<fp[i];
        if(isdigit(fp[i][0]))
        {
            x++;
            st2[x]=atoi(fp[i]);

        }
        else
        {
            if(fp[i][0]=='+')
            {

                int a=st2[x-1];
                int b=st2[x];
                x--;
                st2[x]=a+b;

            }
            if(fp[i][0]=='-')
            {

                int a=st2[x-1];
                int b=st2[x];
                x--;
                st2[x]=a-b;

            }
            if(fp[i][0]=='*')
            {

                int a=st2[x-1];
                int b=st2[x];
                x--;
                st2[x]=a*b;

            }
            if(fp[i][0]=='/')
            {

                int a=st2[x-1];
                int b=st2[x];
                x--;
                st2[x]=a/b;
            }
        }
    }
    g<<st2[x]<<" "<<x;
    return 0;
}