当然可以!下面是一个简单的C语言图书管理系统的代码示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 100
typedef struct {
char title[100];
char author[100];
int year;
} Book;
Book library[MAX_BOOKS];
int numBooks = 0;
void addBook() {
if (numBooks == MAX_BOOKS) {
printf("Sorry, the library is full.\n");
return;
}
Book newBook;
printf("Enter book title: ");
fgets(newBook.title, sizeof(newBook.title), stdin);
printf("Enter author: ");
fgets(newBook.author, sizeof(newBook.author), stdin);
printf("Enter publication year: ");
scanf("%d", &newBook.year);
// 清除输入缓冲区
int c;
while ((c = getchar()) != '\n' && c != EOF) {}
library[numBooks++] = newBook;
printf("Book added successfully!\n");
}
void displayBooks() {
if (numBooks == 0) {
printf("The library is empty.\n");
return;
}
printf("Books in the library:\n");
for (int i = 0; i < numBooks; i++) {
printf("%d. %s by %s (%d)\n", i+1, library[i].title, library[i].author, library[i].year);
}
}
int main() {
int choice;
do {
printf("Library Management System\n");
printf("1. Add a book\n");
printf("2. Display all books\n");
printf("3. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
// 清除输入缓冲区
int c;
while ((c = getchar()) != '\n' && c != EOF) {}
switch (choice) {
case 1:
addBook();
break;
case 2:
displayBooks();
break;
case 3:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice, please try again.\n");
}
printf("\n");
} while (choice != 3);
return 0;
}
这个简单的图书管理系统允许你添加图书,并显示所有已添加的图书。你可以根据自己的需求对其进行扩展,比如实现删除图书、搜索图书等功能。
希望这个程序能够帮助你入门C语言编程,如果你有任何问题,请随时提问!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |