中缀表达式转为后缀表达式:
1.遇到操作数:直接输出(添加到后缀表达式中)
2.栈为空时,遇到运算符,直接入栈
3.遇到左括号:将其入栈
4.遇到右括号:执行出栈操作,并将出栈的元素输出,直到弹出栈的是左括号,左括号不输出。
5.遇到其他运算符:加减乘除:弹出所有优先级大于或者等于该运算符的栈顶元素,然后将该运算符入栈
6.最终将栈中的元素依次出栈,输出。
计算后缀表达式:
1.如果是操作数,则放入栈中
2.如果是操作符,则取出栈中两个操作数,进行运算后,将结果放入栈中;
3.直到最后栈中只有一个元素,此元素就是计算结果;
C语言实现:
stack.h
#ifndef _STACK_H
#define _STACK_H
struct StackRecord;
typedef struct StackRecord *Stack;
typedef int ElementType;
int IsEmpty(Stack S);
int IsFull(Stack S);
Stack CreateStack(int MaxElements);
void Free_Stack(Stack S);
void MakeEmpty(Stack S);
void Push(ElementType X, Stack S);
ElementType Top(Stack S);
void Pop(Stack S);
ElementType TopAndPop(Stack S);
#endif // _STACK_H
stack.c
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
#define EmptyTOS (-1)
#define MinStackSize (1)
struct StackRecord
{
int Capacity;
int TopOfStack;
ElementType *Array;
};
Stack CreateStack(int MaxElements)
{
Stack S;
if(MaxElements < MinStackSize)
printf("Stack Size is too small!\n");
S = malloc(sizeof(struct StackRecord));
if(S == NULL)
printf("Out of Space, malloc Stack S failed\n");
S->Array = malloc(sizeof(ElementType)*MaxElements);
if(S->Array == NULL)
printf("Out of Space, malloc S->Array failed\n");
S->Capacity = MaxElements;
return S;
}
void Free_Stack(Stack S)
{
if(S != NULL)
{
free(S->Array);
free(S);
}
}
int IsEmpty(Stack S)
{
return S->TopOfStack == EmptyTOS;
}
int IsFull(Stack S)
{
return S->TopOfStack == S->Capacity - 1;
}
void MakeEmpty(Stack S)
{
S->TopOfStack = EmptyTOS;
}
void Push(ElementType X, Stack S)
{
if(IsFull(S))
printf("Stack S is full\n");
else
S->Array[++S->TopOfStack] = X;
}
ElementType Top(Stack S)
{
if(IsEmpty(S))
{
printf("Stack S is Empty\n");
return -1;
}
else
return S->Array[S->TopOfStack];
}
void Pop(Stack S)
{
if(IsEmpty(S))
printf("Stack S is Empty\n");
else
S->TopOfStack--;
}
ElementType TopAndPop(Stack S)
{
if(IsEmpty(S))
{
printf("Stack S is Empty\n");
return -1;
}
else
return S->Array[S->TopOfStack--];
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "stack.h"
int IsOperator(char ch)
{
char ops[] = "+-*/";
int i;
for (i = 0; i < 4; i++)
{
if (ch == ops[i])
return 1;
}
return 0;
}
int Precedence(char op)
{
if (op == '(')
{
return -1;
}
else if(op == '*' || op == '/')
return 1;
else if(op == '+' || op == '-')
return 0;
}
void InFixtoPostFix(char *InFix, char *PostFix)
{
int length = 0;
int i = 0;
int j = 0;
length = strlen(InFix);
Stack Experssion = CreateStack(length);
MakeEmpty(Experssion);
for(i = 0; i < length; i++)
{
if(InFix[i] == '(')
Push(InFix[i],Experssion);
else if(InFix[i] == ')')
{
while(Top(Experssion) != '(')
PostFix[j++] = TopAndPop(Experssion);
Pop(Experssion);
}
else
{
if(!IsOperator(InFix[i]))
PostFix[j++] = InFix[i];
else
{
while(IsEmpty(Experssion) == 0 && (Precedence(Top(Experssion))>=Precedence(InFix[i])))
PostFix[j++] = TopAndPop(Experssion);
Push(InFix[i],Experssion);
}
}
}
while(IsEmpty(Experssion) == 0)
PostFix[j++] = TopAndPop(Experssion);
Free_Stack(Experssion);
PostFix[j] = 0;
}
int Calcute(char *PostFix)
{
int length = strlen(PostFix);
int i = 0;
int tmp_var = 0;
int result_var = 0;
Stack Calcute_stack = CreateStack(length);
MakeEmpty(Calcute_stack);
for(i = 0; i < length; i++)
{
if(IsOperator(PostFix[i]))
{
switch(PostFix[i])
{
case '+':
tmp_var = TopAndPop(Calcute_stack) + TopAndPop(Calcute_stack);
break;
case '-':
tmp_var = TopAndPop(Calcute_stack) - TopAndPop(Calcute_stack);
break;
case '*':
tmp_var = TopAndPop(Calcute_stack) * TopAndPop(Calcute_stack);
break;
case '/':
tmp_var = TopAndPop(Calcute_stack) / TopAndPop(Calcute_stack);
break;
}
Push(tmp_var,Calcute_stack);
}
else
{
Push(PostFix[i] - '0',Calcute_stack);
}
}
result_var = Top(Calcute_stack);
Free_Stack(Calcute_stack);
return result_var;
}
int main()
{
char InFix[100];
char PostFix[100];
int Result = 0;
while(1)
{
printf("please enter an expression: ");
gets(InFix);
printf("Infix is : %s \n",InFix);
InFixtoPostFix(InFix,PostFix);
printf("PostFix is : %s \n",PostFix);
Result = Calcute(PostFix);
printf("calculation is : %d \n",Result);
}
return 0;
}