|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
摸鱼~
代码如下:
- /***
- *time:2021/9/30
- *author:LaoGu
- *purpose:基于顺序存储结构的图书信息表的排序
- **/
- #include<iostream>
- #include<string.h>
- using namespace std;
- #define OK 1
- #define ERROR 0
- #define OVERFLOW -2
- typedef int Status;
- #define MAXSIZE 10000
- typedef struct tagBook{
- char no[20];
- char name[50];
- float price;
- }Book;
- typedef struct tagSqList{
- Book *elem;
- int length;
- }SqList;
- int main(){
- //声明函数
- Status InitList_Sq(SqList &L);
- Status CreationList_Sq(SqList &L,char *no,char *name,float price);
- Status PrintList_Sq(SqList &L);
- SqList L; //定义L的类型是SqList
- InitList_Sq(L); //初始化L
- char no[20],name[50];
- float price;
- while(cin>>no>>name>>price){
- if(!strcmp(no,"0")&&!strcmp(name,"0")&&price==0.0)
- break;
- CreationList_Sq(L,no,name,price);
- }
- PrintList_Sq(L);
- return 0;
- }
- //初始化
- Status InitList_Sq(SqList &L){
- L.elem = new Book[MAXSIZE];
- if(!L.elem) exit(OVERFLOW);
- L.length =0;
- return OK;
- }
- //存储书籍
- Status CreationList_Sq(SqList &L,char *no,char *name,float price){
- Book B;
- strcpy(B.no,no);
- strcpy(B.name,name);
- B.price = price ;
- L.elem[L.length] = B;
- L.length++;
- return OK;
- }
- //根据价格从大到小排序且输出
- Status PrintList_Sq(SqList &L)
- {
- int n = L.length;
- //排序
- for(int i=0;i<=n;i++)
- {
- for (int j=0;j<=n;j++)
- {
- if(L.elem[j].price<=L.elem[j+1].price)
- {
- Book B = L.elem[j];
- L.elem[j] = L.elem[j+1];
- L.elem[j+1] = B;
- }
- }
- }
- //输出
- cout<<endl;
- for (int k =0;k<n;k++)
- {
- cout<<L.elem[k].no<<" "<<L.elem[k].name<<" ";
- printf("%.2f\n",L.elem[k].price);
- }
- return OK;
-
- }
复制代码
运行结果如下:
|
|