我试着做一个图书馆,图书是用户可以输入的目标,然后我检查书是否是我的图书菜单:,如果在菜单中的书:
如果这本书是可用的,那么我打印一条消息,如果没有,我会返回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] = '\0';
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] = '\0';
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;
}
}
}
}
}发布于 2020-12-24 01:15:34
也许现在更好:
#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] = '\0';
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] = '\0';
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;
}
}
}
}顺便说一句,你应该听听编译器的警告。
https://stackoverflow.com/questions/65428262
复制相似问题