c-图书库、struct和malloc



我正在尝试创建一个库书籍就是目标用户可以输入一本书,然后我检查该书是否是我的书菜单:如果菜单中的书本:

如果有书,我打印一条消息并返回1如果它不可用,所以我将书更改为可用并打印我添加的书

如果书不在菜单中:我执行malloc,然后检查我的malloc是否成功如果malloc成功:我对对象执行strcp如果malloc没有成功:我释放对象并打印一条消息并返回1个

问题:当用户第二次输入该书时,不应该将该书添加为新书!它应该检查这本书是否可用,然后返回一条消息,但我的代码没有这样做,我不知道哪里有bug!

#define _CRT_SECURE_NO_WARNINGS
#define BOOK_NUM  4
#define NAME_LENGTH 200
#define AVAILABLE 10
#define NOT_AVAILABLE 20
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct book
{
char name[NAME_LENGTH];  char author[NAME_LENGTH];  int available;  int times_borrowed;
}Book;
int main()
{
Book *books[BOOK_NUM] = { 0 };
char book_name[NAME_LENGTH];
char author_name[NAME_LENGTH];
int opreation = 0;
int i;
int j = 0;
int m = 0;
char tav;
scanf("%d", &opreation);
if (opreation == 1) {
printf("please enter the name:");
scanf("%c", &tav);
do {// kelet of the book_name
scanf("%c", &tav);
if (tav == 'n')
break;
book_name[m] = tav;
m++;
} while (m < NAME_LENGTH);
book_name[m] = '';
for (i = 0; i < BOOK_NUM && *(books+i)!=NULL ; i++) {
if (strcmp(*books[i]->name, book_name) == 0) 
{
if (books[i]->available = NOT_AVAILABLE)
{
books[i]->available = AVAILABLE;
printf("This book is already in the library");
return 0;
}
else
{
printf("There is no enough space in the library");
return 0;
} 

}
}  
//befot bs eza 3ml sreka ghad 3la kolshe w ma tghyr eshe 
for (j; j < BOOK_NUM; j++) {
if (books[j] == NULL)
{
books[j] = (Book*)malloc(sizeof(Book));
if (books[j] != NULL)
{
strcpy(books[j]->name, book_name);
printf("Please enter author name:");
m = 0;
do {// kelet of the book_name
scanf("%c", &tav);
if (tav == 'n')
break;
author_name[m] = tav;
m++;
} while (m < NAME_LENGTH);
author_name[m] = '';
strcpy(books[j]->author, author_name);
books[j]->available = AVAILABLE;
books[j]->times_borrowed = 0;
printf("The book %s was successfully added!", book_name);
return 0;
}
else
{
for (int k = 0; k < BOOK_NUM && books[k]!=NULL; k++) {
free(books[k]);
}
printf("NO MEMORY");
return 1;
}

}
}
} 
}

也许现在更好:

#define _CRT_SECURE_NO_WARNINGS
#define BOOK_NUM  4
#define NAME_LENGTH 200
#define AVAILABLE 10
#define NOT_AVAILABLE 20
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct book
{
char name[NAME_LENGTH];  char author[NAME_LENGTH];  int available;  int times_borrowed;
}Book;
int main()
{
Book *books[BOOK_NUM] = { 0 };
char book_name[NAME_LENGTH];
char author_name[NAME_LENGTH];
int opreation = 0;
int i;
int j = 0;
int m = 0;
char tav;
scanf("%d", &opreation);
if (opreation == 1) {
printf("please enter the name:");
scanf("%c", &tav);
do {// kelet of the book_name
scanf("%c", &tav);
if (tav == 'n')
break;
book_name[m] = tav;
m++;
} while (m < NAME_LENGTH);
book_name[m] = '';
for (i = 0; i < BOOK_NUM && *(books+i)!=NULL ; i++) {
if (strcmp(books[i]->name, book_name) == 0)
{
if (books[i]->available == NOT_AVAILABLE)
{
books[i]->available = AVAILABLE;
printf("This book is already in the library");
return 0;
}
else
{
printf("There is no enough space in the library");
return 0;
}
}
}
//befot bs eza 3ml sreka ghad 3la kolshe w ma tghyr eshe
if (books[j] == NULL)
{
books[j] = (Book*)malloc(sizeof(Book));
if (books[j] != NULL)
{
strcpy(books[j]->name, book_name);
printf("Please enter author name:");
m = 0;
do {// kelet of the book_name
scanf("%c", &tav);
if (tav == 'n')
break;
author_name[m] = tav;
m++;
} while (m < NAME_LENGTH);
author_name[m] = '';
strcpy(books[j]->author, author_name);
books[j]->available = AVAILABLE;
books[j]->times_borrowed = 0;
printf("The book %s was successfully added!", book_name);
return 0;
}
else
{
for (int k = 0; k < BOOK_NUM && books[k]!=NULL; k++) {
free(books[k]);
}
printf("NO MEMORY");
return 1;
}
}
}
}

顺便说一下,你应该听听编译器的警告。

最新更新