鱼C论坛

 找回密码
 立即注册
查看: 1640|回复: 1

判断的返回值,不管真假都是返回1

[复制链接]
发表于 2020-2-10 11:29:00 | 显示全部楼层 |阅读模式

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

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

x

  1. /*
  2. 内存池
  3. */
  4. #include<stdio.h>
  5. #include<stdlib.h>
  6. #include<string.h>


  7. struct Contact
  8. {
  9.         char name[128];
  10.         char num[128];
  11.         struct Contact *next;
  12. };
  13. void addPerson(struct Contact **head,int n);
  14. void findPerson(struct Contact *head,int n);
  15. void changePerson(struct Contact *head,int n);
  16. void delPerson(struct Contact **head,char *target);
  17. void displayContact(struct Contact *head,int n);
  18. void getinput(struct Contact *head);


  19. void getinput(struct Contact *head)
  20. {
  21.         printf("请输入联系人姓名:");
  22.         scanf("%s",head->name);
  23.         printf("\n请输入联系人电话号码:");
  24.         scanf("%s",head->num);
  25. }

  26. void addPerson(struct Contact **head,int n)
  27. {
  28.         struct Contact *current;
  29.         static struct Contact *tail;
  30.        
  31.         current = (struct Contact *)malloc(sizeof(struct Contact));
  32.         getinput(current);//填充节点信息
  33.        
  34.         if (*head != NULL)
  35.         {
  36.                 tail->next = current;
  37.                 current->next = NULL;
  38.         }
  39.         else//空链表,即第一次输入的时候
  40.         {
  41.                 *head = current;
  42.                 current->next = NULL;
  43.         }
  44.         tail = current;
  45. }

  46. void delPerson(struct Contact **head,char *target)
  47. {
  48.         struct Contact *current,*previous,*start;
  49.         previous = NULL;
  50.         current = *head;
  51.         start = *head;
  52.         int i,k;
  53.        
  54.         printf("\n目标:%s",target);
  55.         i = (target != current->name || target != current->num);
  56.         printf("\ni ===== %d\n",i);
  57.         if (*head != NULL)        //链表有节点
  58.         {
  59.                
  60.                 while(current != NULL && (target != current->name || target != current->num)) //current == NULL 或 匹配成功为假
  61.                 {
  62.                         //printf("1循环i = %d",i);
  63.                         previous = current;printf("成功1 \n");
  64.                         current = current->next;printf("成功2\n");
  65.                         //k = !(!(strcmp(target,current->name)) || !(strcmp(target,current->num)));printf("成功3");
  66.                 //        printf("\n2循环i = %d",i);
  67.                 }
  68.                
  69.                 if (current == NULL)  //找到最后还是没找到
  70.                 {
  71.                         printf("\n没有找到目标");
  72.                 }
  73.                 else
  74.                 {
  75.                         if (previous == NULL)        //只有一个节点
  76.                         {
  77.                                 *head = current->next;
  78.                                 printf("\n删除成功");
  79.                         }
  80.                         else
  81.                         {
  82.                                 previous->next = current->next;
  83.                                 printf("\n删除成功");
  84.                         }
  85.                 }
  86.                
  87.         }
  88.         else        //空链表
  89.         {
  90.                 printf("\n通讯录里没有成员");
  91.         }
  92. }

  93. int main (void)
  94. {
  95.         struct Contact *head = NULL;
  96.         int n = 0;
  97.        
  98.         while(1)
  99.         {
  100.                 printf("\n请输入对应的数字:1.插入 2.查找 3.更改 4.删除 5.显示所有 -1.退出\n");
  101.                 scanf("%d",&n);
  102.                
  103.                 if (n == 1)
  104.                 {
  105.                        
  106.                         addPerson(&head,n);printf("成功1 ");
  107.                         printf("\n添加成功");
  108.                
  109.                 }
  110.                 else if (n == 2)
  111.                 {
  112.                
  113.                 }
  114.                 else if (n == 3)
  115.                 {
  116.                
  117.                 }
  118.                 else if (n == 4)
  119.                 {
  120.                         char what[128];
  121.                         printf("请输入成员名称或电话:");
  122.                         scanf("%s",what);
  123.                        
  124.                         delPerson(&head,what);
  125.                        
  126.                 }
  127.                 else if (n == 5)
  128.                 {
  129.                
  130.                 }
  131.                 else if (n == -1)
  132.                 {
  133.                         break;
  134.                 }
  135.                 else
  136.                 {
  137.                        
  138.                 }
  139.         }
  140.        
  141.         return 0;
  142. }
复制代码


                             大家帮我看看第61行的 i 不管匹配不匹配它都是返回1.
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-2-10 15:34:06 | 显示全部楼层
第61行: i = (target != current->name || target != current->num);
target和current->name都是指针,而且target是临时变量,肯定和current->name 不相等。current->num也是类似。所以第61行一直为1是没有问题的。

正确的做法是比较其内容
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-15 11:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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