定义了一个双栈的数据结构实现中缀表达式求值,求大神帮忙看下!
程序我刚刚写好,由于要上课我来不及初步调试,求大神帮找语法错误,我回来在看看算法......这样效率会高!@#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
#define STACKSIZE 100
#define STACKADD 10
/*运算符优先级数据结构*/
struct
{
char ysf;
int yxj;
}
ysz[]={{'(',6},{')',0},{'[',6},{']',1},{'{',6},{'}',2},{'#',3},{'^',3},{'*',4},{'/',4},{'+',5},{'-',5}};
typedef struct
{
char *topchar;
char *bottomchar;
int charStackSize;
double *topdb;
double *bottomdb;
int doubleStackSize;
}stack;
void initstack(stack *s)
{
s->bottomchar=(char*)malloc(STACKSIZE*sizeof(char));
if(!s->bottomchar)
exit(0);
s->topchar=s->bottomchar;
s->charStackSize=STACKSIZE;
s->bottomdb=(double *)malloc(STACKSIZE*sizeof(double));
if(!s->bottomdb)
exit(0);
s->topdb=s->bottomdb;
s->doubleStackSize=STACKSIZE;
}
void pushchar(stack *s,char a)
{
if(s->topchar-s->bottomchar>=STACKSIZE)
{
s->bottomchar=(char*)realloc(s->bottomchar,(s->charStackSize+STACKADD)*sizeof(char));
if(!s->bottomchar)
exit(0);
}
*(s->topchar)=a;
s->topchar++;
}
void pushdb(stack *s,double b)
{
if(s->topdb-s->bottomdb>=STACKSIZE)
{
s->bottomdb=(double *)realloc(s->bottomdb,(s->doubleStackSize+STACKADD)*sizeof(double));
if(!s->bottomdb)
exit(0);
}
*(s->topdb)=b;
s->topdb++;
}
char popchar(stack *s)
{
char i;
if(!s&&s->topchar==s->bottomchar)
exif(0);
i=*(--s->topchar);
return i;
}
double popdb(stack *s)
{
double i;
if(!s&&s->topdb==s->bottomdb)
exit(0);
i=*(--s->topdb);
return i;
}
int Cealenstack(stack *s)
{
if(!s)
return 0;
s->topchar=s->bottomchar;
s->topdb=s->bottomdb;
return 1;
}
void Destorystack(stack *s)
{
int lenchar=stackcharLen(s),lendb=stackdblen(s);
if(!s)
exit(0);
while(lenchar)
{
*(--s->topchar)=NULL;
lenchar--;
}
while(lendb)
{
*(--s->topdb)=0;
lendb--;
}
}
int stackcharLen(stack *s)
{
return(s->topchar-s->bottomchar);
}
int stackdblen(stack *s)
{
return (s->topdb-s->bottomdb);
}
/*以下为程序核心*/
double Nibolansuanfa(char *str,stack *s);/*将中缀字符串转换成后缀然后求值*/
int IsRxz(char i)
{
if(i==')'||i==']'||i=='}')
return 1;
else return 0;
}
int Isysf(char i)
{
if(i=='*'||i=='/'||i=='^'||i=='#'||i=='+'||i=='-')
return 1;
else return 0;
}
int Isnum(char i)
{
if(i>='0'&&i<='9'||i=='.')
return 1;
else return 0;
}
char Cmpysf(char a,char b)
{
int j=0,i=0;
while(ysz!=a)
j++;
while(ysz!=b)
i++;
if(ysz.yxj<=ysz.yxj)
return a;
else return 0;
}
void YsStackdb(char i)
{
int k,j;
switch(i)
{
case '#':
k=popdb(s);
j=popdb(s);
pushdb(s,pow(j,1.0/k));
break;
case '^':
k=popdb(s);
j=popdb(s);
pushdb(s,pow(j,k));
break;
case'*':
k=popdb(s);
j=popdb(s);
pushdb(s,j*k);
break;
case'/':
k=popdb(s);
j=popdb(s);
pushdb(s,j/k);
break;
case'+':
k=popdb(s);
j=popdb(s);
pushdb(s,j+k);
break;
case'-':
k=popdb(s);
j=popdb(s);
pushdb(s,j-k);
break;
}
return;
}
int main(void)
{
stack *sk
char str;
printf("请输入一个中缀表达式:");
scanf("%s",str);
printf("结果:%lf\n",Nibolansuanfa(str,sk));
return 0;
}
double Nibolansuanfa(char *str,stack *s)
{
initstack(&s);
char st,tc,xy;
int j=0,i=0,yxcount=0;
double d;
while(str!='\0')
{
/*数字字符拷贝*/
if(Isnum(str))
{
st=str;
i++;
j++;
continue;
}
/*非数字字符进字符栈*/
else if(!IsRxz(str)&&!Isnum(str))
{
pushchar(s,str);
j++;
}
else if(IsRxz(str))
{
if(str==')')
while((tc=popchar(s))!='(')
{
if(Isysf(tc))
{
xy=popchar(s);
pushchar(s,xy);
if(Isysf(xy))
{
if(Cmpysf(tc,xy))
YsStackdb(tc);
else pushchar(tc);
}
}
else
{
printf("中缀表达式格式错误!");
exit(0);
}
}
if(str==']')
while((tc=popchar(s))!='[')
{
if(Isysf(tc))
{
xy=popchar(s);
pushchar(s,xy);
if(Isysf(xy))
{
if(Cmpysf(tc,xy))
YsStackdb(tc);
else pushchar(tc);
}
}
else
{
printf("中缀表达式格式错误!");
exit(0);
}
}
if(str=='}')
while((tc=popchar(s))!='{')
{
if(Isysf(tc))
{
xy=popchar(s);
pushchar(s,xy);
if(Isysf(xy))
{
if(Cmpysf(tc,xy))
YsStackdb(tc);
else pushchar(tc);
}
}
else
{
printf("中缀表达式格式错误!");
exit(0);
}
}
}
else
{
printf("中缀表达式格式错误!");
exit(0);
}
/*数字字符转换并进数字栈*/
i=0;
d=atof(st);
pushdb(s,d);
st={0};
}
if(--(s->topdb)==s->bottomdb)
return popdb(s);
else return 0;
}
答案
页:
[1]