大侠们看一下这个程序哪里错了?
#include<stdio.h>#include<stdlib.h>
#include<malloc.h>
#include"creatTree.h"
#define maxNum 80
typedef struct BinNode{
char data;
struct BinNode *Lchild,*Rchild;
}BinNode;
typedef BinNode * BinTree;
void readExp(char exp[])
{
int i,j;
printf("输入前缀算术表达式(小于80个字符):S");
while(*gets(exp)=='\0');
i=j=0;
while(exp[i]!='\0')/*去除空格字符*/
{
if(exp[i]=='\0')
i++;
else
exp[i++]=exp[j++];
}
exp[j]='\0';
}
BinTree treeExp(char exp[],int *k)
{
char ch;
BinTree p;
ch=exp[(*k)++];/*扫描当前字符*/
p=(BinTree)malloc(sizeof(BinNode));
p->data=ch;/*建立当前字符的结点*/
if(ch>='a'&&ch<='z')
p->Lchild=p->Rchild=NULL;/*置为叶子结点*/
else
{
p->Lchild=creatTree(exp,k);/*建立左子树*/
p->Rchild=creatTree(exp,k);/*建立右子树*/
}
return(p);
}
int opPriority(char c)
{
if(c=='+'||c=='-')
return(1);
if(c=='*'||c=='/')
return(2);
if(c=='^')
return(3);
else
return(4);
}
void printExp(BinTree t)
{
int pri,pri1;
char ch;
ch=t->data;
if(ch>='a'&&ch<='z')
printf("&s",ch);
else
{
pri=opPriority(ch);/*球根结点的优先级*/
pri1=opPriority(t->Lchild->data);/*求左孩子的优先级*/
if(pri>pri1)
printf("(");
printExp(t->Lchild);/*输出左子树*/
if(pri>pri1)
printf(")");
printf("%c",ch);/*输出根结点*/
pri1=opPriority(t->Rchild->data);/*求右孩子的优先级*/
if(pri>=pri1)
printf("(");
printExp(t->Rchild);/*输出右子树*/
if(pri>=pri1);
printf(")");
}
}
void freeTree(BinTree t)
{
if(t)
{
freeTree(t->Lchild);
freeTree(t->Rchild);
free(t);
}
}
void main()
{
char ch,exp[maxNum];
int k;
BinTree p;
ch='Y';
while(ch=='Y'||ch=='y')
{
readExp(exp);/*读入一个合法的前缀算术表达式的字符*/
k=0;
p=treeExp(exp,&k);/*从第K个下标开始扫描字符传,建立二叉树*/
printf("中缀表达式:");
printExp(p);/*输出带括弧的表达式*/
printf("\n");
freeTree(p);/*释放二叉树的各结点空间*/
printf("继续吗(y/n)?");
scanf("%s",&ch);
}
}