鱼C论坛

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

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

[复制链接]
发表于 2022-1-22 11:27:49 | 显示全部楼层 |阅读模式

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

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

x
我把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[20];
    char number[20];
    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[20];
    char number_add[20];

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

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

    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[20];
    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");
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-1-22 13:26:08 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>

  5. typedef struct txl {
  6.     char name[20];
  7.     char number[20];
  8.     int a;
  9.     struct txl *next;
  10. } Node;

  11. typedef struct List {
  12.     Node *head;
  13. } List;

  14. void menu();
  15. void add(List * plist_add);
  16. void search(List * plist_search);
  17. void print(List * plist_dis);

  18. // 这个函数一定要写
  19. void free_list(List *list) {
  20.     Node *i = list->head;
  21.     while(i) {
  22.         Node *temp = i;
  23.         i = i->next;
  24.         free(temp);
  25.     }
  26. }

  27. int main() {
  28.     List list = {.head = NULL};
  29.     /*
  30.     List list;
  31.     list.head = NULL;
  32.     */
  33.     int x; //代表几号模式
  34.     menu();
  35.     scanf("%d", &x);
  36.     while(x) {
  37.         switch(x) {
  38.         case 1:
  39.             add(&list);
  40.             break;
  41.         case 2:
  42.             search(&list);
  43.             break;
  44.             /* case 3: update(); break;
  45.              case 4: del(); break;  */
  46.             /*
  47.         case 5:
  48.             print(&list);
  49.             break;
  50.             */
  51.         default:
  52.             break;
  53.         }

  54.         // 没有这个函数
  55.         //getch();
  56.         getchar();
  57.         menu();
  58.         scanf("%d", &x);
  59.     }
  60.     free_list(&list);
  61.     return 0;
  62. }

  63. void menu() {
  64.     // 没有这个指令
  65.     //system("cls");
  66.     system("clear");
  67.     printf("\n\n\n\n\n\n");
  68.     printf("\t\t                    通讯录\n");
  69.     printf("\t\t|--------------------菜单--------------------|\n");
  70.     printf("\t\t|1.输入联系人信息                            |\n");
  71.     printf("\t\t|2.查找联系人                                |\n");
  72.     printf("\t\t|3.修改联系人信息                            |\n");
  73.     printf("\t\t|4.删除联系人                                |\n");
  74.     printf("\t\t|5.显示全部联系人                            |\n");
  75.     printf("\t\t|--------------------------------------------|\n");
  76.     printf("\t\t|请选择(1-5)");
  77. }

  78. void add(List *plist_add) {
  79.     Node *p = malloc(sizeof(*p));
  80.     printf("请输入新增联系人姓名:\n");
  81.     scanf("%s", p->name);
  82.     printf("请输入新增联系人号码:\n");
  83.     scanf("%s", p->number);
  84.     p->next = plist_add->head;
  85.     plist_add->head = p;
  86. #if 0
  87.     char name_add[20];
  88.     char number_add[20];

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

  92.     // 错
  93.     //p->name[20] = name_add[20];

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

  97.     // 错
  98.     //p->number[20] = number_add[20];

  99.     Node *last = plist_add->head;
  100.     if(last) {
  101.         // 错
  102.         last = last->next;
  103.     } else {
  104.         plist_add->head = p;
  105.     }
  106.     printf("输入成功!\n");
  107.     printf("按任意键返回主菜单\n");
  108.     for(p = plist_add->head; p; p = p->next) {
  109.         printf("%s - %s", p->name, p->number);
  110.     }
  111. #endif
  112. }

  113. void search(List *plist_search) {
  114.     char name_search[20];
  115.     printf("请输入要查找的联系人\n");
  116.     scanf("%s", name_search);
  117.     Node *p;
  118.     int isfound = 0;
  119.     for(p = plist_search->head; p; p = p->next) {
  120.         printf("1");
  121.         // 为什么要加括号?
  122.         //if(strcmp(name_search, (p->name)) == 0) {
  123.         if(strcmp(name_search, p->name) == 0) {
  124.             printf("该联系人的信息:\n");
  125.             printf("%s", p->name);
  126.             printf("%s", p->number);
  127.             isfound = 1;
  128.         }
  129.     }
  130.     if(!isfound) {
  131.         printf("查无此联系人\n");
  132.     }
  133.     printf("按任意键返回主菜单\n");
  134.     getchar();
  135. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-22 15:21:24 | 显示全部楼层
楼主,如果你用的是 Linux 系统,是没有 system("cls")的:
Windows 是 system("cls")
Linux 是 system("clear")

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

  3. int main(){
  4.         printf("hahahahahahaha");
  5.         system("cls");
  6.         printf("is clear");
  7.         return 0;
  8. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-25 01:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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