| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 491 人关注过本帖
标题:求各位神仙可以帮忙调试下下面的程序,总是运行时出错,也不太符合我写代码 ...
只看楼主 加入收藏
xiaoshalong
Rank: 2
等 级:论坛游民
帖 子:20
专家分:16
注 册:2013-1-10
结帖率:0
收藏
 问题点数:0 回复次数:4 
求各位神仙可以帮忙调试下下面的程序,总是运行时出错,也不太符合我写代码时的预想, - -,求助!!!
这是一个简单的构建二叉树并实现二叉树结点插入,和删除程序,
程序代码:
/*主函数*/
#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 编辑 ]
搜索更多相关主题的帖子: 二叉树 
2013-06-08 22:18
xiaoshalong
Rank: 2
等 级:论坛游民
帖 子:20
专家分:16
注 册:2013-1-10
收藏
得分:0 
/*头文件*/
程序代码:
#ifndef _TREE_H_
#define _TREE_H_
#include <stdbool.h>


 typedef struct item

 {
     char name[20];
     char sex[10];
     float age;

 } Item;


 #define MAXITEMS 15


 typedef struct node

 {
     Item item;
     struct node *left;
     struct node *right;

 } Node;


 typedef struct tree

 {
     struct node * root;
      unsigned int size;

 } Tree;

 typedef struct pair
{
    Node *parent;
    Node *child;
} Pair;


 void InitializeTree(Tree * ptree);


 bool TreeIsEmpty(const Tree *ptree);


 bool TreeIsFull(const Tree *ptree);


 unsigned int TreeItemCount(const Tree* ptree);


 bool AddItem(const Item *pitem,Tree * ptree);


 bool InTree(const Item *pitem,const Tree *ptree);


 bool DeleteItem(const Item *pitem,Tree * ptee);


 void Traverse(const Tree *ptree,void(*pun)(Item item));

void DeleteAll(Tree *ptree);
#endif

2013-06-08 22:18
xiaoshalong
Rank: 2
等 级:论坛游民
帖 子:20
专家分:16
注 册:2013-1-10
收藏
得分:0 
/*实现头文件功能*/
程序代码:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "tree.h"

static Node *MakeNode(const Item *pitem);
static bool ToLeft(const Item *pitem1,const Item *pitem2);
static bool ToRight(const Item *pitem1,const Item *pitem2);
static void AddNode(Node *new_node,Node *root);
static void InOrder(const Node *root,void (* pfun)(Item item));
static Pair SeekItem(const Item*pitem,const Tree *ptree);
static void DeleteNode(Node **pnode);
static void (DeleteAllNodes(Node *pnode));

void InitializeTree(Tree *ptree)
{
    ptree->root=NULL;
    ptree->size=0;
}

bool TreeIsEmpty(const Tree *ptree)
{
    if(ptree->root==NULL)
        return true;
    else
        return false;
}
bool TreeIsFull(const Tree *ptree)
{
    if(ptree->size==MAXITEMS)
        return true;
    else
        return false;
}

unsigned int TreeItemCount(const Tree *ptree)
{
    return ptree->size;
}

bool AddItem(const Item *pitem,Tree *ptree)
{
    struct node *new_node;

    if(TreeIsFull(ptree))
    {
        printf("the tree is full.\n");
        return false;
    }
    if(SeekItem(pitem,ptree).child!=NULL)
    {
        printf("the item is already exist.\n");
        return false;
    }
    new_node=MakeNode(pitem);
    if(new_node=NULL)
    {
        printf("can\'t creat node\n");
        exit(1);
    }
    ptree->size++;
    if(ptree->root=NULL)
        ptree->root=new_node;
    else
        AddNode(new_node,ptree->root);
    return true;
    }

    bool InTree(const Item *pitem,const Tree *ptree)
    {
        return(SeekItem(pitem,ptree).child==NULL?false:true);
    }

bool DeleteItem(const Item *pitem,Tree *ptree)
{
    Pair look;
    look=SeekItem(pitem,ptree);
    if(look.child==NULL)
        return false;
    if(look.parent==NULL)
        DeleteNode(&ptree->root);
    else if(look.parent->left==look.child)
        DeleteNode(&look.parent->left);
    else
        DeleteNode(&look.parent->right);
    ptree->size--;

    return true;
}

void Traverse(const Tree *ptree,void(*pfun)(Item item))
{
    if (ptree!=NULL)
        InOrder(ptree->root,(*pfun));
}

void DeleteAll(Tree *ptree)
{
    if(ptree->root!=NULL)
    {
        DeleteAllNodes(ptree->root);
        ptree->root=NULL;
        ptree->size=0;
    }
}

static void InOrder(const Node *root,void(*pfun)(Item item))
{
    if(root!=NULL)
    {
        InOrder(root->left,(*pfun));
          (*pfun)(root->item);
        InOrder(root->right,(*pfun));
    }
}
static void DeleteAllNodes(Node *root)
{
    Node *pright;

    if(root!=NULL)
    {
        pright=root->right;
        DeleteAllNodes(root->left);
        free(root);
        DeleteAllNodes(pright);

      }

}
static void AddNode(Node *new_node,Node *root)
{

 if(ToLeft(&new_node->item,&root->item))

 {
     if(root->left==NULL)
        root->left=new_node;
     else
        AddNode(new_node,root->left);


 }

 else if(ToRight(&new_node->item,&root->item))

 {
     if(root->right==NULL)
         root->right==new_node;
         else
         AddNode(new_node,root->right);
     }

}
static bool ToLeft(const Item *pitem1,const Item *pitem2)
{
    int flag;


    if((flag=strcmp(pitem1->name,pitem2->name))<0)
        return true;
    else if(flag=0&&strcmp(pitem1->sex,"man")==0)
        return true;
    else
        return false;
}
static bool ToRight(const Item *pitem1,const Item *pitem2)
{
    int flag;


    if((flag=strcmp(pitem1->name,pitem2->name))>0)
        return true;
    else if(flag=0&&strcmp(pitem1->sex,"female")==0)
        return true;
    else
        return false;
}
static Node* MakeNode(const Item *pitem)
{
    Node *new_node;
    new_node=(Node *)malloc(sizeof(Node));
    if(new_node!=NULL)
        {
            new_node->item=*pitem;
            new_node->left=NULL;
            new_node->right=NULL;

        }
        return new_node;
}
static Pair SeekItem(const Item *pitem,const Tree *ptree)
{
    Pair look;
    look.parent=NULL;
    look.child=ptree->root;

    if(look.child==NULL)
       return look;
    while(look.child!=NULL)
    {
        if(ToLeft(pitem,&(look.child->item)))
        {
            look.parent=look.child;
            look.child=look.child->left;
        }
        else if(ToRight(pitem,&(look.child->item)))
        {
            look.parent=look.child;
            look.child=look.child->right;
        }
        else
             break;
    }
    return look;
    }
static void DeleteNode(Node **pnode)
{
    Node *temp;
    if((*pnode)->left==NULL)
    {
        temp=*pnode;
        *pnode=(*pnode)->right;
        free("temp");
    }
    else if((*pnode)->right==NULL)
    {
        temp=*pnode;
        *pnode=(*pnode)->left;
        free(temp);
    }
    else
    {
        for(temp=(*pnode)->left;temp->right==NULL;temp=temp->right)
        continue;
        temp->right=(*pnode)->right;
        temp=*pnode;
        *pnode=(*pnode)->left;

        free(temp);
    }
}
2013-06-08 22:19
xiaoshalong
Rank: 2
等 级:论坛游民
帖 子:20
专家分:16
注 册:2013-1-10
收藏
得分:0 
昨天写到现在,还是不行,好烦躁...
2013-06-08 22:20
xiaoshalong
Rank: 2
等 级:论坛游民
帖 子:20
专家分:16
注 册:2013-1-10
收藏
得分:0 
T T,今天没有各位大版主在嘛,  - -,谁能告诉我是哪里出问题了嘛,

[ 本帖最后由 xiaoshalong 于 2013-6-8 23:34 编辑 ]
2013-06-08 23:18
快速回复:求各位神仙可以帮忙调试下下面的程序,总是运行时出错,也不太符合我写 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.032864 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved