zhong2hou.h
/**
* 这个程序是中缀表达式到后缀表达式的转换
* 使用栈的数据结构开实现
*/
#ifndef ZHONG2HOU_H
#define ZHONG2HOU_H
#define elementType char
struct my_stack_in_array
{
int capacity;
int topOfStack;
elementType *Array;
};
typedef struct my_stack_in_array myStack;
typedef struct my_stack_in_array * Stack;
typedef Stack Node;
void fatalError(const char * );
int isEmpty(Stack stack);
int isFull(Stack stack);
Stack createStack(int capacity);
void disposeStack(Stack stack);
void makeEmpty(Stack stack);
void push(Stack stack, elementType X);
void pop(Stack stack);
int isPri(char a, char b);
int isOper(char c);
int isOperNum(char c);
void displayStack(Stack stack);
elementType top(Stack stack);
elementType topAndpop(Stack);
const elementType * zhong2hou(const elementType * zhong);
#define emptyTOS (-1)
#define minStackSize (0)
#endif
zhong2hou.c
#include "zhong2hou.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
void fatelError(const char *outprint)
{
printf(outprint);
fflush(stdout);
exit(EXIT_FAILURE);
}
int isEmpty(Stack stack)
{
return stack->topOfStack == emptyTOS;
}
int isFull(Stack stack)
{
return stack->capacity == stack->topOfStack;
}
Stack createStack(int capacity)
{
Stack stack = (Stack)malloc(sizeof(Stack));
if (stack == NULL)
fatelError("can not malloc stack!\n");
stack->capacity = capacity;
stack->topOfStack = emptyTOS;
stack->Array = (elementType *)malloc(sizeof(elementType) * stack->capacity);
return stack;
}
void disposeStack(Stack stack)
{
free(stack);
}
void makeEmpty(Stack stack)
{
stack->topOfStack = emptyTOS;
}
void push(Stack stack, elementType X)
{
if (isFull(stack))
fatelError("stack is full \n");
else
stack->Array[++(stack->topOfStack)] = X;
}
void pop(Stack stack)
{
if (isEmpty(stack))
fatelError("pop:stack is empty \n");
else
stack->topOfStack--;
}
elementType top(Stack stack)
{
if (isEmpty(stack))
{
fatelError("top:stack is empty \n");
return -1;
}
else
return stack->Array[stack->topOfStack];
}
elementType topAndpop(Stack stack)
{
if (isEmpty(stack))
{
fatelError("topAndpop:stack is empty \n");
return -1;
}
return stack->Array[stack->topOfStack--];
}
int isOperNum(char c)
{
if ((c >= 48 && c <= 57) || (c >= 65 && c <= 90) || (c >= 97 && c <= 122))
return 1;
else
return 0;
}
int isOper(char c)
{
return (c == '+' || c == '*' || c == '(' || c == ')' || c == '/' || c == '-');
}
int priorty(char c)
{
switch (c)
{
case '+':
{
return 0;
break;
}
case '-':
{
return 0;
break;
}
case '*':
{
return 1;
break;
}
case '/':
{
return 1;
break;
}
case '(':
{
return 2;
break;
}
case ')':
{
return 2;
break;
}
default:
{
fatelError("char is not operator\n");
return 0;
break;
}
}
}
int isPri(char a, char b)
{
if (isOper(a) && isOper(b))
{
return priorty(a) >= priorty(b);
}
else
{
fatelError("input args are not operators\n");
return 0;
}
}
void displayStack(Stack stack)
{
printf("==== stack is: ====\n");
Stack tempStack = createStack(stack->capacity);
while (!isEmpty(stack))
{
char c = topAndpop(stack);
putchar(c);
push(tempStack, c);
}
putchar('\n');
fflush(stdout);
while (!isEmpty(tempStack))
{
char c = topAndpop(tempStack);
push(stack, c);
}
free(tempStack);
printf("===================\n");
}
// 中缀表达式到后缀表达式的转换
const elementType *zhong2hou(const elementType *zhong)
{
// 获得中缀表达式的长度
int sizeofzhong = 0;
char c = *zhong;
while (c != '\n')
{
sizeofzhong++;
c = *(zhong + sizeofzhong);
}
// printf("the size of zhong is %d \n", sizeofzhong);
assert(sizeofzhong > 0);
// 进行转换
char *hou = (char *)malloc(sizeof(char) * sizeofzhong);
if (hou == NULL)
fatelError("malloc of hou is error!!\n");
int index = 0;
// 构建一个栈结构
Stack stack = createStack(100);
for (int i = 0; i < sizeofzhong; i++)
{
// printf("\n对 %c 进行操作:\n", zhong[i]);
// if (i == 8)
// getchar();
// 算法核心
if (isOperNum(zhong[i]))
{
putchar(zhong[i]);
fflush(stdout);
*(hou + index++) = zhong[i];
}
else if (isOper(zhong[i]))
{
// 如果栈空,则入栈
if (isEmpty(stack))
{
push(stack, zhong[i]);
// displayStack(stack);
}
else
{
/* 如果栈非空,如果这个符号是')',那么弹出直到 '(' 的所有操作符*/
if (zhong[i] == ')')
{
char c = topAndpop(stack);
while (!isEmpty(stack) && c != '(')
{
putchar(c);
fflush(stdout);
*(hou + index++) = c;
c = topAndpop(stack);
}
}
/* 如果栈非空,符号不是')',且其中有优先级更高的操作符( 不能是符号 '(' ),则弹出所有优先级大于等于此操作符的操作符号,然后入棧*/
else if (top(stack) != '(')
{
char c = top(stack);
while (isPri(c, zhong[i]) )
{
putchar(c);
topAndpop(stack);
fflush(stdout);
*(hou + index++) = c;
if (isEmpty(stack) || top(stack) == '(')
break;
c = top(stack);
}
// 入栈
push(stack, zhong[i]);
// displayStack(stack);
}
else if (top(stack) == '(')
{
// 入栈
push(stack, zhong[i]);
// displayStack(stack);
}
}
}
else
{
fatelError("wrong char \n");
}
}
// 如果栈非空,且是最后一个操作符,则弹出所有的操作符
while (!isEmpty(stack))
{
char c = topAndpop(stack);
putchar(c);
fflush(stdout);
*(hou + index++) = c;
}
return hou;
}
zhong2houTest.c
#include "zhong2hou.c"
int main(void)
{
// 构造一个中缀表达式
const char zhong[] = "a+b*c+(d*e+f)*g\n";
printf(zhong);
putchar('\n');
// 中缀表达式转为后缀表达式
const char * hou = zhong2hou(zhong);
printf("\nhou is :");
printf(hou);
putchar('\n');
}