Cod sursa(job #491111)

Utilizator szabibibiOrban Szabolcs szabibibi Data 9 octombrie 2010 18:49:52
Problema Evaluarea unei expresii Scor 30
Compilator c Status done
Runda Arhiva educationala Marime 4.02 kb
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX 100

typedef struct stack
{
    char op[MAX];
    long top;
} stack;

typedef struct stack2
{
    long nums[1000];
    long top;
} stack2;

typedef char expr[200000];

stack S;
expr infix;
expr postfix;
long ered;


void empty(stack *S);
int isempty(stack *S);
char pop(stack *S);
void push(stack *S, char op);
void empty2(stack2 *S);
int isempty2(stack2 *S);
long pop2(stack2 *S);
void push2(stack2 *S, long num);

void intopost(char* in, char* post);
void eval(char* post, long* ered);
int priority(char);
int isop(char);
long muvel(long a, long b, char op);


int main()
{
    freopen("evaluare.in","r",stdin);
    freopen("evaluare.out","w",stdout);
    gets(infix);
    infix[strlen(infix)] = '\0';
    intopost(&infix[0],&postfix[0]);
    eval(&postfix[0],&ered);
    printf("%ld", ered);
    return 0;
}

void intopost(char *in, char* post)
{
    char *i = &in[0], *p = &post[0];
    char n1 = ' ';
    empty(&S);
    while (*i != '\0')
    {
        if (isdigit(*i))
        {
            while (isdigit(*i))
            {
                *p = *i;
                ++i;
                ++p;
            }
            *(p++) = ' ';
        }
        if (isop(*i))
        {
            if (isempty(&S))
            {
                push(&S, *i);
                ++i;
            }
            else
            {
                n1 = pop(&S);
                while (priority(n1) > priority(*i))
                {
                    *p = n1;
                    ++p;
                    *p = ' ';
                    ++p;
                    n1 = pop(&S);
                }
                push(&S, n1);
                push(&S, *i);
                ++i;
            }
        }
        if (*i == '(')
        {
            push(&S, *i);
            ++i;
        }
        if (*i == ')')
        {
            while((n1 = pop(&S)) != '(')
            {
                *p = n1;
                *(++p)= ' ';
                p++;
            }
            ++i;
        }
    }
    while (!isempty(&S))
    {
        *p = pop(&S);
        *(++p) = ' ';
        p++;
    }
    *p = '\0';
}

void empty(stack *S)
{
    S->top = -1;
}

int isempty(stack *S)
{
    return (S->top >= 0) ? (0) : (1);
}

void push(stack *S, char op)
{
    ++S->top;
    S->op[S->top] = op;
}

char pop(stack *S)
{
    --S->top;
    return S->op[S->top+1];
}
void empty2(stack2 *P)
{
    P->top = -1;
}

int isempty2(stack2 *P)
{
    return (P->top >= 0) ? (0) : (1);
}

void push2(stack2 *P, long num)
{
    ++P->top;
    P->nums[P->top] = num;
}

long pop2(stack2 *P)
{
    --P->top;
    return P->nums[P->top+1];
}

int priority(char op)
{
    if ((op == '+') || (op == '+'))
        return 1;
    else if ((op == '/') || (op == '*'))
        return 2;
    else return 0;
}

int isop(char op)
{
    if ((op == '/') || (op == '-') || (op == '+') || (op == '*'))
        return 1;
    else return 0;
}

void eval(char* postfix, long* ered)
{
    stack2 P;
    empty2(&P);
    char *i = &postfix[0];
    long szam = 0;
    long a,b;

    while (*i != '\0')
    {
        if (isdigit(*i))
        {
            szam = 0;
            while (isdigit(*i))
            {
                szam =szam * 10 + (int)(*i - '0');
                ++i;
            }
            push2(&P, szam);
        }
        if (isop(*i))
        {
            a = pop2(&P);
            b = pop2(&P);
            push2(&P, muvel(b,a,*i));
            ++i;
        }
        if (*i == ' ')
            ++i;
    }

    *ered = pop2(&P);
}

long muvel(long a, long b, char op)
{
    switch (op)
    {
        case '+' :
            return a+b;
            break;
        case '-' :
            return a-b;
            break;
        case '*' :
            return a*b;
            break;
        case '/' :
            return a/b;
            break;
    }
}