描述
定义一个包含图书信息(书号、书名、价格)的顺序表,读入相应的图书数据来完成图书信息表的创建,然后根据指定的待出库的旧图书的位置,将该图书从图书表中删除,最后输出该图书出库后的所有图书的信息。
输入
总计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!
这个主要注意的点就是删除和打印的时候,要看好i的起始,是不是要取等于号,从0还是1开始。
#include<string>
#include<iostream>
#include<map>
#include<algorithm>
using namespace std;
class Book
{
public:
string id, name;
float price;
};
class Sqlist
{
public:
int length;
Book* elem;
};
void Init(Sqlist& l)
{
l.elem = new Book[1000];
if (!l.elem)
exit(1);
l.length = 0;
}
void Insert(Sqlist& l,int n)
{
for (int i = 1; i <= n; i++)
{
l.length++;
cin >> l.elem[i].id >> l.elem[i].name >> l.elem[i].price;
}
}
void Print(Sqlist l)
{
for (int i = 1; i < l.length; i++)
printf("%s %s %.2f\n", l.elem[i].id.c_str(), l.elem[i].name.c_str(), l.elem[i].price);
}
void OutStore(Sqlist& l, int pos)
{
for (int i = pos; i < l.length; i++)
l.elem[i] = l.elem[i + 1];
Print(l);
}
int main()
{
Sqlist l;
Init(l);
int t;
cin >> t;
Insert(l,t);
int p;
cin >> p;
if (p <= 0 || p > l.length)
cout << "Sorry,the position to be deleted is invalid!" << endl;
else
{
OutStore(l, p);
}
return 0;
}