02.顺序表 排序--定义一个包含图书信息(书号、书名、价格)的顺序表读入相应的图书数据完成图书信息表的创建,然后将图书按照价格降序排序,逐行输出排序后每本图书的信息。

题目描述

定义一个包含图书信息(书号、书名、价格)的顺序表读入相应的图书数据完成图书信息表的创建,然后将图书按照价格降序排序,逐行输出排序后每本图书的信息。

输入

输入n+1行,前n行是n本图书的信息(书号、书名、价格),每本图书信息占一行,书号、书名、价格用空格分隔,价格之后没有空格。最后第n+1行是输入结束标志:0 0 0(空格分隔的三个0)。其中书号和书名为字符串类型,价格为浮点数类型。

输出

总计n行,每行是一本图书的信息(书号、书名、价格),书号、书名、价格用空格分隔。其中价格输出保留两位小数。

样例输入
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);
}

#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;
}
  • 4
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值