#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_STACK_SIZE 100
#pragma warning(disable: 4996)
typedef char element;
typedef struct
{
int top;
int capacity;
element* data;
} StackType;
void init_stack(StackType* s)
{
s -> top = -1;
s -> capacity = 1;
s->data = (element*)malloc(s->capacity * sizeof(element));
}
int is_empty(StackType* s)
{
return (s->top == -1);
}
int is_full(StackType* s)
{
return(s->top == (MAX_STACK_SIZE - 1));
}
void push(StackType* s, element item)
{
if (is_full(s))
{
s->capacity *= 2;
s->data = (element*)realloc(s->data , s->capacity * sizeof(element));
}
else s->data[++(s->top)] = item;
}
element pop(StackType* s)
{
if (is_empty(s))
{
fprintf(stderr, "스택 공백 에러\n");
exit(1);
}
else return s->data[(s->top)--];
}
element peek(StackType* s)
{
if (is_empty(s))
{
fprintf(stderr, "스택 공백 에러\n");
exit(1);
}
else return s->data[s->top];
}
void delete(StackType* s)
{
free(s);
}
// 스택 함수
int prec(char op)
{
switch (op)
{
case '(': case ')':return 0;
case '+': case '-':return 1;
case '*': case '/':return 2;
}
return -1;
}
void infix_to_postfix(char exp[])
{
int i = 0;
char ch, top_op;
int len = strlen(exp);
StackType s;
init_stack(&s);
for (int i = 0; i < len; i++)
{
ch = exp[i];
switch (ch)
{
case '+': case '-':case '*':case'/':
while (!is_empty(&s) && (prec(ch) <= prec(peek(&s))))
printf("%c", pop(&s));
push(&s, ch);
break;
case'(':
push(&s, ch);
break;
case')':
top_op = pop(&s);
while (top_op != '(')
{
printf("%c", top_op);
top_op = pop(&s);
}
break;
default:
printf("%c", ch);
break;
}
}
while (!is_empty(&s))
printf("%c", pop(&s));
}
int main(void)
{
char *infix = 0;
printf("중위표기식을 입력하세요");
scanf("%s", infix);
infix_to_postfix(infix);
delete;
}
형님들 여기서 계속 _CRT_INTERNAL_LOCAL_SCANF_OPTIONS,
댓글 0