描述
定义一个包含图书信息(书号、书名、价格)的链表,读入相应的图书数据来完成图书信息表的创建,然后根据指定的最爱图书的名字,查找最爱的图书,输出相应图书的信息。
输入
总计n+m+2行。首先输入n+1行,其中,第一行是图书数目n,后n行是n本图书的信息(书号、书名、价格),每本图书信息占一行,书号、书名、价格用空格分隔,价格之后没有空格。其中书号和书名为字符串类型,价格为浮点数类型。然后输入m+1行,其中,第一行是一个整数m,代表查找m次,后m行是每次待查找的最爱图书名字。
输出
若查找成功: 总计输出m*(k+1)行,对于每一次查找,第一行是最爱图书数目(同一书名的图书可能有多本),后k行是最爱图书的信息(书号、书名、价格),每本图书信息占一行,书号、书名、价格用空格分隔,其中价格输出保留两位小数。 若查找失败: 只输出以下提示:抱歉,没有你的最爱!
输入样例 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 Java-Programming-Language Data-Structure
输出样例 1
Sorry,there is no your favourite! 2 9787302257646 Data-Structure 35.00 9787302257800 Data-Structure 62.00
#include<bits/stdc++.h>
#include<string.h>
using namespace std;
struct Book{
char id[50];
char name[50];
float price;
Book *next;
};
int main(){
int n;
cin>>n;
Book *head=NULL,*tail=NULL,*temp=NULL;
for(int i=0;i<n;i++){
temp=new Book;
cin>>temp->id>>temp->name>>temp->price;
if(head==NULL&&tail==NULL){
head=temp;
tail=temp;
}
else{
tail->next=temp;
tail=temp;
}
temp=temp->next;
}
int m;
cin>>m;
while(m>0){
char name[50];
cin>>name;
int count=0;
temp=head;
while(temp!=NULL){
if(strcmp(temp->name,name)==0){
count++;
}
temp=temp->next;
}
if(count==0)
cout<<"Sorry,there is no your favourite!"<<endl;
else{
cout<<count<<endl;
temp=head;
while(temp!=NULL){
if(strcmp(temp->name,name)==0){
cout<<temp->id<<" "<<temp->name<<" "<<fixed<<setprecision(2)<<temp->price<<endl;
}
temp=temp->next;
}
}
m--;
}
}