一个语句的理解问题
本帖最后由 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 = bookbook = NULL
strcut Book *searchBooks(*library, char *target) *book strcmp
insertBooks(**library,int value) *previous *new *current count
deleteBooks()
*/
struct Book
{
char title;
char author;
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;
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;
} 有没有叹号,是完全相反的意思,怎么能是一个意思呢 !和取反差不多。它是不是的意思。感叹号表明,它的语句中的结果不正确。也就是说strcmp返回结果不为真,即为0的时候。或另一个,结果为0的时候。即,它们两个其中一个为0,就为真,if语句就执行了。就是说,里面的字符串相等就执行了。如果不加感叹号就是相反的意思。即,字符串不相等为真。if执行。我可能说反了。因为汇编中cmp指令是相等返回0.这个strcmp我不知道返回谁。反正感叹号是相反的。 wp231957 发表于 2020-6-12 20:11
有没有叹号,是完全相反的意思,怎么能是一个意思呢
但是我只要得出if(1)就可以了,我觉得不用! ,正常执行也能得出1 逻辑语句||中,只要有一个被满足,结果为真, 就不用再继续判断了
页:
[1]