#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include "eval_i.h"



#define operand 0

#define operator 1



void opandpush(opandstack *st, int n){

if(st->top == 99){

printf("Error : Operand overflow");

printf("\n");

exit(1);

}

st->top++;

st->item[st->top] = n;

}



int opandpop (opandstack *st) {

int n;

if (st->top == -1) {

printf("Error : Operand stack is empty.");

printf("\n");

exit(1);

}

else{

return (st->item[st->top]--);

}

}



void operpush(operstack *st, int n) {

if (st->top == 99) {

printf("Error : Operator overflow");

printf("\n");

exit(1);

}



st->top++;

st->item[st->top] = n;

}



char operpop(operstack *st) {

int n;

if (st->top == -1) {

printf("Error : Operator stack is empty.");

printf("\n");

exit(1);

}



n = st->item[st->top];

st->top--;

return n;

}


int eval2(char oper, int n1, int n2) {

switch(oper) {

case '+':

return (n1 + n2);

case '-':

return (n1 - n2);

case'*':

return (n1 * n2);

case '/':

return (n1 / n2);

case '%':

return (n1 % n2);

default :

printf("Error : Operator-1\n");

exit(1);

}

}



int checktype_i(char c) {

switch(c) {

case '+':

case '-':

case '*':

case '/':

case '%': return operator;

default: return operand;

}

}



int priority(char c) {

switch(c) {

case '@' : return 0;

case '+':

case '-': return 1;

case '*':

case '/':

case '%': return 2;

case '(': return 3;

default : printf("Error : Operator-2\n");

exit(1);

}

}



int topstack_i(opandstack *st){

return st->item[0];

}



void eval_i(char *expr) {

int opand1, opand2, val;

char c, c2;

int l = strlen(expr);



opandstack opand;

operstack oper;



opand.top = -1;

oper.top = -1;



operpush(&oper, '@');



for(int i = 0; i < l; i++) {

if(expr[i] >= '0' && expr[i] <= '9') {

opandpush(&opand,(int)(expr[i] - '0'));

}

else if (checktype_i(expr[i])) {

c2 = operpop(&oper);



if(priority(expr[i]) > priority(c2)) {

operpush(&oper, c2);

operpush(&oper, expr[i]);

}

else

while(priority(expr[i]) <= priority(c2)) {

opand2 = opandpop(&opand);

opand1 = opandpop(&opand);

val = eval2(c2, opand1, opand2);

c2 = operpop(&oper);

}

operpush(&oper, c2);

operpush(&oper, expr[i]);

}

}

while (c = operpop(&oper) != '@') {

opand2 = opandpop(&opand);

opand1 = opandpop(&opand);

val = eval2(c2, opand1, opand2);

opandpush(&opand, val);

}

printf("Result of evaluation : %d\n", opandpop(&opand));

}
중위표현식 계산하는건데 오류떠요ㅠㅠ제발요