鱼C论坛

 找回密码
 立即注册
查看: 1160|回复: 2

为什么这代码错了

[复制链接]
发表于 2021-9-28 18:07:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<iostream>
#include<cstdlib>
#include<string.h>
#include<iomanip>

using namespace std;

typedef struct
{
    char num[5];
    char name[9];
    char phone[13];
}ElemType;

typedef struct node
{
    ElemType data;
   
    struct node *next;
}ListNode,*LinkList;

int menu_select(){
   int sn;
   cout<<"Welcome to Friends Book Management"<<endl;
   cout<<"1.LinkList of FriendsBook--Create"<<endl;
   cout<<"2.Node of Friends Book--Insert"<<endl;
   cout<<"3.Node of Friends Book--Search"<<endl;
   cout<<"4.Node of Friends Book--Delete"<<endl;
   cout<<"5.LinkList of Friends Book--Show"<<endl;
   cout<<"6.Exit"<<endl;
   cout<<"CeEnter 1-6 to select the menu: ";
   for(; ;){
    cin>>sn;
    if(sn<1 || sn>6)
        cout<<"\n\tInput Error! please enter 1-6 to select!\n\nCeEnter 1-6 to select the menu: ";
    else break;
   }
   cout<<endl<<endl;
   return sn;
}

void PrintLIst(LinkLIst head){
    LinkList p;
    p=head->next;
    if(p){
        cout<<"number(4) name(8) phone(11)"<<endl;
        cout<<"-------------------------------"<<endl;
    }
    else  cout<<"No record!"<<endl;
    while(p){
        cout<<setw(6)<<p->data.num<<setw(10)<<p->data.name<<setw(13)<<p->data.phone<<endl;
        p=p->next;
    }
    cout<<"---------------------------"<<endl<<endl;
}

voidCreateList(LinkList &head){
     ListNode *p, *rear;
     int flag=0;
     rear=head;
     while(flag==0){
        cout<<"---------------------------------"<<endl;
        cout<<"-     number(4)   name(8)  phone(11)    -"<<endl;
        cout<<"---------------------------------"<<endl;
        cout<<"Please input the data of record including : number(4) name(8) phone(11)"<<endl;
        p=new ListNode;
        cin>>p->data.num>>p->data.name>>p->data.phone;
        rear->next=p;
        rear=p;
        cout<<"Stop Creating List?(1:Stop or 0:Continue)\n";
        cin>>flag;
     }
     rear->next=NULL;
     PrintList(head);
     return;
}

void InsertNode(LinkList &head, ListNode  *p){
     ListNode  *p1,*p2;
     p1=head;
     p2=p1->next;
     while(p2&&(strcmp(p2->data.num,p->data.num)<0)) {
        p1=p2;
        p2=p2->next;
     }
     p1->next=p;
     p->next=p2;
     PrintList(head);
}

ListNode *ListFind(LinkList head){
     LinkList p;
     char num(5);
     char name(9);
     int selnum;
     
     cout<<"===========================\n";
     cout<<"1.search by number \n";
     cout<<"2.search by name \n";
     cout<<"===========================\n";
     cout<<"Enter your selection: ";
     p=head->next;
     cin>>selnum;
     if(selnum==1){
        cout<<"Please enter the number for searching: ";
        cin>>num;
        while(p && strcmp(p->data.num,num)!=0)
            p=p->next;
        if(p==NULL  ||  strcmp(p->data.num,num)!=0)
            p=NULL;
     }
     else
     if(selnum==2){
        cout<<"Please enter the name for searching: ";
        cin>>name;
        while(p && strcmp(p->data.name,name)!=0)
            p=p->next;
     }
     return p;
}

void DelNode(LinkList head){
     char ch;
     LinkList p,q;
     p=ListFind(head);
     if(!p){
        cout<<"not found!\n\n";
        return;
     }
     cout<<"Delete it, sure(y/n)?";
     cin>>ch;
     if(ch=='y'||ch=='Y'){
        q=head;
        while(q&&q->next!=p)
            q=q->next;
        q->next=p->next;
        delete p;
        cout<<"it has been delete!\n\n";
        PrintList(head);
     }
}


void DestroyList(LinkList  &L){
     LinkList p;
     while(L) {
        p=L;
        L=L->next;
        delete p;
     }
     return;
}


int main(){
   LinkList head,p;
   
   head=new ListNode;
   head->next=NULL;
   while(1){
    switch(menu_select()){
case 1:
    cout<<"**************************"<<endl;
    cout<<"*LinkList of Friends Book--Create *"<<endl;
    cout<<"**************************"<<endl;
    CreateList(head);
    break;
case 2:
    cout<<"**************************"<<endl;
    cout<<"  Node of Friends Book--Insert  *"<<endl;
    cout<< "**************************"<<endl;
    cout<<"*  number(4) name(8) phone(11)  *"<<endl;
    cout<<"**************************"<<endl;
    cout<<"Please input the data of record including:number(4) name(8) phone(11) "<<endl;
    p=new ListNode;
    cin>>p->data.num>>p->data.name>>p->data.phone;
    InsertNode(head,p);
    break;
case 3:
    cout<<"**************************"<<endl;
      cout<<"* Node of Friends Book --Search *"<<endl;
              cout<<"*********************************"<<endl;
              p=ListFind(head);
              if(p)
              {
                   cout<<" number(4) name(8) Phone(11) "<<endl;
                    cout<<"*********************************"<<endl;
                    cout<<setw(6)<<p->data.num <<setw(10)<<p->data.name<<setw(13)<<p->data.Phone<<endl;
                     cout<<"*********************************"<<endl;
              }
              else
                cout<<"Not Found\n\n";
              break;
        case 4:
             cout<<"*********************************"<<endl;
              cout<<"* Node of Friends Book --Delete *"<<endl;
              cout<<"*********************************"<<endl;
              DelNode(head);
              break;
        case 5:
             cout<<"*********************************"<<endl;
             cout<<"* LinkList of Friends Book --Show *"<<endl;
              cout<<"*********************************"<<endl;
              PrintList(head);
              break;
        case 6:
            cout<<"\t Quit! \n";
            DestroyList(head);
            return 0;
    }
   }

}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-9-28 18:25:18 | 显示全部楼层
本帖最后由 jhq999 于 2021-9-28 18:27 编辑

注意大小写;函数类型不能和函数名连在一起;声明数组用方括号不是括号
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string.h>
  4. #include<iomanip>

  5. using namespace std;

  6. typedef struct
  7. {
  8.     char num[5];
  9.     char name[9];
  10.     char phone[13];
  11. }ElemType;

  12. typedef struct node
  13. {
  14.     ElemType data;
  15.    
  16.     struct node *next;
  17. }ListNode,*LinkList;

  18. int menu_select(){
  19.    int sn;
  20.    cout<<"Welcome to Friends Book Management"<<endl;
  21.    cout<<"1.LinkList of FriendsBook--Create"<<endl;
  22.    cout<<"2.Node of Friends Book--Insert"<<endl;
  23.    cout<<"3.Node of Friends Book--Search"<<endl;
  24.    cout<<"4.Node of Friends Book--Delete"<<endl;
  25.    cout<<"5.LinkList of Friends Book--Show"<<endl;
  26.    cout<<"6.Exit"<<endl;
  27.    cout<<"CeEnter 1-6 to select the menu: ";
  28.    for(; ;){
  29.     cin>>sn;
  30.     if(sn<1 || sn>6)
  31.         cout<<"\n\tInput Error! please enter 1-6 to select!\n\nCeEnter 1-6 to select the menu: ";
  32.     else break;
  33.    }
  34.    cout<<endl<<endl;
  35.    return sn;
  36. }

  37. void PrintLIst(LinkList head){
  38.     LinkList p;
  39.     p=head->next;
  40.     if(p){
  41.         cout<<"number(4) name(8) phone(11)"<<endl;
  42.         cout<<"-------------------------------"<<endl;
  43.     }
  44.     else  cout<<"No record!"<<endl;
  45.     while(p){
  46.         cout<<setw(6)<<p->data.num<<setw(10)<<p->data.name<<setw(13)<<p->data.phone<<endl;
  47.         p=p->next;
  48.     }
  49.     cout<<"---------------------------"<<endl<<endl;
  50. }

  51. void CreateList(LinkList &head){///////////////
  52.      ListNode *p, *rear;
  53.      int flag=0;
  54.      rear=head;
  55.      while(flag==0){
  56.         cout<<"---------------------------------"<<endl;
  57.         cout<<"-     number(4)   name(8)  phone(11)    -"<<endl;
  58.         cout<<"---------------------------------"<<endl;
  59.         cout<<"Please input the data of record including : number(4) name(8) phone(11)"<<endl;
  60.         p=new ListNode;
  61.         cin>>p->data.num>>p->data.name>>p->data.phone;
  62.         rear->next=p;
  63.         rear=p;
  64.         cout<<"Stop Creating List?(1:Stop or 0:Continue)\n";
  65.         cin>>flag;
  66.      }
  67.      rear->next=NULL;
  68.      PrintLIst(head);///////////
  69.      return;
  70. }

  71. void InsertNode(LinkList &head, ListNode  *p){
  72.      ListNode  *p1,*p2;
  73.      p1=head;
  74.      p2=p1->next;
  75.      while(p2&&(strcmp(p2->data.num,p->data.num)<0)) {
  76.         p1=p2;
  77.         p2=p2->next;
  78.      }
  79.      p1->next=p;
  80.      p->next=p2;
  81.      PrintLIst(head);///////////
  82. }

  83. ListNode *ListFind(LinkList head){
  84.      LinkList p;
  85.      char num[5];//////////////
  86.      char name[9];
  87.      int selnum;
  88.      
  89.      cout<<"===========================\n";
  90.      cout<<"1.search by number \n";
  91.      cout<<"2.search by name \n";
  92.      cout<<"===========================\n";
  93.      cout<<"Enter your selection: ";
  94.      p=head->next;
  95.      cin>>selnum;
  96.      if(selnum==1){
  97.         cout<<"Please enter the number for searching: ";
  98.         cin>>num;
  99.         while(p && strcmp(p->data.num,num)!=0)
  100.             p=p->next;
  101.         if(p==NULL  ||  strcmp(p->data.num,num)!=0)
  102.             p=NULL;
  103.      }
  104.      else
  105.      if(selnum==2){
  106.         cout<<"Please enter the name for searching: ";
  107.         cin>>name;
  108.         while(p && strcmp(p->data.name,name)!=0)
  109.             p=p->next;
  110.      }
  111.      return p;
  112. }

  113. void DelNode(LinkList head){/////////////////
  114.      char ch;
  115.      LinkList p,q;
  116.      p=ListFind(head);
  117.      if(!p){
  118.         cout<<"not found!\n\n";
  119.         return;
  120.      }
  121.      cout<<"Delete it, sure(y/n)?";
  122.      cin>>ch;
  123.      if(ch=='y'||ch=='Y'){
  124.         q=head;
  125.         while(q&&q->next!=p)
  126.             q=q->next;
  127.         q->next=p->next;
  128.         delete p;
  129.         cout<<"it has been delete!\n\n";
  130.         PrintLIst(head);//////////////////////////
  131.      }
  132. }


  133. void DestroyList(LinkList  &L){
  134.      LinkList p;
  135.      while(L) {
  136.         p=L;
  137.         L=L->next;
  138.         delete p;
  139.      }
  140.      return;
  141. }


  142. int main(){
  143.    LinkList head,p;
  144.    
  145.    head=new ListNode;
  146.    head->next=NULL;
  147.    while(1){
  148.     switch(menu_select()){
  149. case 1:
  150.     cout<<"**************************"<<endl;
  151.     cout<<"*LinkList of Friends Book--Create *"<<endl;
  152.     cout<<"**************************"<<endl;
  153.     CreateList(head);
  154.     break;
  155. case 2:
  156.     cout<<"**************************"<<endl;
  157.     cout<<"  Node of Friends Book--Insert  *"<<endl;
  158.     cout<< "**************************"<<endl;
  159.     cout<<"*  number(4) name(8) phone(11)  *"<<endl;
  160.     cout<<"**************************"<<endl;
  161.     cout<<"Please input the data of record including:number(4) name(8) phone(11) "<<endl;
  162.     p=new ListNode;
  163.     cin>>p->data.num>>p->data.name>>p->data.phone;
  164.     InsertNode(head,p);
  165.     break;
  166. case 3:
  167.     cout<<"**************************"<<endl;
  168.       cout<<"* Node of Friends Book --Search *"<<endl;
  169.               cout<<"*********************************"<<endl;
  170.               p=ListFind(head);
  171.               if(p)
  172.               {
  173.                    cout<<" number(4) name(8) Phone(11) "<<endl;
  174.                     cout<<"*********************************"<<endl;
  175.                     cout<<setw(6)<<p->data.num <<setw(10)<<p->data.name<<setw(13)<<p->data.phone<<endl;/////////////////////
  176.                      cout<<"*********************************"<<endl;
  177.               }
  178.               else
  179.                 cout<<"Not Found\n\n";
  180.               break;
  181.         case 4:
  182.              cout<<"*********************************"<<endl;
  183.               cout<<"* Node of Friends Book --Delete *"<<endl;
  184.               cout<<"*********************************"<<endl;
  185.               DelNode(head);
  186.               break;
  187.         case 5:
  188.              cout<<"*********************************"<<endl;
  189.              cout<<"* LinkList of Friends Book --Show *"<<endl;
  190.               cout<<"*********************************"<<endl;
  191.               PrintLIst(head);////////////////////////
  192.               break;
  193.         case 6:
  194.             cout<<"\t Quit! \n";
  195.             DestroyList(head);
  196.             return 0;
  197.     }
  198.    }

  199. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-28 18:41:08 From FishC Mobile | 显示全部楼层
为什么一直显示42行错了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-12 15:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表