求各位神仙可以帮忙调试下下面的程序,总是运行时出错,也不太符合我写代码时的预想, - -,求助!!!
这是一个简单的构建二叉树并实现二叉树结点插入,和删除程序,程序代码:
/*主函数*/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> #include "tree.h" #define LEN 20 void menu(void); void AddOne(Tree *ptree); void DeleteOne(Tree *ptree); void ShowAll(const Tree *ptree); void findone(const Tree *ptree); void PrintItem(Item item); bool the_order_isuseful=false; enum choices{additem,deleteitem,itemcount,search,showall,cleanall,quit}; const char * allchoice[]={"additem","deleteitem","itemcount","search","showall","cleanall","quit"}; char yourchoice[LEN]; int main(void) { Tree classmates; enum choices choice; bool the_choice_isuseful=false; InitializeTree(&classmates); menu(); while(strcmp(gets(yourchoice),"quit")&&yourchoice[0]!='\0') { for (choice=additem;choice<=quit;choice++) { if(strcmp(yourchoice,allchoice[choice])==0) { the_choice_isuseful=true; break; } if(the_choice_isuseful) switch (choice) //这里肯定有问题,但应该怎么修改 { case additem:AddOne(&classmates); break; case deleteitem:DeleteOne(&classmates); break; case itemcount:printf("%d classmates in my class.",TreeItemCount(&classmates)); break; case search :FindOne(classmates); case showall:ShowAll(&classmates); break; case cleanall:DeleteAll(&classmates); break; default : puts("switching error\n");break; } the_order_isuseful=false; puts("please enter your next order:"); } menu(); } puts("good job"); return 0; } void menu(void) { puts("please chioce what do you want to to?\n"); puts("enter \"additem\" to add a classmate.\n"); puts("enter \"deleteitem\" to delete a classmate\n"); puts("enter \"itemcount\" to konw the number of your classmatas\n"); puts("enter \"search\" to find your classmate\n"); puts("enter\"showall\"to printf all of your classmates.\n"); puts("enter \"cleanall\"to chean all items. \n"); puts("enter \"quit\" to quit\n"); return 0; } void AddOne (Tree *ptree) { Item temp; if(TreeIsFull(ptree)) puts("no room in my class."); else { puts("please enter name of your classmate:"); gets("temp.name"); puts("please enter sex of your classmate:"); gets("temp.sex"); puts("please enter age of your classmate"); AddItem(&temp,&ptree); } } void ShowAll(const Tree *ptree) { if(TreeIsEmpty(ptree)) puts("no information in yourclass,/n"); else Traverse(ptree,PrintItem); } void PrintItem(Item item) { printf("Classmate name:%4s",item.name); printf("\tsex :%4s",item.sex); printf("\tage:%4f\n",item.age); } void FindOne(const Tree *ptree) { Item item; Pair look; Item *pitem=&item; if (TreeIsEmpty(ptree)) { puts("no room to enter") ; return; } puts("enter the name of classmate you want to find:"); gets(item.name); puts("enter the sex of classmate you want to find:"); gets(item.sex); if(InTree(&item,ptree)) { printf("he or she is your classmate,and this is his or her information.\n"); /*look=SeekItem(pitem,ptree); printf("Classmate name:%4s",look.child->item.name); printf("\tsex :%4s",look.child->item.sex); printf("\tage:%4f\n",look.child->item.age);*/ //添加这几行就会发生变异错误... } else printf("he or she is not your classmate.\n"); } void DeleteOne(Tree *ptree) { Item temp; if(TreeIsEmpty(ptree)) {puts("no classmates."); return;} puts("please enter name of classmate you want to delete:\n"); gets(temp.name); puts("please enter sex of classmate you want to delete:\n"); gets(temp.sex); if(DeleteItem(&temp,ptree)) puts("sccessfully\n"); else puts("is not a member\n"); }
[ 本帖最后由 xiaoshalong 于 2013-6-9 07:03 编辑 ]