|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
废话不多说,直接上代码
- /***
- *author:LaoGu
- *time:2021/9/30
- *purpose:基于顺序存储结构的图书信息表的最贵图书的查找
- ***/
- #include<iostream>
- #include<string.h>
- using namespace std;
- #define OK 1
- #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
- InitList_Sq(L); //初始化L
- char no[20],name[50];
- float price;
- while(1){
- 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,maxnum=0;
- SqList Bmax; //定义Bmax
- InitList_Sq(Bmax); //初始化Bmax
- float maxprice = L.elem[0].price;
- //找出最高的价格
- for(int i=1;i<N;i++)
- {
- if(maxprice<L.elem[i].price){
- maxprice = L.elem[i].price;
- }
- }
- //将书的价格与这个价格比较,将最贵的书籍的信息存入Bmax.elem中
- for(int j =0;j<N;j++){
- if(L.elem[j].price==maxprice){
- Bmax.elem[maxnum] = L.elem[j];
- maxnum++;
- }
- }
- //输出
- cout<<endl; //先空一行
- cout<<"maxnum: "<<maxnum<<endl; //输出最高的价格书的数量
- for (int k =0; k < maxnum;k++){
- cout<<Bmax.elem[k].no<<" "<<Bmax.elem[k].name<<" ";
- printf("%.2f\n",Bmax.elem[k].price);
- }
- return OK;
-
- }
复制代码
运行结果如下:
|
|