描述

 

定义一个包含图书信息(书号、书名、价格)的链表,读入相应的图书数据来完成图书信息表的创建,然后根据指定的待出库的旧图书的位置,将该图书从图书表中删除,最后输出该图书出库后的所有图书的信息。

输入

总计n+2行。首先输入n+1行,其中,第一行是图书数目n,后n行是n本图书的信息(书号、书名、价格),每本图书信息占一行,书号、书名、价格用空格分隔,价格之后没有空格。其中书号和书名为字符串类型,价格为浮点数类型。之后输入第n+2行,内容仅为一个整数,代表待删除的旧图书的位置序号。

输出

若删除成功: 输出旧图书出库后所有图书的信息(书号、书名、价格),总计n-1行,每行是一本图书的信息,书号、书名、价格用空格分隔。其中价格输出保留两位小数。 若删除失败: 只输出以下一行提示:抱歉,出库位置非法!

输入样例 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

输出样例 1

9787302257646 Data-Structure 35.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 

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
9

输出样例 2

Sorry,the position to be deleted is invalid!
#include <iostream>
#include<iomanip>
using namespace std;
#define MAXSIZE 10000

typedef struct 
{
	string no;
	string name;
	double price;
}Book;

typedef struct LNode
{
	Book data;
	struct LNode *next;
}LNode,*LinkList;

void CreateList_R000(LinkList &L)
{
	LinkList r,p;int i=0;
	L=new LNode;
	L->next=NULL;
	r=L;
	while(1) 
	{
		p=new LNode;
		//cin>>p->data;
		string a,b;
		double c;
		cin>>a>>b>>c;
		if(a=="0"&&b=="0"&&c==0) break;
		p->data.no=a;
		p->data.name=b;
		p->data.price=c; 
		//cin>>p->data;
		p->next=NULL;
		r->next=p;
		r=p;
		i++;
	}
}

void CreateList_R(LinkList &L)
{
	LinkList r,p;int i=0;
	L=new LNode;
	L->next=NULL;
	r=L;
	
	int n;
	cin>>n;
	
	while(1) 
	{
		p=new LNode;
		//cin>>p->data;
		string a,b;
		double c;
		cin>>a>>b>>c;
		//if(a=="0"&&b=="0"&&c==0) break;
		p->data.no=a;
		p->data.name=b;
		p->data.price=c; 
		//cin>>p->data;
		p->next=NULL;
		r->next=p;
		r=p;
		i++;
		if(i==n) break;
	}
	return;
}

void CreateList_H(LinkList &L)
{
	LinkList r,p;int i=0;
	L=new LNode;
	L->next=NULL;
	
	int n;
	cin>>n;
	while(1) 
	{
		p=new LNode;
		//cin>>p->data;
		string a,b;
		double c;
		cin>>a>>b>>c;
		//if(a=="0"&&b=="0"&&c==0) break;
		p->data.no=a;
		p->data.name=b;
		p->data.price=c; 
		//cin>>p->data;
		p->next=L->next;
		L->next=p;
		i++;
		if(i==n) break;
	}
	return;
}


void Show(LinkList &L)
{
	LinkList p=L->next;
	while(p)
	{ 
		cout<<p->data.no<<" "<<p->data.name<<" ";
		cout<<fixed<<setprecision(2)<<p->data.price<<endl;//保存小数点后两位
		p=p->next;
	} 
}

int ListLength(LinkList L)
{
    LinkList p;
    p=L->next;  //p指向第一个结点
    int i=0;             
    while(p){//遍历单链表,统计结点数
           i++;
           p=p->next;   
    } 
    return i;                             
 }

void Sort(LinkList &L)
{
	
    int n=ListLength(L);
    LinkList p;
        for(int i=1;i<=n-1;i++)
        {
            p=L->next;//p指向首元节点 
            int j=0;
            while(p&&j<n-i)// 当p不=NULL且未比较到最后一个,继续循环 
            {
                if(p->data.price < p->next->data.price)
                {
                	Book t;
                    t=p->data;
                    p->data=p->next->data;
                    p->next->data=t;//只交换数据域 
                }
                p=p->next;//向下指 
                ++j;//已比较个数 
            }
        }
    return;
	
}

void Aver(LinkList &L)
{
	double aver=0,sum=0;int i;int num=0;
	LinkList p;p=L->next;
	while(p){
		sum=sum+p->data.price;
		num++;
		p=p->next;
	}
	aver=sum/num;
	p=L->next;
	while(p)
	{
		if(p->data.price<aver)
			p->data.price=1.2*p->data.price;
		else if(p->data.price>=aver)
			p->data.price=1.1*p->data.price;
		p=p->next;
	}
	cout<<setiosflags(ios::fixed)<<setprecision(2)<<aver<<endl;
}


void mostexpen(LinkList &L)
{
	LinkList p;p=L->next;
	LinkList max=p;LinkList maxs[3];//使用没有初始化的指针将发生无法预料的结果
	
	while(p)
	{
		if(p->data.price > max->data.price)
		{
			max=p;
		}
		p=p->next;
	}
	
	p=L->next;int k=0;
	
	while(p)	
	{
		if(p->data.price==max->data.price)
		{
			maxs[k]=p;
			k++;
		}
		p=p->next;
	}
	
	cout<<k<<endl;
	int i=0;
	while(maxs[i]&&i<k)
	{
		cout<<maxs[i]->data.no<<" "<<maxs[i]->data.name<<" ";
		cout<<fixed<<setprecision(2)<<maxs[i]->data.price<<endl;
		i++;
	}

}


void favourite(LinkList &L)
{
	int n;int i;int j;string fav[10];	
	LinkList flag[3]; int k=0;
	cin>>n;
	for(i=0;i<n;i++)
	{
		cin>>fav[i];
	}
	
	LinkList p=L->next;
	for(j=0;j<n;j++)
	{
		p=L->next;//不能漏 
		while(p)
		{
			if(p->data.name==fav[j])
			{
				flag[k]=p;
				k++;
			}			
			p=p->next;
		}
		if(k==0)
		cout<<"Sorry,there is no your favourite!"<<endl;
		else
		{
			cout<<k<<endl;
			for(i=0;i<k;i++)
				cout<<flag[i]->data.no<<" "<<flag[i]->data.name<<" "<<setiosflags(ios::fixed)<<setprecision(2)<<flag[i]->data.price<<endl;
		}
		k=0;//不能漏 
	}
	
}

void BestLocate(LinkList &L)
{
	int n;int i;int j;int no[10]; int k=0;
	cin>>n;
	for(i=0;i<n;i++)
	{
		cin>>no[i];
	}
	
	LinkList p=L->next;i=0;
	LinkList flag=NULL;
	for(j=0;j<n;j++)
	{
		p=L->next;
		while(p)
		{
			i++;
			if(no[j]==i)
			{
				flag=p;
				k++;
				break;
			}			
			p=p->next;
		}
		if(k!=0)
		{
			cout<<flag->data.no<<" "<<flag->data.name<<" "<<setiosflags(ios::fixed)<<setprecision(2)<<flag->data.price<<endl;
			flag=NULL;	
		}
		if(k==0)
		cout<<"Sorry,the book on the best position doesn't exist!"<<endl;
		k=0;
	}
		
}

void Add(LinkList &L)
{
	int no; Book add;int j;
	cin>>no;
	cin>>add.no>>add.name>>add.price;
	
	if(no>ListLength(L)||no<1)
	{
		cout<<"Sorry,the position to be inserted is invalid!"<<endl;
		return;
	}
	LinkList p;
	p=L;j=0;
	while(p && (j<no-1))
	{
		p=p->next;
		j++; 
	}
	LinkList s;
	s=new LNode;
	s->data=add;
	s->next=p->next;
	p->next=s;
	Show(L); 
	return;
 } 


void Del(LinkList &L)
{
	int no;int i;int j;
	cin>>no;
	if(no>ListLength(L)||no<1)
	{
		cout<<"Sorry,the position to be deleted is invalid!"<<endl;
		return;
	}
	LinkList p,q;
	p=L;j=0;
	while(p && (j<no-1))
	{
		p=p->next;
		j++; 
	}
	q=p->next;
	p->next=q->next;
	delete q;
	Show(L);
	return; 
}


int main()
{
	LinkList L;
	CreateList_R(L);
	Del(L); 
	//Add(L);
	//BestLocate(L); 
	//favourite(L); 
	//CreateList_H(L);
	//mostexpen(L);
	//Sort(L);
	//Aver(L); 
	//Show(L);
	return 0;
}

 

posted on 2020-02-16 20:05  海月CSDN  阅读(329)  评论(0编辑  收藏  举报