标题:[原创]求助!!急急急急!·
取消只看楼主
snowylhm
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2005-8-29
 问题点数:0 回复次数:0 
[原创]求助!!急急急急!·

谁来帮我看这个程序呀,计算表达式值的程序,编译通过,就是没有正确结果,其中的字符输入函数总是跳过去不执行。 #include <stdio.h> #define maxsize 100 typedef char elementype; typedef struct { char data[maxsize]; int top; }stacktype;

void initstack(stacktype *s) { s->top=-1; }

void push(stacktype *s,char x) { if (s->top==maxsize) printf("the stack is full"); else { s->top++; s->data[s->top]=x; } }

char pop(stacktype *s) { elementype x; if (s->top==-1) printf("the stack is empty"); else { x=s->data[s->top]; s->top--; return(x); } }

int empty(stacktype *s) { if (s->top==-1) return(1); else return(0); }

int stackfull(stacktype *s) { if (s->top==maxsize) return(1); else return(0); }

char top(stacktype *s) { char x; if (s->top==-1) return(0); else x=s->data[s->top]; return(x); }

char OP[10]={'+','-','*','/','(',')','#'}; int precede[7][7]={ 1,1,2,2,2,1,1, 1,1,2,2,2,1,1, 1,1,1,1,2,1,1, 1,1,1,1,2,1,1, 2,2,2,2,2,3,0, 1,1,1,1,0,1,1, 2,2,2,2,2,0,3};

int In(char c,char *op) { int i=0; while(i<7) if(c==op[i++]) return 1; else return 0; }

char Preced(char op,char c) { int pos_op; int pos_c; int i; for(i=0;i<7;i++) { if(op==OP[i]) pos_op=i; if(c==OP[i]) pos_c=i; } switch(precede[pos_op][pos_c]) { case 1: return '>'; case 2: return '<'; case 3: return '='; } }

char Operate(int a,char theta,int b) { switch(theta) { case '+':return a+b-'0'; case '-':return a-b+'0'; case '*':return (a-'0')*(b-'0')+'0'; case '/':return (a-'0')/(b-'0')+'0'; } }

char EvaluateExpression() { stacktype s2,s1; char c,x,theta; char a,b; initstack(&s2); push(&s2,'#'); initstack(&s1); c=getchar(); while(c!='#'||top(&s2)!='#') { if(!In(c,OP)) { push(&s1,c); c=getchar();} else switch(Preced(top(&s2),c)) { case '<': push(&s2,c); c=getchar(); break; case '=': pop(&s2); c=getchar(); break; case '>': theta=pop(&s2); b=pop(&s1); a=pop(&s1); push(&s1,Operate(a,theta,b)); break; } } c=top(&s1);

return c; }

main() { char i; i=EvaluateExpression(); printf("\nThis expression's result is: "); printf("%d\n\n\n\n",i-'0');

} 

搜索更多相关主题的帖子: void include 表达式 color 
2005-08-29 11:25



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-26240-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.043221 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved