c语言表达式求值 栈的应用 问题流程图,后缀表达式求值算法。栈的应用解决方案...

C/C++ code#include

#include

#define StackMaxSize 30

typedef int ElemType;

struct Stack

{

ElemType elem[StackMaxSize];

int top;

}s;

void Push(ElemType x)

{

if(s.top ==StackMaxSize - 1) //判断错误

printf("stack is full!\n");

else

s.elem[s.top++] = x; //

}

ElemType Pop()

{

ElemType e;

if(s.top == -1)

printf("stack is empty!\n");

else

e = s.elem[s.top--];

return e;

}

void ExprCompute(char *str)

{

char ch;

int i = 0, x;

ch = str[i++];

while(ch != '@')

{

switch(ch)

{

case '+':

x = Pop() + Pop();

break;

case '-':

x = Pop();

x = Pop() - x;

break;

case '*':

x = Pop() * Pop();

break;

case '/':

x = Pop();

if(x != 0)

x = Pop() / x;

else

{

printf("Divided by 0 !\n");

exit(1);

}

break;

default:

x = ch - 48; //

break;

while(str[i] >= '0' && str[i] <= '9')

{

x = x * 10 + str[i] - 48; //

i++;

}

}

Push(x);

ch = str[i++];

}

if(s.top > -1)

{

x = Pop();

if(s.top == -1)

return ;

else

{

printf("expression error !\n");

exit(1);

}

}

else

{

printf("Stack is empty!\n");

exit(1);

}

}

void main()

{

s.top = -1;

char str[20];

printf("please input a string ended with a @:");

gets(str);

ExprCompute(str);

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值