chan- 发表于 2022-1-22 11:27:49

请问大家,我在用链表写一个简单通讯录时遇到的错误

我把list的地址传入其他函数,结果是当我有先通过add函数输入信息后,在search中无论输入什么都进入for循环但是不能进入if语句并且程序自行结束,我感觉是 if(strcmp(name_search,(p->name))==0)出问题但是又不知道错在哪里,

然后我在add函数下面加了一段遍历并输出的代码结果是乱码,所以我想add函数在输入时应该也出了错误大家帮我看看可以嘛,十分感谢!
下面是代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

typedef struct txl
{
    char name;
    char number;
    int a;
    struct txl *next;
}Node;

typedef struct List
{
    Node *head;
}List;
void menu();
int main()
{
   List list;
   list.head=NULL;
   void add(List *plist_add);
   void search(List *plist_search);
   void print(List *plist_dis);
   int x;   //代表几号模式
   menu();
   scanf("%d",&x);
   while(x)
   {
       switch(x)
       {

         case 1: add(&list); break;
         case 2: search(&list); break;
          /* case 3: update(); break;
         case 4: del(); break;*/
         case 5: print(&list); break;
         default:break;
       }
       getch();
       menu();
       scanf("%d",&x);
   }
   return 0;
}

void menu()
{
    system("cls");
    printf("\n\n\n\n\n\n");
    printf("\t\t                  通讯录\n");
    printf("\t\t|--------------------菜单--------------------|\n");
    printf("\t\t|1.输入联系人信息                            |\n");
    printf("\t\t|2.查找联系人                              |\n");
    printf("\t\t|3.修改联系人信息                            |\n");
    printf("\t\t|4.删除联系人                              |\n");
    printf("\t\t|5.显示全部联系人                            |\n");
    printf("\t\t|--------------------------------------------|\n");
    printf("\t\t|请选择(1-5)");

}


void add(List *plist_add)
{
    char name_add;
    char number_add;

    printf("请输入新增联系人姓名:\n");
    scanf("%s",name_add);
    Node *p=(Node*)malloc(sizeof(Node));
    p->name=name_add;

    printf("请输入新增联系人号码:\n");
    scanf("%s",number_add);
    p=(Node*)malloc(sizeof(Node));
    p->number=number_add;

    Node *last=plist_add->head;
    if(last)
    {
      last=last->next;
    }
    else
    {
      plist_add->head=p;
    }
   printf("输入成功!\n");
   printf("按任意键返回主菜单\n");
   for(p=plist_add->head;p;p=p->next)
   {
         printf("%s - %s",p->name,p->number);
   }
}

void search(List *plist_search)
{
    char name_search;
    printf("请输入要查找的联系人\n");
    scanf("%s",name_search);
    Node *p;
    int isfound=0;
    for(p=plist_search->head;p;p=p->next)
    {
      printf("1");
      if(strcmp(name_search,(p->name))==0)
      {
            printf("该联系人的信息:\n");
            printf("%s",p->name);
            printf("%s",p->number);
            isfound=1;
      }
    }
    if(!isfound)
    {
      printf("查无此联系人\n");
    }
    printf("按任意键返回主菜单\n");
}

人造人 发表于 2022-1-22 13:26:08

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

typedef struct txl {
    char name;
    char number;
    int a;
    struct txl *next;
} Node;

typedef struct List {
    Node *head;
} List;

void menu();
void add(List * plist_add);
void search(List * plist_search);
void print(List * plist_dis);

// 这个函数一定要写
void free_list(List *list) {
    Node *i = list->head;
    while(i) {
      Node *temp = i;
      i = i->next;
      free(temp);
    }
}

int main() {
    List list = {.head = NULL};
    /*
    List list;
    list.head = NULL;
    */
    int x; //代表几号模式
    menu();
    scanf("%d", &x);
    while(x) {
      switch(x) {
      case 1:
            add(&list);
            break;
      case 2:
            search(&list);
            break;
            /* case 3: update(); break;
             case 4: del(); break;*/
            /*
      case 5:
            print(&list);
            break;
            */
      default:
            break;
      }

      // 没有这个函数
      //getch();
      getchar();
      menu();
      scanf("%d", &x);
    }
    free_list(&list);
    return 0;
}

void menu() {
    // 没有这个指令
    //system("cls");
    system("clear");
    printf("\n\n\n\n\n\n");
    printf("\t\t                  通讯录\n");
    printf("\t\t|--------------------菜单--------------------|\n");
    printf("\t\t|1.输入联系人信息                            |\n");
    printf("\t\t|2.查找联系人                              |\n");
    printf("\t\t|3.修改联系人信息                            |\n");
    printf("\t\t|4.删除联系人                              |\n");
    printf("\t\t|5.显示全部联系人                            |\n");
    printf("\t\t|--------------------------------------------|\n");
    printf("\t\t|请选择(1-5)");
}

void add(List *plist_add) {
    Node *p = malloc(sizeof(*p));
    printf("请输入新增联系人姓名:\n");
    scanf("%s", p->name);
    printf("请输入新增联系人号码:\n");
    scanf("%s", p->number);
    p->next = plist_add->head;
    plist_add->head = p;
#if 0
    char name_add;
    char number_add;

    printf("请输入新增联系人姓名:\n");
    scanf("%s", name_add);
    Node *p = (Node *)malloc(sizeof(Node));

    // 错
    //p->name = name_add;

    printf("请输入新增联系人号码:\n");
    scanf("%s", number_add);
    p = (Node *)malloc(sizeof(Node));

    // 错
    //p->number = number_add;

    Node *last = plist_add->head;
    if(last) {
      // 错
      last = last->next;
    } else {
      plist_add->head = p;
    }
    printf("输入成功!\n");
    printf("按任意键返回主菜单\n");
    for(p = plist_add->head; p; p = p->next) {
      printf("%s - %s", p->name, p->number);
    }
#endif
}

void search(List *plist_search) {
    char name_search;
    printf("请输入要查找的联系人\n");
    scanf("%s", name_search);
    Node *p;
    int isfound = 0;
    for(p = plist_search->head; p; p = p->next) {
      printf("1");
      // 为什么要加括号?
      //if(strcmp(name_search, (p->name)) == 0) {
      if(strcmp(name_search, p->name) == 0) {
            printf("该联系人的信息:\n");
            printf("%s", p->name);
            printf("%s", p->number);
            isfound = 1;
      }
    }
    if(!isfound) {
      printf("查无此联系人\n");
    }
    printf("按任意键返回主菜单\n");
    getchar();
}

傻眼貓咪 发表于 2022-1-22 15:21:24

楼主,如果你用的是 Linux 系统,是没有 system("cls")的:
Windows 是 system("cls")
Linux 是 system("clear")

我用 Windows,以下代码执行正常:#include <stdio.h>
#include <stdlib.h>

int main(){
        printf("hahahahahahaha");
        system("cls");
        printf("is clear");
        return 0;
}
页: [1]
查看完整版本: 请问大家,我在用链表写一个简单通讯录时遇到的错误