#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));
}
중위표현식 계산하는건데 오류떠요ㅠㅠ제발요
남의 코드 읽는거 솔직히 귀찮은데 에러 메시지 라인이라도 좀 마킹해주고 그래라 엉?
컴파일 에러가 아니라 결과값이 에러가나서 저도 뭐가 문젠지를 모르겠어요ㅠㅠ 진짜 죄송해요...
디버그 포인트 찍고 계속 눌러가면서 비교
터미널에서도 그게 되나요?