用c语言写一个图书信息管理系统包含书号书名和价格
时间: 2023-04-01 14:00:48 浏览: 148
好的,我可以回答这个问题。以下是一个简单的图书信息管理系统的 C 语言代码示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 100
struct Book {
char number[20];
char title[100];
float price;
};
struct Book books[MAX_BOOKS];
int num_books = ;
void add_book() {
if (num_books >= MAX_BOOKS) {
printf("Error: too many books.\n");
return;
}
struct Book book;
printf("Enter book number: ");
scanf("%s", book.number);
printf("Enter book title: ");
scanf("%s", book.title);
printf("Enter book price: ");
scanf("%f", &book.price);
books[num_books++] = book;
printf("Book added successfully.\n");
}
void list_books() {
if (num_books == ) {
printf("No books in the system.\n");
return;
}
printf("Number\tTitle\tPrice\n");
for (int i = ; i < num_books; i++) {
printf("%s\t%s\t%.2f\n", books[i].number, books[i].title, books[i].price);
}
}
int main() {
while (1) {
printf("1. Add book\n");
printf("2. List books\n");
printf("3. Quit\n");
printf("Enter your choice: ");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
add_book();
break;
case 2:
list_books();
break;
case 3:
printf("Goodbye!\n");
exit();
default:
printf("Invalid choice.\n");
}
}
return ;
}
阅读全文