c语言表达式栈,后缀表达式的栈实现(C语言)

中缀表达式转为后缀表达式:

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

#include

#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

#include

#include

#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;

}

中缀表达式转为后缀表达式的结果

8802746d8d1758ee04bef8c6968232be.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值