c语言 printbook(book)
时间: 2024-10-18 20:04:39 浏览: 74
在C语言中,"printbook(book)"通常表示打印书籍的功能,但这并不是一个内置函数,因为C标准库并没有提供直接用于打印书籍内容的函数。如果你想模拟这个功能,你需要创建一个自定义函数,它接收一个指向书籍结构体(如struct Book* book)的指针,并通过该指针访问书名、作者、章节等信息,然后使用printf或其他输出函数将这些信息显示出来。
例如,假设有一个简单的书籍结构体定义如下:
```c
#include <stdio.h>
typedef struct {
char title[50];
char author[50];
int chapters;
} Book;
void printBook(Book *book) {
printf("书名: %s\n", book->title);
printf("作者: %s\n", book->author);
printf("章节数: %d\n", book->chapters);
}
```
你可以像下面这样使用这个函数:
```c
int main() {
Book myBook = {"计算机程序设计艺术", "Donald E. Knuth", 3};
printBook(&myBook);
return 0;
}
```
在这个例子中,`printBook`函数就是实现了打印书籍信息的功能。
相关问题
以下程序的输出结果是()。 fo = open( " book. txt" , " w") ls = [C语言', 'Java' , 'C#' , 'Python'] fo.writelines(ls) fo.close() A C语言"Java"C#'Python' B C语言, Java,C#,Python C [C语言,Java,C#,Python] D [C语言', 'Java' , 'C#' , 'Python']
选项 D:`[C语言', 'Java' , 'C#' , 'Python']`。
解析:
程序首先以写入模式打开文件 `book.txt`,然后将字符串列表 `ls` 中的元素写入文件。由于使用的是 `writelines` 方法,因此写入的每个元素不会自动添加换行符。最后关闭文件。因此,输出结果为字符串列表 `ls`。
完整的程序如下:
```python
fo = open("book.txt", "w")
ls = ['C语言', 'Java', 'C#', 'Python']
fo.writelines(ls)
fo.close()
print(ls)
# 输出:['C语言', 'Java', 'C#', 'Python']
```
图书管理系统c语言
图书管理系统可以分为两个部分:前端与后端。前端即用户交互界面,后端则负责数据管理。
以下是一个简单的图书管理系统的C语言代码示例:
前端:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 100
typedef struct Book {
char title[100];
char author[50];
char publisher[50];
int year;
} Book;
void print_menu() {
printf("\n");
printf("1. Add book\n");
printf("2. Remove book\n");
printf("3. Print books\n");
printf("4. Exit\n");
printf("\n");
}
void add_book(Book books[], int *num_books) {
if (*num_books == MAX_BOOKS) {
printf("Error: Maximum number of books reached.\n");
return;
}
Book book;
printf("Title: ");
fgets(book.title, 100, stdin);
book.title[strcspn(book.title, "\n")] = 0; // remove trailing newline
printf("Author: ");
fgets(book.author, 50, stdin);
book.author[strcspn(book.author, "\n")] = 0; // remove trailing newline
printf("Publisher: ");
fgets(book.publisher, 50, stdin);
book.publisher[strcspn(book.publisher, "\n")] = 0; // remove trailing newline
printf("Year: ");
scanf("%d", &book.year);
getchar(); // remove trailing newline
books[*num_books] = book;
(*num_books)++;
}
void remove_book(Book books[], int *num_books) {
if (*num_books == 0) {
printf("Error: No books to remove.\n");
return;
}
char title[100];
printf("Title: ");
fgets(title, 100, stdin);
title[strcspn(title, "\n")] = 0; // remove trailing newline
int i;
for (i = 0; i < *num_books; i++) {
if (strcmp(books[i].title, title) == 0) {
break;
}
}
if (i == *num_books) {
printf("Error: Book not found.\n");
} else {
for (int j = i; j < *num_books - 1; j++) {
books[j] = books[j + 1];
}
(*num_books)--;
}
}
void print_books(Book books[], int num_books) {
printf("%-50s%-20s%-20s%-10s\n", "Title", "Author", "Publisher", "Year");
for (int i = 0; i < num_books; i++) {
printf("%-50s%-20s%-20s%-10d\n", books[i].title, books[i].author, books[i].publisher, books[i].year);
}
}
int main() {
Book books[MAX_BOOKS];
int num_books = 0;
while (1) {
print_menu();
int choice;
printf("Choice: ");
scanf("%d", &choice);
getchar(); // remove trailing newline
switch (choice) {
case 1:
add_book(books, &num_books);
break;
case 2:
remove_book(books, &num_books);
break;
case 3:
print_books(books, num_books);
break;
case 4:
return 0;
default:
printf("Error: Invalid choice.\n");
}
}
}
```
后端:
由于数据存储方式有多种,这里只提供一种简单的实现方式,即使用文件存储数据。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 100
typedef struct Book {
char title[100];
char author[50];
char publisher[50];
int year;
} Book;
void add_book(Book book) {
FILE *file = fopen("books.dat", "ab");
if (file == NULL) {
printf("Error: Could not open file.\n");
return;
}
fwrite(&book, sizeof(Book), 1, file);
fclose(file);
}
void remove_book(char *title) {
FILE *file = fopen("books.dat", "rb");
if (file == NULL) {
printf("Error: Could not open file.\n");
return;
}
Book book;
int found = 0;
FILE *temp = fopen("temp.dat", "wb");
while (fread(&book, sizeof(Book), 1, file) == 1) {
if (strcmp(book.title, title) != 0) {
fwrite(&book, sizeof(Book), 1, temp);
} else {
found = 1;
}
}
fclose(file);
fclose(temp);
remove("books.dat");
rename("temp.dat", "books.dat");
if (!found) {
printf("Error: Book not found.\n");
}
}
void print_books() {
FILE *file = fopen("books.dat", "rb");
if (file == NULL) {
printf("Error: Could not open file.\n");
return;
}
Book book;
printf("%-50s%-20s%-20s%-10s\n", "Title", "Author", "Publisher", "Year");
while (fread(&book, sizeof(Book), 1, file) == 1) {
printf("%-50s%-20s%-20s%-10d\n", book.title, book.author, book.publisher, book.year);
}
fclose(file);
}
int main() {
while (1) {
printf("\n");
printf("1. Add book\n");
printf("2. Remove book\n");
printf("3. Print books\n");
printf("4. Exit\n");
printf("\n");
int choice;
printf("Choice: ");
scanf("%d", &choice);
getchar(); // remove trailing newline
switch (choice) {
case 1: {
Book book;
printf("Title: ");
fgets(book.title, 100, stdin);
book.title[strcspn(book.title, "\n")] = 0; // remove trailing newline
printf("Author: ");
fgets(book.author, 50, stdin);
book.author[strcspn(book.author, "\n")] = 0; // remove trailing newline
printf("Publisher: ");
fgets(book.publisher, 50, stdin);
book.publisher[strcspn(book.publisher, "\n")] = 0; // remove trailing newline
printf("Year: ");
scanf("%d", &book.year);
getchar(); // remove trailing newline
add_book(book);
break;
}
case 2: {
char title[100];
printf("Title: ");
fgets(title, 100, stdin);
title[strcspn(title, "\n")] = 0; // remove trailing newline
remove_book(title);
break;
}
case 3:
print_books();
break;
case 4:
return 0;
default:
printf("Error: Invalid choice.\n");
}
}
}
```
这只是一个简单的示例,实际上图书管理系统还有很多细节需要考虑,比如数据校验、数据查询、数据备份等等。
阅读全文