Cod sursa(job #1168495)

Utilizator DastasIonescu Vlad Dastas Data 8 aprilie 2014 19:31:39
Problema Evaluarea unei expresii Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 4.39 kb
//Carausu Catrinel & Nagy Stefan
#include <iostream>
#include <string.h>
#include<fstream>
#include<cmath>
using namespace std;
ifstream f("evaluare.in");
ofstream g("evaluare.out");
const int maxn=100010;
char s[maxn];
long double rez;
int prio(char);

void elim_space(char expr[])
{
    char aux[maxn];
    for(int i=0;i<strlen(expr);i++)
        if(expr[i]==' ')
        {
            strcpy(aux, expr+i+1);
            strcpy(expr+i,"\0");
            strcat(expr,aux);
        }
}

void negativ(char expr[])
{
    char aux[maxn];
    if(expr[0]=='-')
    {
            strcpy(aux, expr);
            strcpy(expr,"(0");
            strcat(expr,aux);
            int i=3,k=0;
            while((expr[i]>='0'&&expr[i]<='9')||expr[i]=='.')
            {
                k++;
                i++;
            }

            strcpy(aux, expr+2+k+1);
            strcpy(expr+2+k+1,"\0");
            strcat(expr,")");
            strcat(expr,aux);
    }
    for(int i=1;i<strlen(expr)-1;i++)
        if(expr[i]=='('&&expr[i+1]=='-')
        {
            strcpy(aux, expr+i+1);
            strcpy(expr+i+1,"\0");
            strcat(expr,"0");
            strcat(expr,aux);
        }
}

int isoperator(char e)
{
    if(e=='+'||e=='-'||e=='*'||e=='/'||e=='%'||e=='^')
        return 1;
    return 0;
}
void forma_pol(char expr[], char fpol[])
{

    int k=0, p=0;
    char st[maxn];
    int ok=0;
    for(int i=0;i<strlen(expr);++i)
        if(expr[i]>='0'&&expr[i]<='9')
        {
            while((expr[i]>='0'&&expr[i]<='9')||expr[i]=='.')
                fpol[p++]=expr[i++];
            fpol[p++]=' ';
            i--;
        }
        else
            if(expr[i]=='(')
                st[k++]=expr[i];
            else
                if(expr[i]==')')
                {
                    while(k-1>=0)
                        if(st[k-1]!='(')
                        {
                            fpol[p++]=st[--k];
                            fpol[p++]=' ';
                        }
                        else
                            break;
                    --k;
                }


                else // am neaparat un operator
                {
                    while(k-1>=0)
                        if(prio(st[k-1])>=prio(expr[i]))
                        {
                            fpol[p++]=st[--k];
                            fpol[p++]=' ';
                        }
                        else
                            break;

                    st[k++]=expr[i];

                    if(expr[i]=='s')
                           i+=3;

                }


        while(k-1>=0)
        {
            fpol[p++]=st[--k];
            fpol[p++]=' ';
        }
        fpol[p]='\0';
}

int prio(char oper)
{
    if(oper=='+'||oper=='-')
        return 0;
    else
        if(oper=='*'||oper=='/')
            return 1;
        else
            if(oper=='^'||oper=='s')
                return 2;
    return -1;
}
long double operatie(long double a,long double b, char op)
{
    switch(op)
    {
        case '+': return a+b;
        case '-': return a-b;
        case '*': return a*b;
        case '/': return a/b;
        case '^': return pow(a,b);

    }
}
double st[maxn];
long double eval(char expr[])
{
    char fpol[maxn];
    forma_pol(expr, fpol);
    //la sf va contine
    //rez final
    int p=0;
    for(int k=0; fpol[k]; k++)
    {
        if(fpol[k]>='0'&&fpol[k]<='9')
        {
            //st[p++]=fpol[k]-'0';
            st[p]=0;
            while(fpol[k]>='0'&&fpol[k]<='9')
                st[p]=st[p]*10+(fpol[k++]-'0');
            int put=0;
            if(fpol[k]=='.')
            {
                k++;
                while(fpol[k]>='0'&&fpol[k]<='9')
                {
                    st[p]=st[p]*10+(fpol[k++]-'0');
                    put++;
                }
                st[p]/=(double)pow(10,put);
            }

            p++;
        }
        else
            if(isoperator(fpol[k]))
            {
                st[p-2]=operatie(st[p-2],st[p-1],fpol[k]);
                --p;
            }
            else
                if(fpol[k]=='s')

                    st[p-1]=sqrt(st[p-1]);

    }
    return st[0];
}

int main()
{
    f.get(s, maxn);
    elim_space(s);
    negativ(s);
    g<<eval(s);
    return 0;
}