C++:基于顺序存储结构的图书信息表的排序
摸鱼~{:10_254:}代码如下:
/***
*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;
char name;
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,name;
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;
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 = 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.price<=L.elem.price)
{
Book B = L.elem;
L.elem = L.elem;
L.elem = B;
}
}
}
//输出
cout<<endl;
for (int k =0;k<n;k++)
{
cout<<L.elem.no<<""<<L.elem.name<<"";
printf("%.2f\n",L.elem.price);
}
return OK;
}
static/image/hrline/5.gif
运行结果如下:
收藏了。学学
页:
[1]