giegie666 发表于 2021-10-3 01:51:34

有人帮忙看看吗,一直运行不出来

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

using namespace std;

typedef struct
{
    char num;
    char name;
    char phone;
}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 Friends Book--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;
}

void CreateList(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 InsetNode(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 number 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 deleted!\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;
      }
    }
}

人造人 发表于 2021-10-3 09:06:27


你这代码能通过编译吗?

giegie666 发表于 2021-10-3 10:09:40

人造人 发表于 2021-10-3 09:06
你这代码能通过编译吗?

不能

人造人 发表于 2021-10-3 10:59:37

giegie666 发表于 2021-10-3 10:09
不能

写代码的时候认真一点,如果写代码不认真的话,那就检查代码的时候认真一点
这些语法错误是很简单的,是因为你没有认真检查代码?我认为是这样的
认真检查代码,这是你的工作吧

giegie666 发表于 2021-10-3 11:10:21

人造人 发表于 2021-10-3 10:59
写代码的时候认真一点,如果写代码不认真的话,那就检查代码的时候认真一点
这些语法错误是很简单的,是 ...

可我看源代码是这样的啊

人造人 发表于 2021-10-3 11:20:22

giegie666 发表于 2021-10-3 11:10
可我看源代码是这样的啊

真的认真的检查代码了?
下面这些错误为什么检查不出来呢?
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <string.h>

using namespace std;

typedef struct {
    char num;
    char name;
    char phone;
} 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 Friends Book--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) {
void PrintList(LinKlist head) {
    //LinkList p;
    LinKlist p;
    p = head->next;
    if(p) {
      cout << "number(4) name(8) Phone(11)" << endl;
      //cout << "---------------------------------<<" 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;
}

//void CreateList(LinkList &head) {
void CreateList(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 InsetNode(LinkList &head, ListNode *p) {
//void InsetNode(LinKlist &head, ListNode *p) {
void InsertNode(LinKlist &head, ListNode *p) {
    ListNode *p1, *p2;
    p1 = head;
    p2 = p1->next;
    //while(p2 &&(strcmp(p2->data.num,p->data.num)<0){
    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) {
ListNode *ListFind(LinKlist head) {
    //LinkList p;
    LinKlist p;
    /*
    char num(5);
    char name(9);
    */
    char num;
    char name;
    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)
      if(p == NULL || strcmp(p->data.num, num) != 0)
            p = NULL;
    } else if(selnum == 2) {
      cout << "Please enter the name number for searching: ";
      cin >> name;
      //while(P && strcmp(p->data.name, name) != 0)
      while(p && strcmp(p->data.name, name) != 0)
            p = p->next;
    }
    return p;
}

//void DelNode(LinkList head) {
void DelNode(LinKlist head) {
    char ch;
    //LinkList p, q;
    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 deleted!\n\n";
      PrintList(head);
    }
}

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

int main() {
    //LinkList head, p;
    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;
            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;
                     << 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;
      }
    }
}

giegie666 发表于 2021-10-3 12:55:40

知道了,太粗心(д;),谢谢啦
页: [1]
查看完整版本: 有人帮忙看看吗,一直运行不出来