Untitled
raw download clone
C
views 32
,
size 4511 b
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct Stack{
    int top;
    char* S;
    int* s; 
}stack;

int plus(int a,int b){
    return a+b;
}

int subtract(int a,int b){
    return a-b;
}

int min(int a,int b){
    return a<b?a:b;
}

int max(int a,int b){
    return a>b?a:b;
}

int C(int a,int b){
    return a<b?(a-b):(a+b);
}

int precedence(char k){
    if(k == 'c'){
        return 3;
    }
    else if(k == 'M' || k == 'm'){
        return 2;   
    }
    else if(k == '-' || k == '+'){
        return 1;
    }else if(k == '(')
    {
        return -1;
    }
    else if(k == ')')
    {
        return 0;
    }else
    {
        return -2;
    }
}

stack* create_stack(){
    stack* new = (stack*)malloc(sizeof(stack));

    new->S = (char*)malloc(1000*sizeof(char));

    new->s = (int*)malloc(1000*sizeof(int));

    new->top = -1;

    return new;
}

int isEmpty(stack* a){
    return a->top<0?1:0;
}

void pop(stack* a){
    if(!isEmpty(a)){
        a->top--;
    }
}

void pushc(stack* a,char n){
    ++(a->top);
    a->S[a->top] = n; 
}

void pushi(stack* a,int n){
    ++(a->top);
    a->s[a->top] = n;
}

int topi(stack* a){
    return a->s[a->top];
}

char topc(stack* a){
    return a->S[a->top];
}

void converse(char* in,char* post){
    
    stack* conv = create_stack();
    int len = strlen(in);
    int i;
    int k=0;
    
    for (i = 0; i < len; i++)
    {
        int prec = precedence(in[i]);
     
        if (prec != -2)
        {
            if (isEmpty(conv))
            {
                pushc(conv,in[i]);   
            }else
            {
                if (prec == 3)
                {
                    pushc(conv,in[i]);   
                }
                else if (prec > 0 && prec > precedence(conv->S[conv->top]))
                {
                    pushc(conv,in[i]);   
                }
                else if (prec > 0 && prec <= precedence(conv->S[conv->top]))
                {
                    while ( !isEmpty(conv) && prec <= precedence(conv->S[conv->top]))
                    {
                        post[k++] = topc(conv);
                        pop(conv);
                    }
                    pushc(conv,in[i]);
                }else if(prec == -1)
                {
                    pushc(conv,in[i]);
                }else if(prec == 0)
                {
                    while (precedence(conv->S[conv->top]) != -1)
                    {
                        post[k++] = topc(conv);
                        pop(conv);
                    }
                    pop(conv);
                }
            }
        }else
        {
            post[k++] = in[i];
        }
    }

    while (!isEmpty(conv))
    {
        post[k++] = topc(conv);
        pop(conv);
    }
    post[k] = '\0';
    free(conv);
}

int evaluate(char* s){

    stack* evaluation = create_stack();
    int len = strlen(s);
    int ans = 0;
    
    for (int i = 0; i < len; i++)
    {
        int k = s[i]-48;

        if (k<0 || k>9)
        {
            int a;
            int b;

            b = topi(evaluation);
            pop(evaluation);
            a = topi(evaluation);
            pop(evaluation);

            switch (s[i])
            {
            case '+' :
                pushi(evaluation,plus(a,b));
                break;

            case '-' :
                pushi(evaluation,subtract(a,b));                
                break;

            case 'c' :
                pushi(evaluation,C(a,b));                
                break;

            case 'M' :
                pushi(evaluation,max(a,b));                                
                break;

            case 'm' :
                pushi(evaluation,min(a,b));                
                break;
            default:
                break;
            }
        }else
        {
            pushi(evaluation,k);   
        }
    }
    ans = topi(evaluation);
    free(evaluation);
    return ans;
}

int main(int argc, char const *argv[])
{
    int n;

    scanf("%d",&n);

    char inp[1000];

    char post[1000]; 

    for (int i = 0; i < n; i++)
    {
        scanf("%s",inp);

        converse(inp,post);

        //printf("%s\n",post);

        printf("%d\n",evaluate(post));
    }
    
    return 0;
}
close fullscreen
Login or Register to edit or fork this paste. It's free.