#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中你定义的是字符,怎么装下数值?