#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 1000
typedef struct {
char no[20];
char name[60];
float price;
} Book;
typedef struct {
Book *elem;
int length;
} SqList;
int BubbleSort_Sq(SqList L) {
for (int i = 0; i < L.length - 1; i++) {
for (int j = 0; j < L.length - i - 1; j++) {
if (L.elem[j].price < L.elem[j + 1].price) {
Book book = L.elem[j];
L.elem[j] = L.elem[j + 1];
L.elem[j + 1] = book;
}
}
}
return OK;
}
int main() {
SqList bookList;
int i = 0;
int maxBooks = MAXSIZE; // Define a maximum number of books to avoid overflows
bookList.elem = (Book *)malloc(maxBooks * sizeof(Book));
bookList.length = 0; // Initialize the length to 0
char input[20]; // Buffer to store input
while (1) {
if (i >= maxBooks) {
printf("已达到书的最大数量,无法继续添加。\n");
break;
}
// Read input without prompts
scanf("%s", input);
// Check if input is '0 0 0'
if (strcmp(input, "0") == 0) {
break; // Input is '0', so stop
}
// Store input in bookList
strcpy(bookList.elem[i].no, input);
scanf("%s%f", bookList.elem[i].name, &bookList.elem[i].price);
i++;
}
bookList.length = i; // Update the actual number of books entered
// Sort the bookList
BubbleSort_Sq(bookList);
// Save the sorted list to out.txt
FILE *file = fopen("out.txt", "w");
if (file == NULL) {
printf("打开文件时出错!");
return ERROR;
}
for (i = 0; i < bookList.length; i++) {
fprintf(file, "ISBN: %s, 书名: %s, 价格: %.2f\n", bookList.elem[i].no, bookList.elem[i].name, bookList.elem[i].price);
}
fclose(file);
// Display the sorted list in the console
printf("\n书籍的排序列表:\n");
for (i = 0; i < bookList.length; i++) {
printf("ISBN: %s, 书名: %s, 价格: %.2f\n", bookList.elem[i].no, bookList.elem[i].name, bookList.elem[i].price);
}
free(bookList.elem);
return 0;
}

刚好五个字575
- 粉丝: 842
最新资源
- 2023年数字图像处理实验报告完整版.doc
- 地下敷设通信光缆线路施工组织设计.pdf
- 2023年单片机AD模数转换实验报告.doc
- SMART编程基础.ppt
- DB2104_T_0001.11_2019_满族特色菜_八碟八碗_第11部分_大碗肉.pdf
- 常用中文数据库.ppt
- PIC的C语言使用.doc
- 2023年网络营销资料网络营销知识点.doc
- cad学习心得体会.docx
- GIS设备知识.ppt
- 2023年数据库原理与设计题库.doc
- C++运算符优先级.docx
- 2023年咨询师继续教育工程项目管理答案.docx
- 第十三章卫生项目管理.pptx
- 2023年综合布线实验实验报告.doc
- 大学生心理健康教育-第八章-大学生网络心理健康.ppt
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


