当前位置: 代码迷 >> C语言 >> 表达式求值中的一个问题1
  详细解决方案

表达式求值中的一个问题1

热度:153   发布时间:2007-04-23 22:03:24.0
表达式求值中的一个问题1

看下面那个Precede函数哪里出了问题,不能输出!
#define STACK_INIT_SIZE 100
#include "stdio.h"
#include "stdlib.h"

int i,j;
char a[7]={'+','-','*','/','(',')','#'};
char b[7][7]={
{'>','>','<','<','<','>','>'},
{'>','>','<','<','<','>','>'},
{'>','>','>','>','<','>','>'},
{'>','>','>','>','<','>','>'},
{'<','<','<','<','<','=',' '},
{'>','>','>','>',' ','>','>'},
{'<','<','<','<','<',' ','='}
};

typedef struct
{
int *base;
int *top;
int stacksize;
} *SqStack_I;

typedef struct
{
char *base;
char *top;
int stacksize;
} *SqStack_C;

SqStack_I InitStack_I()
{
SqStack_I S;
S->base=(int*)malloc(STACK_INIT_SIZE*sizeof(int));
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
return S;
}

SqStack_C InitStack_C()
{
SqStack_C S;
S->base=(char*)malloc(STACK_INIT_SIZE*sizeof(char));
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
return S;
}

SqStack_I Push_I(SqStack_I S,char e)
{ int w;
w=e-48;
*S->top++=w;
return S;
}

SqStack_C Push_C(SqStack_C S,char e)
{
*S->top++=e;
return S;
}

int Pop_I(SqStack_I S)
{
int e;
e=*--S->top;
return e;
}

char Pop_C(SqStack_C S)
{
char e;
e=*--S->top;
return e;
}

int GetTop_I(SqStack_I S)
{
int e;
e=*(S->top-1);
return e;
}

char GetTop_C(SqStack_C S)
{
char e;
e=*(S->top-1);

return e;
}

int In(char c)
{if(c<='9'&&c>='0') return 1;
else return 0;
}

char Precede(char d,char c)
{ int m,n;
for(i=0;i<7;i++)
if(d==a[i]) m=i;
printf("%d",m);
for(j=0;j<7;j++)
if(c==a[j]) n=j; /*printf("%d",n);*/
/*printf("%d,%d",i,j); */
return b[m][n];
/* printf("%c",b[i][j]);*/
}

int Operate(int a,char theta,int b)
{
if (theta=='+') return (a+b);
if (theta=='-') return (a-b);
if (theta=='*') return (a*b);
if (theta=='/') return (a/b);
}

main()
{
int x,y;
char c,theta;
SqStack_C OPTR;
SqStack_I OPND;
OPTR=InitStack_C();
Push_C(OPTR,'#');
/*printf("%c",GetTop_C(OPTR));*/

OPND=InitStack_I();
c=getchar();
Precede('#','+');
while (c!='#'||GetTop_C(OPTR)!='#')
{if(In(c)) {Push_I(OPND,c);c=getchar();/*printf("%d\n",GetTop_I(OPND));*/}
else
switch (Precede(GetTop_C(OPTR),c))
{ /*printf("%c",GetTop_C(OPTR));*/
/*printf("%c",Precede(GetTop_C(OPTR),c));*/
case'<': Push_C(OPTR,c); c=getchar(); break;
case'=': Pop_C(OPTR); c=getchar(); break;
case'>': theta=Pop_C(OPTR);
x=Pop_I(OPND); y=Pop_I(OPND);
Push_I(OPND,Operate(x,theta,y)+48);
break;
}
}
printf("%d\n",GetTop_I(OPND));
getchar();
getchar();

}

搜索更多相关的解决方案: 表达式  include  

----------------解决方案--------------------------------------------------------
  相关解决方案