鱼C论坛

 找回密码
 立即注册
查看: 2592|回复: 4

一个语句的理解问题

[复制链接]
发表于 2020-6-12 17:16:57 | 显示全部楼层 |阅读模式
10鱼币
本帖最后由 Justin1 于 2020-6-12 17:16 编辑
  1. if (!strcmp(book->title, target) || !strcmp(book->author, target))
复制代码


  1. if (strcmp(book->title, target) || strcmp(book->author, target))
复制代码

这两个的比较

这个句子目标是找到book 结构体里面 有没有和target相同的内容。 有的话进行if语句内内容
我知道这个语句的意思,比较title,target  和 author,target 也知道if(1)的情况下后面内容执行

但是对于第二种,我认为实现手段是一样的。 不需要用!  当用strcmp比较找到一样的内容时 类型应该是 if( 0 || x)  x不可能为0, 所以返回值也是1 也就是 if(1)

对于这个程序  去掉!结果却不一样,所以有些不理解

修改代码在第99行, 去掉!就可以运行了。


  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>


  4. /* functions:
  5. First:  main()  addBooks()  getInput() print()  release()

  6. main()  struct Book *library = NULL

  7. addBooks( s B **library) struct Book *book,*temp
  8. getInput(s B *library)

  9. Print(library) struct Book *book

  10. release()  temp = library library = library-next free(temp)

  11. addBooks2() *book static s B *tail    tail-next = book  book = NULL

  12. strcut Book *searchBooks(*library, char *target) *book    strcmp

  13. insertBooks(**library,int value) *previous *new *current count

  14. deleteBooks()

  15. */
  16. struct Book
  17. {
  18.         char title[128];
  19.         char author[128];
  20.         struct Book* next;
  21. };

  22. void addBooks(struct Book** library);
  23. void getInput(struct Book* library);
  24. void printBooks(struct Book* library);
  25. void release(struct Book* library);
  26. struct Book* searchBooks(struct Book* library, char* target);

  27. void addBooks(struct Book** library)
  28. {
  29.         struct Book* book,*temp;

  30.         book = (struct Book*)malloc(sizeof(struct Book));
  31.         if (book == NULL)
  32.         {
  33.                 printf("分配内存失败!\n");
  34.                 exit(1);
  35.         }

  36.         getInput(book);

  37.         temp = *library;
  38.         *library = book;
  39.         book->next = temp;

  40. }

  41. void getInput(struct Book* book)
  42. {
  43.         printf("请输入书名:");
  44.         scanf("%s", book->title);
  45.         printf("请输入作者名:");
  46.         scanf("%s", book->author);
  47. }

  48. void printBooks(struct Book* library)
  49. {
  50.         struct Book* book;
  51.         book = library;

  52.         while (book != NULL)
  53.         {
  54.                 printf("书名:%s\n", book->title);
  55.                 printf("作者名:%s\n", book->author);
  56.                 book = book->next;
  57.         }
  58. }

  59. void release(struct Book* library)
  60. {
  61.         struct Book* temp;
  62.        
  63.         while (library != NULL)
  64.         {
  65.                 temp = library;
  66.                 library = library->next;
  67.                 free(temp);
  68.         }

  69. }

  70. struct Book *searchBooks(struct Book* library, char* target)
  71. {
  72.         struct Book* book;
  73.         book = library;
  74.         while (book != NULL)
  75.         {
  76.                 if (!strcmp(book->title, target) || !strcmp(book->author, target))
  77.                 {
  78.                         break;
  79.                 }
  80.                 book = book->next;
  81.         }

  82.         return book;
  83. }

  84. int main()
  85. {
  86.         struct Book* library = NULL;
  87.         struct Book* book = NULL;
  88.         char ch;
  89.         char target[128];

  90.         while (1)
  91.         {
  92.                 printf("是否要录入书籍(Y/N)");
  93.                 do
  94.                 {
  95.                         ch = getchar();
  96.                 } while (ch != 'Y' && ch != 'N');

  97.                 if (ch == 'Y')
  98.                 {
  99.                         addBooks(&library);
  100.                 }
  101.                 else if (ch == 'N')
  102.                 {
  103.                         break;
  104.                 }
  105.         }
  106.         printf("书籍录入完毕!\n");

  107.         printf("是否要打印书籍(Y/N)");
  108.         do
  109.         {
  110.                 ch = getchar();
  111.         } while (ch != 'Y' && ch != 'N');

  112.         if (ch == 'Y')
  113.         {
  114.                 printBooks(library);
  115.         }

  116.         printf("书籍打入完毕!\n");
  117.         putchar('\n');

  118.         while (1)
  119.         {
  120.                 printf("是否要搜索书籍(Y/N)");
  121.                 do
  122.                 {
  123.                         ch= getchar();
  124.                 } while (ch != 'Y' && ch != 'N');

  125.                 if (ch == 'Y')
  126.                 {
  127.                         printf("请输入你检索的内容!\n");
  128.                         scanf("%s", target);
  129.                         book = searchBooks(library,target);
  130.                         if (book == NULL)
  131.                         {
  132.                                 printf("很抱歉,未检索到你输入的信息\n");
  133.                         }
  134.                         else
  135.                         {
  136.                                 printf("找到相关书籍!\n");
  137.                                 do
  138.                                 {
  139.                                         printf("书名:%s\n",book->title);
  140.                                         printf("作者名:%s\n", book->author);
  141.                                 } while ((book  = searchBooks(book->next,target) ) != NULL);
  142.                         }
  143.                 }
  144.                 else if (ch == 'N')
  145.                 {
  146.                         break;
  147.                 }
  148.         }
  149.         printf("书籍搜索完毕!\n");


  150.         release(library);

  151.         return 0;
  152. }
复制代码

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

使用道具 举报

发表于 2020-6-12 20:11:20 From FishC Mobile | 显示全部楼层
有没有叹号,是完全相反的意思,怎么能是一个意思呢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-6-12 21:12:42 | 显示全部楼层
!和取反差不多。它是不是的意思。感叹号表明,它的语句中的结果不正确。也就是说strcmp返回结果不为真,即为0的时候。或另一个,结果为0的时候。即,它们两个其中一个为0,就为真,if语句就执行了。就是说,里面的字符串相等就执行了。如果不加感叹号就是相反的意思。即,字符串不相等为真。if执行。我可能说反了。因为汇编中cmp指令是相等返回0.这个strcmp我不知道返回谁。反正感叹号是相反的。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-6-13 08:03:03 | 显示全部楼层
wp231957 发表于 2020-6-12 20:11
有没有叹号,是完全相反的意思,怎么能是一个意思呢

但是我只要得出if(1)就可以了,我觉得不用! ,正常执行也能得出1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-6-16 21:36:20 | 显示全部楼层
逻辑语句  ||  中,  只要有一个被满足,结果为真, 就不用再继续判断了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-12 14:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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