写一个图书管理系统 的C语言丹麦
时间: 2023-12-06 08:04:41 浏览: 33
很抱歉,我不会丹麦语,但我可以为您提供一个简单的图书管理系统的C语言代码示例。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 100
struct book {
char title[50];
char author[50];
int year;
};
struct library {
struct book books[MAX_BOOKS];
int num_books;
};
void add_book(struct library *lib) {
if (lib->num_books >= MAX_BOOKS) {
printf("The library is full.\n");
return;
}
printf("Enter the title: ");
scanf("%s", lib->books[lib->num_books].title);
printf("Enter the author: ");
scanf("%s", lib->books[lib->num_books].author);
printf("Enter the year of publication: ");
scanf("%d", &lib->books[lib->num_books].year);
lib->num_books++;
printf("Book added.\n");
}
void display_books(struct library *lib) {
if (lib->num_books == 0) {
printf("The library is empty.\n");
return;
}
printf("Books in the library:\n");
for (int i = 0; i < lib->num_books; i++) {
printf("%d. %s by %s (%d)\n", i+1, lib->books[i].title, lib->books[i].author, lib->books[i].year);
}
}
void save_books(struct library *lib, char *filename) {
FILE *fp;
fp = fopen(filename, "w");
if (fp == NULL) {
printf("Error: could not open file.\n");
return;
}
for (int i = 0; i < lib->num_books; i++) {
fprintf(fp, "%s,%s,%d\n", lib->books[i].title, lib->books[i].author, lib->books[i].year);
}
fclose(fp);
printf("Books saved to file.\n");
}
void load_books(struct library *lib, char *filename) {
FILE *fp;
fp = fopen(filename, "r");
if (fp == NULL) {
printf("Error: could not open file.\n");
return;
}
char line[100];
while (fgets(line, sizeof(line), fp)) {
char *title = strtok(line, ",");
char *author = strtok(NULL, ",");
char *year_str = strtok(NULL, ",");
int year = atoi(year_str);
strcpy(lib->books[lib->num_books].title, title);
strcpy(lib->books[lib->num_books].author, author);
lib->books[lib->num_books].year = year;
lib->num_books++;
}
fclose(fp);
printf("Books loaded from file.\n");
}
int main() {
struct library lib;
lib.num_books = 0;
int choice;
char filename[50];
printf("Welcome to the library!\n");
do {
printf("\nMenu:\n");
printf("1. Add book\n");
printf("2. Display books\n");
printf("3. Save books to file\n");
printf("4. Load books from file\n");
printf("5. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_book(&lib);
break;
case 2:
display_books(&lib);
break;
case 3:
printf("Enter the name of the file to save to: ");
scanf("%s", filename);
save_books(&lib, filename);
break;
case 4:
printf("Enter the name of the file to load from: ");
scanf("%s", filename);
load_books(&lib, filename);
break;
case 5:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice.\n");
break;
}
} while (choice != 5);
return 0;
}
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)