#include < stdio.h >
#include < stdlib.h >
#define MAX 100
int stack[MAX];
int stack_top;
void init_stack(void)
{
stack_top = -1;
}
int push(int t)
{
if (stack_top >= MAX - 1)
{
printf("\nStack overflow !!!");
exit(1);
}
stack[++stack_top] = t;
return t;
}
int pop(void) /* Pop function */
{
if (stack_top < 0)
{
printf("\nStack underflow !!!"); /* Underflow error control part */
exit(1);
}
return stack[stack_top--];
}
int get_stack_top(void) /* Getting the stack's top value */
{
return (stack_top < 0) ? -1 : stack[stack_top];
}
int is_stack_empty(void) /* Checking the stack is empty or not */
{
return (stack_top < 0);
}
int is_operator(int op) /* Is this an operator ? */
{
return (op == '+' || op == '-' || op == '*' || op == '/');
}
int precedence(int op) /* Check the precedence between operators */
{
if (op == '(') return 0;
if (op == '+' || op == '-') return 1;
if (op == '*' || op == '/') return 2;
else return 3;
}
int is_legal(char *postfix) /* Check the postfix expression... legal? illegal? */
{
int result = 0;
while (*postfix)
{
while (*postfix == ' ') /* Get rid of space */
postfix++;
if (is_operator(*postfix))
result--;
else
{
result++;
while (*postfix != ' ')
postfix++;
}
if (result < 1) break;
postfix++;
}
return (result == 1);
}
void postfix(char *dst, char *src) /* Change the infix expression to the postfix expression */
{
char c;
init_stack();
while (*src)
{
if (*src == '(') /* push '(' */
{
push(*src);
src++;
}
else if (*src == ')') /* If meet ')', pop until meet '('. and then throw '(' */
{
while (get_stack_top() != '(')
{
*dst++ = pop();
*dst++ = ' ';
}
pop();
src++;
}
else if (is_operator(*src)) /* If it is an operator... */
{
while (!is_stack_empty() && precedence(get_stack_top()) >= precedence(*src))
{
*dst++ = pop();
*dst++ = ' ';
}
push(*src);
src++;
}
else if (*src >= '0' && *src <= '9') /* If it is an operand... */
{
do
{
*dst++ = *src++;
} while (*src >= '0' && *src <= '9');
*dst++ = ' ';
}
else
src++;
}
while (!is_stack_empty()) /* Pop rest of them in the stack */
{
*dst++ = pop();
*dst++ = ' ';
}
dst--;
*dst = 0;
}
int eval(char *post) /* Calculation postfix expression */
{
int i;
init_stack();
while (*post)
{
if (*post >= '0' && *post <= '9') /* Push the operands */
{
i = 0;
do
{
i = i * 10 + *post - '0';
post++;
} while (*post >= '0' && *post <= '9');
push(i);
}
else if (*post == '+') /* If it is an operator, pop two times and push it */
{
push(pop() + pop());
post++;
}
else if (*post == '*')
{
push(pop() * pop());
post++;
}
else if (*post == '-')
{
i = pop();
push(pop() - i);
post++;
}
else if (*post == '/')
{
i = pop();
push(pop() / i);
post++;
}
else
post++;
}
return pop(); /* Last one in the stack! That is an answer of postfix expression */
}
int main()
{
FILE * fp1 = fopen("hw5.inp","r");
FILE * fp2 = fopen("hw5.inp","r");
FILE * fp3 = fopen("hw5.out","w");
int result;
char exp[100], infix[100];
while(1) /* infinite loop */
{
printf("\n\nInput infix expression\n");
printf("==> ");
if(scanf("%s", &infix)==EOF) /* If press ^d, scanf returns EOF. We can make it exit algorithm */
{
printf("\n");
}
postfix(exp, infix); /* Call postfix function */
printf("\n%s", exp);
if (!is_legal(exp)) /* Check the postfix expression */
{
printf("\n Expression is not legal!\n\n");
continue;
}
result = eval(exp); // 결과값
printf("\n%d\n\n", result);
printf("\n" "*");
}
return 0;
}
이렇게 까진 다 짯는데.
txt파일에서 저렇게 나올려면 어떻게 짜야하는지 궁금합니다..