C++:基于顺序存储结构的图书信息表的最贵图书的查找
废话不多说,直接上代码{:10_282:}static/image/hrline/5.gif
/***
*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;
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
InitList_Sq(L); //初始化L
char no,name;
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;
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,maxnum=0;
SqList Bmax; //定义Bmax
InitList_Sq(Bmax); //初始化Bmax
float maxprice = L.elem.price;
//找出最高的价格
for(int i=1;i<N;i++)
{
if(maxprice<L.elem.price){
maxprice = L.elem.price;
}
}
//将书的价格与这个价格比较,将最贵的书籍的信息存入Bmax.elem中
for(int j =0;j<N;j++){
if(L.elem.price==maxprice){
Bmax.elem = L.elem;
maxnum++;
}
}
//输出
cout<<endl;//先空一行
cout<<"maxnum: "<<maxnum<<endl;//输出最高的价格书的数量
for (int k =0; k < maxnum;k++){
cout<<Bmax.elem.no<<""<<Bmax.elem.name<<"";
printf("%.2f\n",Bmax.elem.price);
}
return OK;
}
static/image/hrline/5.gif
运行结果如下:
学习来了
页:
[1]