题目描述
定义一个包含图书信息(书号、书名、价格)的顺序表读入相应的图书数据完成图书信息表的创建,然后将图书按照价格降序排序,逐行输出排序后每本图书的信息。
输入
输入n+1行,前n行是n本图书的信息(书号、书名、价格),每本图书信息占一行,书号、书名、价格用空格分隔,价格之后没有空格。最后第n+1行是输入结束标志:0 0 0(空格分隔的三个0)。其中书号和书名为字符串类型,价格为浮点数类型。
输出
总计n行,每行是一本图书的信息(书号、书名、价格),书号、书名、价格用空格分隔。其中价格输出保留两位小数。文章来源:https://www.toymoban.com/news/detail-729588.html
样例输入
9787302257646 Data-Structure 35.00
9787302164340 Operating-System 50.00
9787302219972 Software-Engineer 32.00
9787302203513 Database-Principles 36.00
9787810827430 Discrete-Mathematics 36.00
9787302257800 Data-Structure 62.00
9787811234923 Compiler-Principles 62.00
9787822234110 The-C-Programming-Language 38.00
0 0 0
样例输出
9787302257800 Data-Structure 62.00
9787811234923 Compiler-Principles 62.00
9787302164340 Operating-System 50.00
9787822234110 The-C-Programming-Language 38.00
9787302203513 Database-Principles 36.00
9787810827430 Discrete-Mathematics 36.00
9787302257646 Data-Structure 35.00
9787302219972 Software-Engineer 32.00
AC代码
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
typedef struct {
string name;
float price;
string id;
} Book;
typedef struct {
int length;
Book *elem;
}Sqlist;
void Init(Sqlist &l) {
l.elem = new Book[1000];
if (!l.elem)
exit(1);
l.length = 0;
}
void Insert(Sqlist &l) {
for (int i = 0; i < 1000; i++) {
cin >> l.elem[i].id >> l.elem[i].name >> l.elem[i].price;
l.length++;
if (l.elem[i].id == "0" && l.elem[i].name == "0" && l.elem[i].price == 0) {
l.length--;
return;
}
}
}
void Sort(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;
}
}
}
}
void Print(Sqlist l) {
//cout << l.length << endl;先注释了,不然格式不对
for (int i = 0; i < l.length; i++)
printf("%s %s %.2f\n", l.elem[i].id.c_str(), l.elem[i].name.c_str(), l.elem[i].price);
}
int main() {
Sqlist l;
Init(l);
Insert(l);
Sort(l);
Print(l);
}
二文章来源地址https://www.toymoban.com/news/detail-729588.html
#include <bits/stdc++.h>
#define Maxsize 1000
using namespace std;
struct Book {
string no;
string name;
double price;
};
int main() {
int i = 0;
struct Book book[Maxsize];
while (1) {
cin >> book[i].no >> book[i].name >> book[i].price;
if (book[i].no == "0" && book[i].name == "0" && book[i].price == 0)
break;
++i;
}
struct Book bookcmp;
for (int a = 0; a < i - 1; a++) {
for (int b = a + 1; b < i; b++) {
if (book[a].price < book[b].price) {
bookcmp = book[a];
book[a] = book[b];
book[b] = bookcmp;
}
if (book[a].price == book[b].price) {
if (book[a].no > book[b].no) {
bookcmp = book[a];
book[a] = book[b];
book[b] = bookcmp;
}
}
}
}
for (int j = 0; j < i; j++)
cout << book[j].no << " " << book[j].name << " " << fixed << setprecision(2) << book[j].price << endl;
return 0;
}
到了这里,关于02.顺序表 排序--定义一个包含图书信息(书号、书名、价格)的顺序表读入相应的图书数据完成图书信息表的创建,然后将图书按照价格降序排序,逐行输出排序后每本图书的信息。的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!