none
求助,高手帮忙修改一下程序!! RRS feed

  • 问题

  • 程序可以运行,但是每个功能都无法执行,马房帮忙修改一下,谢谢!!
    // preprocessor directives
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <ctype.h>
    #include <malloc.h>

    // user-defined types
    typedef char string30[31];

    struct bookNode
    {
     string30 title;
     int noInStock;
     bookNode *next;
    };

    typedef struct bookNode *bookNodePtr;

    struct listADT
    {
      bookNodePtr head;
    };

    struct authorNode
    {
      string30  entry;
      listADT books;
      authorNode *left;
      authorNode *right;
    };

    typedef struct authorNode *authorNodePtr;

    struct treeADT
    {
      authorNodePtr root;
    };

     

    //------- Function Prototypes -------
    void displayMenu();
    void initialiseControl(treeADT&);
    void addControl(treeADT &);
    void deleteControl(treeADT &);
    void inOrder (authorNodePtr);
    void addBookControl(treeADT &);
    void deleteBookControl (treeADT &);
    void displayBooks (treeADT &);
    int isEmpty(treeADT aTree);
    void createNode ( string30, authorNodePtr & );
    void addToTree (string30 newVal,authorNodePtr & );
    authorNodePtr getAuthor(authorNodePtr &treePtr,string30 newVal);
    void searchAuthor(authorNodePtr &treePtr,string30 newVal);
    void addToList(authorNodePtr &treePtr,bookNodePtr );
    void searchdelete(authorNodePtr &treePtr,string30 book);
    void searchDelete(string30 searchVal, authorNodePtr &treePtr );
    void deleteNode(authorNodePtr &treePtr);
    void initialise(authorNodePtr treePtr);
    void creat(authorNodePtr treePtr,bookNodePtr );
    // Start of program
    void main()
    {
       treeADT oneTree;
       char choice;

       oneTree.root = NULL; // initialise tree

       do
       {
          //clrscr();
          displayMenu();
     //gotoxy(18,14);
     printf ("Make your selection > ");
     choice = toupper(getch());

     switch (choice)
     {
           case '1' : initialiseControl(oneTree);
         break;
        case '2' : addControl(oneTree);
         break;
        case '3' : deleteControl(oneTree);
         break;
        case '4' : inOrder (oneTree.root);
         break;
        case '5' : addBookControl (oneTree);
         break;
        case '6' : deleteBookControl(oneTree);
         break;
        case '7' : displayBooks(oneTree);
         break;
          } // endswitch
       }while (choice != 'Q');
    } // end  main

    void displayMenu()
    {
       printf ("\n\t\tSoftware Development: Linked Data Structures.");
       printf ("\n\t\t   Assessment Task 5 - application");
       printf ("\n\n\t\t\t1. Initialise tree.");
       printf ("\n\t\t\t2. Add author.");
       printf ("\n\t\t\t3. Delete author.");
       printf ("\n\t\t\t4. Display authors.");
       printf ("\n\t\t\t5. Add book.");
       printf ("\n\t\t\t6. Delete book.");
       printf ("\n\t\t\t7. Display books.");
       printf ("\n\t\t\tQ. QUIT");
    } // end displayMenu

    void initialiseControl (treeADT& aTree)
    {
     initialise(aTree.root);
        aTree.root = NULL;
    /*
       Recursively frees up memory occupied by books and
       authors and then sets the tree root pointer TO NULL
    */
    } // end initialiseControl

     
    void addControl(treeADT &aTree)
    {
     authorNodePtr tempPtr;
     string30 newValue;
        printf ("\n\n\t\tEnter a name > ");
        scanf ("%s", newValue);
        if (isEmpty(aTree))
     {
      createNode ( newValue, aTree.root );
      tempPtr=aTree.root;
     }
        else
            addToTree( newValue, aTree.root );
    /*
     CALL createNode
     IF empty tree THEN
      SET tree root pointer TO temp node pointer
     ELSE
      CALL addToTree
     ENDIF
    */
    } // end Control

    void deleteControl(treeADT &aTree)
    {
     string30 searchValue;
     authorNodePtr treePtr;
     if(aTree.root=NULL)
      printf("empty tree");
     else
     {
      treePtr=getAuthor(treePtr,searchValue);
      searchDelete(searchValue,treePtr);
     }


    /*
       IF empty tree THEN
          DISPLAY "empty tree" message
       ELSE
          PROMPT FOR and ACCEPT search value
     CALL searchDelete
       ENDIF
    */
    } // end deleteControl

    void inOrder (authorNodePtr treePtr)
    {
     if(treePtr)
     {
      inOrder(treePtr->left);
      printf("\n\t%s",treePtr->entry);
      inOrder(treePtr->right);
     }
    /*
       Display the author names using
       an in-order traversal of the tree.
    */
    } // end inOrder


    void addBookControl (treeADT &aTree)
    {
       bookNodePtr tempPtr;
       authorNodePtr treePtr=aTree.root;
       string30 newVal;
       tempPtr=new bookNode;
       if(aTree.root==NULL)
        printf("out of memory");
       else
        treePtr=getAuthor(treePtr,newVal);
        printf("Please enter the book title");
     scanf("%s",tempPtr->title);
     printf("Please enter the book no.");
     scanf("%d",&tempPtr->noInStock);
     if(treePtr->books.head==NULL)
        {
         creat(treePtr,tempPtr);
        }
        else
         addToList(treePtr,tempPtr);
    /*
       IF NOT enough memory on heap THEN
          DISPLAY "out of memory" message
       ELSE
          SET tree pointer TO CALL getAuthor
       ENDIF
     
       IF tree pointer <> NULL THEN
          CALL creatNode
     IF empty list THEN
        SET list head pointer TO temp node pointer
     ELSE
        CALL addToList
     ENDIF
       ENDIF
    */
    } // end addBookControl


    void deleteBookControl (treeADT &aTree)
    {
     //bookNodePtr tempPtr;
     authorNodePtr treePtr;
     //int found;
     string30 searchVal,book;
     treePtr=getAuthor(treePtr,searchVal);
     if(treePtr)
     {
      if(treePtr->books.head==NULL)
       printf("empty list");
      else
       searchdelete(treePtr,book);
     }
    /*
       SET tree pointer TO CALL getAuthor

       IF tree pointer <> NULL THEN
          IF empty list THEN
        DISPLAY "empty list" message
     ELSE
        PROMPT FOR and ACCEPT search value
        SET found TO CALL deleteBookNode

        IF found THEN
           Put list node back on heap
        ELSE
           DISPLAY "book not found" message
        ENDIF
     ENDIF
       ENDIF
    */
    } // end deleteBookcontrol

    void displayBooks (treeADT &aTree)
    {
       bookNodePtr currPtr;
       authorNodePtr treePtr;
       string30 searchVal;
       treePtr=getAuthor(treePtr,searchVal);
       if(treePtr)
       {
        if(treePtr->books.head==NULL)
         printf("empty list");
        else
        {
         currPtr=treePtr->books.head;
         while(currPtr)
         {
          printf("\t\n",currPtr->title,currPtr->noInStock);
          currPtr=currPtr->next;
         }
        }
       }

    /*
       SET tree pointer TO CALL getAuthor

       IF tree pointer <> NULL THEN
          IF empty list THEN
        DISPLAY "empty list" message
     ELSE
        SET current pointer TO list head pointer
        WHILE current pointer <> NULL DO
           DISPLAY book title and stock
      SET current pointer TO current node next pointer
        ENDWHILE
     ENDIF
       ENDIF
    */
    } // end displayBooks

     

    int isEmpty(treeADT aTree)
    {
     if(aTree.root==NULL)
      return 1;
     else
      return 0;
    } // end isEmpty
    void createNode ( string30 newVal, authorNodePtr &newptr )
    {
       newptr = new (authorNode);
       strcpy (newptr->entry,newVal);
       newptr->left = NULL;
       newptr->books.head=NULL;
       newptr->right = NULL;
    } // end createNode
    void addToTree (string30 newVal, authorNodePtr &treePtr)
    {
     authorNodePtr p,q,s;
     p=new authorNode;
     p->left=NULL;
     p->right=NULL;
     strcpy(p->entry,newVal);
     q=treePtr;
     while(q)
     {
     
      if(strcmp(q->entry,p->entry)>0)
      {
          s=q;
       q=q->left;
            }
      else
      {
       if(strcmp(q->entry,p->entry)<0)
       {
        s=q;
        q=q->right;
       }
       else
        printf("The value has been exist");
      }
     }
     if(strcmp(s->entry,p->entry)>0)
      s->left=p;
     else
     {
      if(strcmp(s->entry,p->entry)<0)
      s->right=p;
     }
    } // end addToTree
    authorNodePtr getAuthor(authorNodePtr &treePtr,string30 newVal)
    {
     printf("Please enter the author name:");
      scanf("%s",newVal);
     searchAuthor(treePtr,newVal);
     return(treePtr);
    }
    void searchAuthor(authorNodePtr &treePtr,string30 newVal)
    {
     while(treePtr!=NULL&&strcmp(treePtr->entry,newVal)!=0)
     {
      if(strcmp(treePtr->entry,newVal)>0)
       treePtr=treePtr->left;
      else if(strcmp(treePtr->entry,newVal)<0)
       treePtr=treePtr->right;
     }
    }
    void addToList(authorNodePtr &treePtr,bookNodePtr tempPtr)
    {
     bookNodePtr q,s;
     q=treePtr->books.head;
     tempPtr->next=NULL;
     s=NULL;
     if(strcmp(tempPtr->title,q->title)<0)
     {
      tempPtr->next=treePtr->books.head;
      treePtr->books.head=tempPtr;
     }
     else
     {
      while(q->next!=NULL)
      {
       q=q->next;
      }
      if(strcmp(q->title,tempPtr->title)<0)
       q->next=tempPtr;
      else
      {
       q=treePtr->books.head->next;
       s=treePtr->books.head;
       while(strcmp(q->title,tempPtr->title)<0)
       {
        s=q;
        q=q->next;
       }
       tempPtr->next=q;
       s->next=tempPtr;
      }
     }
    }
    void searchdelete(authorNodePtr &treePtr,string30 book)
    {
     bookNodePtr p,s;
     p=treePtr->books.head;
     printf("Please enter the books name:");
     gets(book);
     while(strcmp(p->title,book)!=0 && p->next!=NULL)
     {
      s=p;
      p=p->next;
     }
     if(strcmp(p->title,book)==0)
     {
      s->next=p->next;
      delete p;
     }
     else
      printf("book not found");
    }
    void deleteNode(authorNodePtr &treePtr)
    {
     authorNodePtr p,q;
     if(treePtr->left==NULL&&treePtr->right==NULL)
     {
      delete treePtr;
      treePtr=NULL;
     }
     else
      if(treePtr->left!=NULL&&treePtr->right==NULL)
      {
       p=treePtr;
       treePtr=p->left;
       delete p;
      }
     else
             if(treePtr->left==NULL&&treePtr->right!=NULL)
      {
       p=treePtr;
       treePtr=p->right;
       delete p;
      }
     else
     {
      p=treePtr;
      q=treePtr->right;
      while(q->left)
      {
       p=q;
       q=q->left;
      }
      if(p==treePtr)
      {
       treePtr->right=q->right;
      }
      else
      {
       p->left=q->right;
      }
      strcpy(treePtr->entry,q->entry);
      delete q;
     }
     
    } // end deleteNode
    void searchDelete(string30 searchVal, authorNodePtr &treePtr )
    {
     if(treePtr)
     {
      if(strcmp(searchVal,treePtr->entry)<0)
       searchDelete(searchVal,treePtr->left);
      else
         if(strcmp(searchVal,treePtr->entry)>0)
          searchDelete(searchVal,treePtr->right);
         else
         {
          deleteNode(treePtr);
         }
     }
    }
    void initialise(authorNodePtr treePtr)
    {
     bookNodePtr p,s;
     if(treePtr)
     {
      p=treePtr->books.head;
      while(p->next)
      {
       s=p;
       p=p->next;
       delete s;
      }
      deleteNode(treePtr);
     }
     else
      printf("The tree hasn't exist");
    }
    void creat(authorNodePtr treePtr,bookNodePtr p)
    {
     treePtr->books.head=p;
     p->next=NULL;
    }

     


     

     

    2009年12月17日 11:35

答案

  • 单步调试你的程序,检查每一步修改的变量的值。



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
    Visual C++ MVP
    2009年12月17日 19:47
    版主
  • 不要上来就进行集成测试,你可以对基础的功能比如AddControl和DeleteControl进行UnitTest。保证这个功能能正常运行。否则你可能不知道问题处在什么地方。
    麻烦把正确答案设为解答。
    2009年12月21日 1:27
    版主

全部回复

  • 单步调试你的程序,检查每一步修改的变量的值。



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
    Visual C++ MVP
    2009年12月17日 19:47
    版主
  • 还是没有修改过来这个程序。

    2009年12月18日 13:02
  • 不要上来就进行集成测试,你可以对基础的功能比如AddControl和DeleteControl进行UnitTest。保证这个功能能正常运行。否则你可能不知道问题处在什么地方。
    麻烦把正确答案设为解答。
    2009年12月21日 1:27
    版主