|
10鱼币
本帖最后由 Justin1 于 2020-6-12 17:16 编辑
- if (!strcmp(book->title, target) || !strcmp(book->author, target))
复制代码
- 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行, 去掉!就可以运行了。
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- /* functions:
- First: main() addBooks() getInput() print() release()
- main() struct Book *library = NULL
- addBooks( s B **library) struct Book *book,*temp
- getInput(s B *library)
- Print(library) struct Book *book
- release() temp = library library = library-next free(temp)
- addBooks2() *book static s B *tail tail-next = book book = NULL
- strcut Book *searchBooks(*library, char *target) *book strcmp
- insertBooks(**library,int value) *previous *new *current count
- deleteBooks()
- */
- struct Book
- {
- char title[128];
- char author[128];
- struct Book* next;
- };
- void addBooks(struct Book** library);
- void getInput(struct Book* library);
- void printBooks(struct Book* library);
- void release(struct Book* library);
- struct Book* searchBooks(struct Book* library, char* target);
- void addBooks(struct Book** library)
- {
- struct Book* book,*temp;
- book = (struct Book*)malloc(sizeof(struct Book));
- if (book == NULL)
- {
- printf("分配内存失败!\n");
- exit(1);
- }
- getInput(book);
- temp = *library;
- *library = book;
- book->next = temp;
- }
- void getInput(struct Book* book)
- {
- printf("请输入书名:");
- scanf("%s", book->title);
- printf("请输入作者名:");
- scanf("%s", book->author);
- }
- void printBooks(struct Book* library)
- {
- struct Book* book;
- book = library;
- while (book != NULL)
- {
- printf("书名:%s\n", book->title);
- printf("作者名:%s\n", book->author);
- book = book->next;
- }
- }
- void release(struct Book* library)
- {
- struct Book* temp;
-
- while (library != NULL)
- {
- temp = library;
- library = library->next;
- free(temp);
- }
- }
- struct Book *searchBooks(struct Book* library, char* target)
- {
- struct Book* book;
- book = library;
- while (book != NULL)
- {
- if (!strcmp(book->title, target) || !strcmp(book->author, target))
- {
- break;
- }
- book = book->next;
- }
- return book;
- }
- int main()
- {
- struct Book* library = NULL;
- struct Book* book = NULL;
- char ch;
- char target[128];
- while (1)
- {
- printf("是否要录入书籍(Y/N)");
- do
- {
- ch = getchar();
- } while (ch != 'Y' && ch != 'N');
- if (ch == 'Y')
- {
- addBooks(&library);
- }
- else if (ch == 'N')
- {
- break;
- }
- }
- printf("书籍录入完毕!\n");
- printf("是否要打印书籍(Y/N)");
- do
- {
- ch = getchar();
- } while (ch != 'Y' && ch != 'N');
- if (ch == 'Y')
- {
- printBooks(library);
- }
- printf("书籍打入完毕!\n");
- putchar('\n');
- while (1)
- {
- printf("是否要搜索书籍(Y/N)");
- do
- {
- ch= getchar();
- } while (ch != 'Y' && ch != 'N');
- if (ch == 'Y')
- {
- printf("请输入你检索的内容!\n");
- scanf("%s", target);
- book = searchBooks(library,target);
- if (book == NULL)
- {
- printf("很抱歉,未检索到你输入的信息\n");
- }
- else
- {
- printf("找到相关书籍!\n");
- do
- {
- printf("书名:%s\n",book->title);
- printf("作者名:%s\n", book->author);
- } while ((book = searchBooks(book->next,target) ) != NULL);
- }
- }
- else if (ch == 'N')
- {
- break;
- }
- }
- printf("书籍搜索完毕!\n");
- release(library);
- return 0;
- }
复制代码 |
|