210基于顺序存储结构的图书信息表的最佳位置图书的查找
描述
定义一个包含图书信息(书号、书名、价格)的顺序表,读入相应的图书数据来完成图书信息表的创建,然后根据指定的最佳位置的序号,查找该位置上的图书,输出相应图书的信息。
输入
总计n+m+2行。首先输入n+1行,其中,第一行是图书数目n,后n行是n本图书的信息(书号、书名、价格),每本图书信息占一行,书号、书名、价格用空格分隔,价格之后没有空格。其中书号和书名为字符串类型,价格为浮点数类型。然后输入m+1行,其中,第一行是一个整数m,代表查找m次,后m行每行内容为一个整数,代表待查找的图书的位置序号。
输出
输出m行 若查找成功: 输出内容为第i次查询的指定位置上的一本图书的信息(书号、书名、价格),书号、书名、价格用空格分隔,其中价格输出保留两位小数。 若查找失败: 只输出以下提示:抱歉,最佳位置上的图书不存在!
输入样例 1
8
9787302257646 Data-Structure 35.00
9787302164340 Operating-System 50.00
9787302219972 Software-Engineer 32.00
9787302203513 Database-Principles 36.00
9787810827430 Discrete-Mathematics 36.00
9787302257800 Data-Structure 62.00
9787811234923 Compiler-Principles 62.00
9787822234110 The-C-Programming-Language 38.00
2
2
0
输出样例 1
9787302164340 Operating-System 50.00
Sorry,the book on the best position doesn't exist!
来源
BJFU
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 1000
typedef struct {
char no[20];
char name[50];
float price;
}Book;
typedef struct {
Book *elem;
int length;
}SqList;
int initList(SqList *list){
list->elem= malloc(sizeof (Book)*MAXSIZE);
if(list->elem==NULL) return 0;
list->length=0;
return 1;
}
//void printList(SqList list,int n){ //输出顺序表
// printf("%d\n",n);
// for (int i = 0; i < n; ++i) {
// printf("%s %s %.2f\n",list.elem[i].no,list.elem[i].name,list.elem[i].price);
// }
//}
void selectByindex(SqList list){
int n; //查询次数
scanf("%d",&n);
int index; //查询位序
for(int m=0;m<n;++m){
scanf("%d",&index);
if(index==0){
printf("Sorry,the book on the best position doesn't exist!");
} else{
printf("%s %s %.2f\n",list.elem[index-1].no,list.elem[index-1].name,list.elem[index-1].price);
}
}
}
int main() {
SqList list; //创建顺序表
initList(&list); //初始化顺序表
int n;
scanf("%d",&n);
for (int i = 0; i <n ; ++i) {
scanf("%s %s %f",list.elem[i].no,list.elem[i].name,&list.elem[i].price);
// if(list.elem[i].price==0) break;
list.length++;
}
selectByindex(list);
}