问题描述
我正在尝试做一个图书馆 书是目标 用户可以输入一本书,然后我检查这本书是否是我的书本菜单: 如果菜单中的书:
如果这本书可用,那么我打印一条消息并返回 1 如果它不可用,那么我将这本书更改为可用并打印我添加了这本书
如果书不在菜单中: 我做 malloc 然后检查我 malloc 是否成功 如果 malloc 成功:我对对象执行 strcp 如果 malloc 没有成功:我可以释放对象并打印一条消息并返回 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] = '\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;
}
}
}
}
}
解决方法
现在可能更好:
#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;
}
}
}
}
顺便说一句,你应该听取编译器警告。