用c语言定义字符栈,用c语言中的栈实现表达式求值

#include

#include

#include

#include

#include

#define TURUE 1

#define FALSE 0

#define Stack_Size 50

/*顺序栈整形*/

typedef struct{

char elem[Stack_Size];

int top;

}nStack;

//初始化

void nInitStack(nStack*S)

{

S->top=-1;

}

//判满

int nIsFull(nStack*S)

{

return(S->top==Stack_Size-1?TURUE:FALSE);

}

//判空

int nIsEmpty(nStack*S)

{

return(S->top==-1?TURUE:FALSE);

}

//进栈

int nPush(nStack*S,int x)

{

if(S->top==Stack_Size-1)return(FALSE);

S->top++;

S->elem[S->top]=x;

return(TURUE);

}

//出栈

int nPop(nStack*S,int*x)

{

if(S->top==-1)return(FALSE);

else{

*x=S->elem[S->top];

S->top--;

return(TURUE);

}

}

//取栈顶

int nGetTop(nStack*S)

{

int *x;

if(S->top==-1)return(FALSE);

else{

*x=S->elem[S->top];

return *x;

}

}

/*顺序栈_字符型*/

typedef struct{

char elem[Stack_Size];

int top;

}strStack;

/*初始化*/

void strInitStack(strStack *S)

{

S->top=-1;

}

/*判空*/

int strIsEmpty(strStack *S)

{

return(S->top==-1?TURUE:FALSE);

}

//判满

int strIsFull(strStack*S)

{

return(S->top==Stack_Size-1?TURUE:FALSE);

}

//进栈

char strPush(strStack*S,char x)

{

if(S->top==Stack_Size-1)return(FALSE);

S->top++;

S->elem[S->top]=x;

return(TURUE);

}

//出栈

char strPop(strStack*S,char*x)

{

if(S->top==-1)return(FALSE);

else{

*x=S->elem[S->top];

S->top--;

return(TURUE);

}

}

//取栈顶

char strGetTop(strStack*S,char*x)

{

if(S->top==-1)return(FALSE);

else{

*x=S->elem[S->top];

return(TURUE);

}

}

/*.........功能函数.........*/

//判断两个括号是否匹配

int Match(char ch,char str)

{

if(ch=='('&&str==')')return(TURUE);

else if(ch=='['&&str==']')return(TURUE);

else if(ch=='{'&&str=='}')return(TURUE);

else return(FALSE);

}

//判断是操作数还是操作符

int In(char ch)

{

if(ch=='+'){return(TURUE);}

else if(ch=='-'){return(TURUE);}

else if(ch=='*'){return(TURUE);}

else if(ch=='/'){return(TURUE);}

else if(ch=='('){return(TURUE);}

else if(ch==')'){return(TURUE);}

else if(ch=='#'){return(TURUE);}

else return(FALSE);

}

char Compare(char the,char c)//优先级

{

if (the == '+' || the == '-'){

if (c == '+' || c == '-' || c == ')' || c == '#')

return('>');

else return('

}

if (the == '*' || the == '/'){

if (c == '(')return('

else return('>');

}

if (the == '('){

if (c == ')')return('=');

else return('>');

}

if (the == '#'){

if (c == '#')return('=');

else return('

}

}

int Execute(int a,char op,int b)

{

switch(op){

case '+':

return(a+b);break;

case '-':

return(a-b);break;

case '*':

return(a*b);break;

case '/':

return(a/b);break;

}

}

int ExpEvaluation()

{

int i,a,b,v,str[20];

char ch,op;

nStack OPND;//运算数栈

strStack OPTR;//运算符栈

nInitStack(&OPND);

strInitStack(&OPTR);

strPush(&OPTR, '#');

printf("\n\nPlease input an expression(Ending with #): ");

ch = getchar();//取一个字符送入ch

while (ch != '#'||strGetTop(&OPTR,&ch)!='#'){

if (!In(ch)){

if (ch >= '0'&&ch

ch=ch*10+(str[i]-48);

i++;

nPush(&OPND, ch);

}

}

else if(ch=='(')nPush(&OPND,ch);

else if(ch==')')while(strGetTop(&OPTR,&ch)!='('){

strPop(&OPTR,&ch);

}

else{//ch是操作符

switch (Compare(strGetTop(&OPTR, &ch),ch){

case '=':

case '

case '>':nPop(&OPND, &a); nPop(&OPND, &b); strPop(&OPTR, &op);

v = Execute(a, op, b); nPush(&OPND, v); break;

}//switch

}//else

}//while

v = nGetTop(&OPND);

return(v);

}

void main()

{

int result;

printf("\t\t\t欢迎使用本系统\n\n");

result=ExpEvaluation();

printf("=%d",result);

printf("\n");

}

我已写好,但不知问题出在哪儿,那位好心人,大神帮忙修改一下,谢谢

1、i 没有初值;

2、ch = ch * 10 + (str[i] - 48); 这句是什么意思,如果是想转换字符为数值,算法有问题,而且nStack中你定义的是字符,怎么装下数值?

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值