鱼C论坛

 找回密码
 立即注册
查看: 2998|回复: 1

[其他分类] C++:基于顺序存储结构的图书信息表的排序

[复制链接]
发表于 2021-9-30 13:36:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
摸鱼~

代码如下:
  1. /***
  2. *time:2021/9/30
  3. *author:LaoGu
  4. *purpose:基于顺序存储结构的图书信息表的排序
  5. **/
  6. #include<iostream>
  7. #include<string.h>

  8. using namespace std;

  9. #define OK 1
  10. #define ERROR 0
  11. #define OVERFLOW -2
  12. typedef int Status;
  13. #define MAXSIZE 10000

  14. typedef struct tagBook{
  15.   char no[20];
  16.   char name[50];
  17.   float price;
  18. }Book;

  19. typedef struct tagSqList{
  20.         Book *elem;
  21.         int length;
  22. }SqList;

  23. int main(){
  24.         //声明函数
  25.         Status InitList_Sq(SqList &L);
  26.         Status CreationList_Sq(SqList &L,char *no,char *name,float price);
  27.         Status PrintList_Sq(SqList &L);
  28.         SqList L;      //定义L的类型是SqList
  29.         InitList_Sq(L);  //初始化L
  30.         char no[20],name[50];
  31.         float price;
  32.         while(cin>>no>>name>>price){
  33.           if(!strcmp(no,"0")&&!strcmp(name,"0")&&price==0.0)
  34.                   break;
  35.      CreationList_Sq(L,no,name,price);
  36.         }
  37.         PrintList_Sq(L);
  38.         return 0;       
  39. }

  40. //初始化
  41. Status InitList_Sq(SqList &L){   
  42.         L.elem = new Book[MAXSIZE];
  43.         if(!L.elem) exit(OVERFLOW);
  44.         L.length =0;
  45.         return OK;  
  46. }

  47. //存储书籍
  48. Status CreationList_Sq(SqList &L,char *no,char *name,float price){
  49.   Book B;
  50.   strcpy(B.no,no);
  51.   strcpy(B.name,name);
  52.   B.price = price ;
  53.   L.elem[L.length] = B;
  54.   L.length++;
  55.   return OK;
  56. }

  57. //根据价格从大到小排序且输出
  58. Status PrintList_Sq(SqList &L)
  59. {
  60.         int n = L.length;
  61.         //排序
  62.         for(int i=0;i<=n;i++)
  63.         {
  64.                 for (int j=0;j<=n;j++)
  65.                 {
  66.                    if(L.elem[j].price<=L.elem[j+1].price)
  67.                    {
  68.                             Book B = L.elem[j];
  69.                                 L.elem[j] = L.elem[j+1];
  70.                                 L.elem[j+1] = B;
  71.                    }
  72.                 }
  73.         }
  74.         //输出
  75.         cout<<endl;
  76.         for (int k =0;k<n;k++)
  77.         {
  78.                 cout<<L.elem[k].no<<"  "<<L.elem[k].name<<"  ";
  79.                 printf("%.2f\n",L.elem[k].price);
  80.         }
  81.         return OK;
  82.        
  83. }
复制代码



                               
登录/注册后可看大图


运行结果如下:
1632970108(1).jpg
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-11-5 09:06:23 | 显示全部楼层
收藏了。学学
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-30 20:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表