附:boot.txt:
北京林业大学图书馆计算机类图书采购列表
ISBN 书名 定价
9787302257646 程序设计基础 25
9787302164340 程序设计基础(第2版) 20
9787302219972 单片机技术及应用 32
9787302203513 单片机原理与应用技术 26
9787810827430 工业计算机控制技术——原理与应用 29
9787811234923 汇编语言程序设计教程 21
9787512100831 计算机操作系统 17
9787302202844 计算机操作系统基础与应用(第二版) 30
9787302265436 计算机导论实验指导 18
9787302131304 计算机数学 20
9787302162834 计算机数学 30
9787302221630 计算机数学(第二版) 25
9787811235388 计算机网络基础 32
9787302137849 计算机维护与维修 26
9787302188308 计算机系统组装与维护 26
9787512101517 计算机应用基础与信息处理案例教程 25
9787811239447 计算机英语 29
9787302257448 计算机硬件与维护 29.8
9787302125679 计算机专业英语 25
9787302235682 计算机专业英语 28
9787302149286 计算机专业英语(第2版) 25
9787302211860 计算机组成原理(第2版) 29.5
9787302215530 计算机组装与维护 27
9787302232629 计算机组装与维护 24
9787302180630 实用数据结构 29
9787302225065 数据结构(C/C#语言版) 28
9787302135180 数据结构 21
9787302228141 数据结构 28
9787302163282 数据结构实验与实训教程(第2版) 23
9787302194934 微机原理与汇编语言实用教程 32
9787302133841 微机原理与接口技术 31
9787302223771 微型计算机原理及应用技术 29
9787302171973 微型计算机原理与结构(第二版) 28
9787302184898 微型计算机原理与结构习题解答与实验指导 21
9787302162988 ARM9嵌入式系统设计与开发应用 35
9787302197850 ARM体系结构与程序设计 30
9787302222705 EDA技术与VerilogHDL 38
9787302250708 基于ARM的嵌入式系统接口技术 43
9787302225515 嵌入式LinuxC语言应用程序设计与实践 36
9787302250753 嵌入式Linux实时操作系统及应用编程 48
9787302253402 ARM9嵌入式系统设计与应用 39
9787302188667 ASP.NET&IIS7高级编程 79.8
9787302259596 ASP.NET程序设计实训教程 26
9787302195252 ASP.NET程序设计与软件项目实训 34
9787302220763 ASP.NET实践教程 29
9787302258117 ASP.NET网站开发案例教程 36
9787302197867 ASP案例汇编 25
9787302212850 ASP语言程序设计 33
9787302198499 Authorware7.0实用教程 28
9787811230710 C#程序设计易懂易会教程 32
9787302219583 C#程序设计与开发 27
9787302171676 C#面向对象程序设计 39.5
9787302266341 C#面向对象程序设计(第2版) 58
9787302250692 C语言程序设计 42
9787302201090 C语言程序设计(第2版) 35
9787302137634 C语言程序设计基础与实训教程 25
9787810824125 C语言程序设计教程(修订本) 22
9787302127970 C语言程序设计与应用开发 29
9787302202646 C语言简明教程 26
9787302254454 C语言课程设计案例精编(第二版) 52
9787302150664 J2ME无线开发实训教程 25
9787302176091 Java2程序设计基础 27
9787302246909 Java高级程序设计 29
9787302260806 Java编程与实践 36
9787302210542 Java程序设计 29
9787302170037 Java程序设计教程(第2版) 29.5
9787302252887 Java程序设计与应用教程 39
9787302129226 Java高级编程实用教程 28
9787302214953 Java应用案例教程 33
9787302266211 Java语言程序设计实用教程 39
9787302202059 JSP动态网站开发实用教程 35
9787302256502 MATLAB程序设计与应用基础教程 24
9787302127956 UML基础与应用(高等学校规划教材) 22
9787302179306 VB.NET程序设计实用教程 31
9787302195696 VB.NET程序设计与软件项目实训 29
9787302220381 VB程序设计案例汇编 22
9787302229070 VC++程序设计基础教程 28
9787302214489 VC++程序设计项目实践 26
9787302176909 VisualBasic.NET程序设计 24
9787302172246 VisualBasic程序设计实用教程 30
9787302176060 VisualBasic程序设计与应用 30
9787302255604 VisualBasic项目化教程同步训练题 28
9787302254461 VisualC#2008程序设计与应用教程 38
9787302241522 VisualC#程序设计与项目实践 35
9787302218753 Web应用程序设计技术——ASP.NET(C#) 29
9787302231660 Web应用程序设计技术——ASP.NET(第2版) 30
9787811232554 XML基础教程 25
9787302179832 XML实用教程 20
9787302165156 基于C#的WindowsCE程序开发实例教程 23
9787302215356 基于工作过程的Java程序设计 27
9787302198505 嵌入式操作系统及编程 25
9787302169666 软件测试 24
9787811232301 软件测试 17
9787302167884 软件测试教程 22
9787302204862 软件工程 23
9787302186137 软件开发生命周期 23
9787302198697 软件开发与管理 28
9787302226024 软件项目综合实训(.NET篇) 29
9787811231991 实用Java程序设计 25
9787811235890 实用软件工程 26
9787302195863 实用软件工程与实训 25
9787302199519 新编Java实用教程 48
9787811231557 Eclipse基础与应用 35
第1关:采用数组实现图书浏览
任务描述
读取book.txt文件中信息并输出。
编程要求
输入要求
book.txt。
输出要求
依次输出所有图书信息(书号、书名、价格),包括文件前两行的标题说明信息。
测试说明
平台会对你编写的代码进行测试:
输入样例:
无
输出样例:
全部文件信息
#include <bits/stdc++.h>
using namespace std;
typedef struct{
char no[20]; //书号
char name[100]; //书名
float price; //价格
}Book;
typedef struct{
Book *elem; //指向数组的指针
int length; //数组的长度
}SqList;
void InitList(SqList &L){
//使用动态内存分配new进行初始化
L.elem = new Book[200];
L.length=0;
}
void FreeList(SqList &L){
//释放内存
delete[] L.elem;
L.length=0;
}
void ReadFile(SqList &L, string filepath){
//从文件中读取图书信息,将其按顺序存入L.elem指向的数组中
fstream f;
f.open(filepath.c_str(),ios::in);
if(!f.is_open()){
cout<<"ERROR!"<<endl;
return;
}
string line;
//跳过已有的表头与字段名
getline(f,line);
getline(f,line);
while(getline(f,line)){
stringstream ss(line);
Book b;
ss >>b.no;
ss>>b.name;
ss >>b.price;
L.elem[L.length] = b;
L.length++;
}
f.close();
}
void Print(SqList &L){
//输出结果
if(L.length == 0){
return;
}
cout << "北京林业大学图书馆计算机类图书采购列表" << endl;
cout << "ISBN" << " ";
cout << "书名" << " ";
cout << "定价" << endl; //打印表头
for(int i = 0; i < L.length; i++){ //输出数据
cout << L.elem[i].no << " ";
cout << L.elem[i].name << " ";
cout << L.elem[i].price << endl;
}
}
int main(){
SqList L;
string filepath = "/data/workspace/myshixun/book.txt";
InitList(L);
ReadFile(L, filepath); //读取文件
Print(L); //打印输出
FreeList(L);
return 0;
}
要点:
- C++文件操作
- 注意:f.open(filepath.c_str(),ios::in)此处不能写为f.open(filepath,ios::in),第一个参数需要使用C风格字符串。
- C++字符串流
- new与delete的使用
第2关:采用数组实现图书统计
任务描述
读取book.txt文件中的图书信息并统计图书总数。
编程要求
输入要求
book.txt。
输出要求
输出一行数据,为图书的总数。
测试说明
平台会对你编写的代码进行测试:
输入样例:
无
输出样例:
103
#include <bits/stdc++.h>
using namespace std;
typedef struct{
char no[20]; //书号
char name[100]; //书名
float price; //价格
}Book;
typedef struct{
Book *elem; //指向数组的指针
int length; //数组的长度
}SqList;
void InitList(SqList &L){
//使用动态内存分配new进行初始化
L.elem= new Book[200];
L.length=0;
}
void FreeList(SqList &L){
//释放内存
delete[] L.elem;
L.length=0;
}
void ReadFile(SqList &L, string filepath){
//从文件中读取图书信息,将其按顺序存入L.elem指向的数组中,同时统计图书总数存入L.length中
fstream f;
f.open(filepath.c_str(),ios::in);
string line;
getline(f,line);
getline(f,line);
while(getline(f,line)){
stringstream ss(line);
Book b;
ss>>b.no;
ss>>b.name;
ss>>b.price;
L.elem[L.length]=b;
L.length++;
}
f.close();
}
void Print(SqList &L){
//打印输出
cout << L.length << endl;
}
int main(){
SqList L;
string filepath = "/data/workspace/myshixun/book.txt";
InitList(L);
ReadFile(L, filepath); //读取文件
Print(L);
FreeList(L);
return 0;
}
第3关:采用数组实现图书插入
任务描述
将给定的图书信息插入到数组指定位置中,并将变化后的图书信息写回至book.txt文件中。
编程要求
输入要求
输入四行数据。其中,第1行为插入位置pos(1≤pos≤n+1),第2行到第4行分别为待插入的图书书号、书名和价格。
输出要求
输出一行数据,为插入的图书信息。
测试说明
平台会对你编写的代码进行测试:
输入样例:
1
9787115578567
数据结构上机指导(C语言版)
69.9
输出样例:
9787115578567 数据结构上机指导(C语言版) 69.9
#include <bits/stdc++.h>
using namespace std;
typedef struct{
char no[20]; //书号
char name[100]; //书名
float price; //价格
}Book;
typedef struct{
Book *elem; //指向数组的指针
int length; //数组的长度
}SqList;
void InitList(SqList &L){
//使用动态内存分配new进行初始化
L.elem = new Book[200];
L.length=0;
}
void FreeList(SqList &L){
//释放内存
delete[] L.elem;
L.length=0;
}
void ReadFile(SqList &L, string filepath){
//从文件中读取图书信息,将其按顺序存入L.elem指向的数组中
fstream f;
f.open(filepath.c_str(),ios::in);
if(!f.is_open()){
cout<<"ERROR!"<<endl;
return;
}
string line;
//跳过已有的表头与字段名
getline(f,line);
getline(f,line);
while(getline(f,line)){
stringstream ss(line);
Book b;
ss >>b.no;
ss>>b.name;
ss >>b.price;
L.elem[L.length] = b;
L.length++;
}
f.close();
}
void SaveFile(SqList &L, string filepath) {
//保存图书信息到文件
fstream f;
f.open(filepath.c_str(),ios::out);
if(!f.is_open()){
cout<<"ERROR!"<<endl;
return;
}
f<<"北京林业大学图书馆计算机类图书采购列表"<<endl<<"ISBN\t"<<"书名\t"<<"定价"<<endl;
for(int i =0;i<L.length;i++){
f<<L.elem[i].no<<"\t"<<L.elem[i].name<<"\t"<<L.elem[i].price<<endl;
}
f.close();
}
bool InsertBook(SqList &L, int pos, Book *book){
//插入图书信息,输入图书的书号、书名和价格,将该图书的基本信息插入到数组中的指定位置
//如果插入成功,返回true,否则,返回false
if (pos < 1 || pos > L.length + 1) {
return false;
}
//将要插入位置及其后面的数据向后移一位
for(int i = L.length;i>=pos;i--){
L.elem[i]=L.elem[i-1];
}
L.elem[pos-1]=*book;
L.length++;
return true;
}
void Check(SqList L, Book *book, int pos){
//结果检查
if(!strcmp(L.elem[pos-1].no, book->no)&&!strcmp(L.elem[pos-1].name, book->name)&&L.elem[pos-1].price==book->price)
cout<< book->no << " " << book->name << " "<< book->price <<endl;
else{
cout<<"插入失败"<<endl;
}
}
int main(){
SqList L;
string filepath = "/data/workspace/myshixun/book.txt";
string newpath = "/data/workspace/myshixun/1.3-采用数组实现图书插入/book-new.txt";
int pos;
cin >> pos;
Book *book = new Book();
cin >> book->no >> book->name >> book->price;
InitList(L);
ReadFile(L, filepath);
bool flag=InsertBook(L, pos, book);
if(flag){
SaveFile(L, newpath);
SqList L2;
InitList(L2);
ReadFile(L2, newpath);
Check(L2, book, pos);
FreeList(L2);
}
else{
cout<<"插入失败"<<endl;
}
FreeList(L);
return 0;
}
要点:
- 注意对pos进行合法性检查
- 数组插入数据操作
第4关:采用数组实现图书删除
任务描述
根据指定位置删除book.txt文件中该位置上的图书信息。
编程要求
输入要求
输入一行数据,为删除位置pos(1≤pos≤n)。
输出要求
依次输出删除后的所有图书信息(书号、书名、价格),包括文件前两行的标题说明信息。
测试说明
平台会对你编写的代码进行测试:
输入样例:
6
输出样例:
删除后的全部文件信息
#include <bits/stdc++.h>
using namespace std;
typedef struct{
char no[20]; //书号
char name[100]; //书名
float price; //价格
}Book;
typedef struct{
Book *elem; //指向数组的指针
int length; //数组的长度
}SqList;
void InitList(SqList &L){
//使用动态内存分配new进行初始化
L.elem = new Book[200];
L.length=0;
}
void FreeList(SqList &L){
//释放内存
delete[] L.elem;
L.length=0;
}
void ReadFile(SqList &L, string filepath) {
//从文件中读取图书信息,将其按顺序存入L.elem指向的数组中
fstream f;
f.open(filepath.c_str(),ios::in);
if(!f.is_open()){
cout<<"ERROR!"<<endl;
return;
}
string line;
//跳过已有的表头与字段名
getline(f,line);
getline(f,line);
while(getline(f,line)){
stringstream ss(line);
Book b;
ss >>b.no;
ss>>b.name;
ss >>b.price;
L.elem[L.length] = b;
L.length++;
}
f.close();
}
void SaveFile(SqList &L, string filepath) {
//保存图书信息到文件
fstream f;
f.open(filepath.c_str(),ios::out);
if(!f.is_open()){
cout<<"ERROR!"<<endl;
return;
}
f<<"北京林业大学图书馆计算机类图书采购列表"<<endl<<"ISBN\t"<<"书名\t"<<"定价"<<endl;
for(int i =0;i<L.length;i++){
f<<L.elem[i].no<<"\t"<<L.elem[i].name<<"\t"<<L.elem[i].price<<endl;
}
f.close();
}
bool DeleteBook(SqList &L, int pos) {
//删除指定图书信息
//如果删除成功,返回true,否则,返回false
if(pos<1||pos>L.length)
return 0;
for(int i=pos-1;i<L.length-1;i++){
L.elem[i]=L.elem[i+1];
}
L.length--;
return 1;
}
void Print(SqList &L) {
//输出结果
if(L.length == 0) {
return;
}
cout << "北京林业大学图书馆计算机类图书采购列表" << endl;
cout << "ISBN" << " ";
cout << "书名" << " ";
cout << "定价" << endl; //打印表头
for(int i = 0; i < L.length - 1; i++) { //输出数据
cout << L.elem[i].no << " ";
cout << L.elem[i].name << " ";
cout << L.elem[i].price << endl;
}
cout << L.elem[L.length - 1].no << " ";
cout << L.elem[L.length - 1].name << " ";
cout << L.elem[L.length - 1].price;
}
int main() {
SqList L;
SqList L1;
int pos;
cin>>pos;
InitList(L);
InitList(L1);
ReadFile(L, "./book.txt");
bool flag = DeleteBook(L, pos);
if (flag) {
SaveFile(L, "./book-new.txt");
ReadFile(L1, "./book-new.txt");
Print(L1);
}
else {
cout << "删除失败" << endl;
}
FreeList(L);
FreeList(L1);
return 0;
}
要点:
- 数组数据的删除(后面数据向前补)
第5关:采用数组实现图书查找
任务描述
用两种方式实现对book.txt文件中的图书查找:
① 按位置进行查找并输出;
② 按书名进行查找并输出(如果有多本,则全部输出)。
编程要求
输入要求
输入两行数据。其中,第1行为查找的类型,输入“1”表示按位置查找,输入“2”表示按书名查找。第2行根据第1行的类型输入相应的位置和或书名。
输出要求
依次输出查找的所有图书信息(书号、书名、价格)
测试说明
平台会对你编写的代码进行测试:
输入样例:
1
6
输出样例:
9787811234923 汇编语言程序设计教程 21
输入样例:
2
计算机数学
输出样例:
9787302131304 计算机数学 20
9787302162834 计算机数学 30
#include <bits/stdc++.h>
using namespace std;
typedef struct{
char no[20]; //书号
char name[100]; //书名
float price; //价格
}Book;
typedef struct{
Book *elem; //指向数组的指针
int length; //数组的长度
}SqList;
void InitList(SqList &L){
//使用动态内存分配new进行初始化
L.elem = new Book[200];
L.length=0;
}
void FreeList(SqList &L){
//释放内存
delete[] L.elem;
L.length=0;
}
void ReadFile(SqList &L, string filepath) {
//从文件中读取图书信息,将其按顺序存入L.elem指向的数组中
fstream f;
f.open(filepath.c_str(),ios::in);
if(!f.is_open()){
cout<<"ERROR!"<<endl;
return;
}
string line;
//跳过已有的表头与字段名
getline(f,line);
getline(f,line);
while(getline(f,line)){
stringstream ss(line);
Book b;
ss >>b.no;
ss>>b.name;
ss >>b.price;
L.elem[L.length] = b;
L.length++;
}
f.close();
}
SqList SearchBook(SqList &L, int type) {
//图书信息查找,返回包含查找图书信息的数组和数组的长度,如果不存在,则令长度为0
SqList t;
InitList(t);
if(type == 1){
int pos;
cin >> pos;
if(pos<1||pos>L.length)
return t;
t.elem[0]=L.elem[pos-1];
t.length++;
return t;
}
if(type == 2){
char name[50];
cin >> name;
for(int i =0;i<L.length;i++){
if(strcmp(L.elem[i].name,name)==0){
t.elem[t.length]=L.elem[i];
t.length++;
}
}
return t;
}
}
int main() {
SqList L;
int type;
cin >> type;
InitList(L);
ReadFile(L, "/data/workspace/myshixun/book.txt");
SqList t=SearchBook(L, type);
if(t.length==0){
cout<<"查找失败"<<endl;
}
else{
for(int i=0;i<t.length;i++){
cout<<t.elem[i].no<<" "<<t.elem[i].name<<" "<<t.elem[i].price<<endl;
}
}
FreeList(L);
return 0;
}
要点:
- pos的合法性检查
- 函数返回类型的要求与具体实现
第6关:采用数组实现图书价格更新
任务描述
将book.txt文件中价格小于45元的图书价格提高20%,价格大于等于45元的图书价格提高10%,将修改后的图书信息重新写入新文件book-newprice.txt中。
编程要求
输入要求
book.txt。
输出要求
依次输出更新后的所有图书信息(书号、书名、价格),包括文件前两行的标题说明信息。
测试说明
平台会对你编写的代码进行测试:
输入样例:
无
预期输出:
更新后的全部文件信息
#include <bits/stdc++.h>
using namespace std;
typedef struct{
char no[20]; //书号
char name[100]; //书名
float price; //价格
}Book;
typedef struct{
Book *elem; //指向数组的指针
int length; //数组的长度
}SqList;
void InitList(SqList &L){
//使用动态内存分配new进行初始化
L.length=0;
L.elem=new Book[200];
}
void FreeList(SqList &L){
//释放内存
delete[] L.elem;
L.length= 0;
}
void ReadFile(SqList &L, string filepath) {
//从文件中读取图书信息,将其按顺序存入L.elem指向的数组中
fstream f;
f.open(filepath.c_str(),ios::in);
if(!f.is_open()){
cout<<"ERROR!"<<endl;
return;
}
string line;
//跳过已有的表头与字段名
getline(f,line);
getline(f,line);
while(getline(f,line)){
stringstream ss(line);
Book b;
ss >>b.no;
ss>>b.name;
ss >>b.price;
L.elem[L.length] = b;
L.length++;
}
f.close();
}
void SaveFile(SqList &L, string filepath) {
//保存图书信息到文件
fstream f;
f.open(filepath.c_str(),ios::out);
if(!f.is_open()){
cout<<"ERROR!"<<endl;
return;
}
f<<"北京林业大学图书馆计算机类图书采购列表"<<endl<<"ISBN\t"<<"书名\t"<<"定价"<<endl;
for(int i =0;i<L.length;i++){
f<<L.elem[i].no<<"\t"<<L.elem[i].name<<"\t"<<L.elem[i].price<<endl;
}
f.close();
}
void UpdateBook(SqList &L) {
//图书信息更新
for(int i=0;i<L.length;i++){
if(L.elem[i].price<45)
L.elem[i].price*=1.2;
else
L.elem[i].price*=1.1;
}
}
void Print(SqList &L) {
//输出结果
if(L.length == 0) {
return;
}
cout << "北京林业大学图书馆计算机类图书采购列表" << endl;
cout << "ISBN" << " ";
cout << "书名" << " ";
cout << "定价" << endl; //打印表头
for(int i = 0; i < L.length; i++) { //输出数据
cout << L.elem[i].no << " ";
cout << L.elem[i].name << " ";
cout << L.elem[i].price << endl;
}
}
int main() {
SqList L;
SqList L1;
InitList(L);
ReadFile(L, "/data/workspace/myshixun/book.txt");
UpdateBook(L);
SaveFile(L, "/data/workspace/myshixun/1.6-采用数组实现图书价格更新/book-newprice.txt");
InitList(L1);
ReadFile(L1, "/data/workspace/myshixun/1.6-采用数组实现图书价格更新/book-newprice.txt");
Print(L1);
FreeList(L);
FreeList(L1);
return 0;
}
第7关:采用数组实现图书价格排序
任务描述
对book.txt中图书价格升序排序,并将排序后的图书信息写入新文件book-newsort.txt中。
编程要求
输入要求
book.txt
输出要求
输出指定下标的图书信息(书号、书名、价格)。
测试说明
平台会对你编写的代码进行测试:
输入样例:
无
输出样例:
输出数组中下标为3的图书信息
书号:9787302164340
书名:程序设计基础(第2版)
价格:20
输出数组中下标为20的图书信息
书号:9787302169666
书名:软件测试
价格:24
输出数组中下标为23的图书信息
书号:9787512101517
书名:计算机应用基础与信息处理案例教程
价格:25
#include<bits/stdc++.h>
using namespace std;
typedef struct{
char no[20]; //书号
char name[100]; //书名
float price; //价格
}Book;
typedef struct{
Book *elem; //指向数组的指针
int length; //数组的长度
}SqList;
void InitList(SqList &L){
//使用动态内存分配new进行初始化
L.elem = new Book[200];
L.length=0;
}
void ReadFile(SqList &L, string filepath){
//从文件中读取图书信息,将其按顺序存入L.elem指向的数组中
fstream f;
f.open(filepath.c_str(),ios::in);
if(!f.is_open()){
cout<<"ERROR!"<<endl;
return;
}
string line;
//跳过已有的表头与字段名
getline(f,line);
getline(f,line);
while(getline(f,line)){
stringstream ss(line);
Book b;
ss >>b.no;
ss>>b.name;
ss >>b.price;
L.elem[L.length] = b;
L.length++;
}
f.close();
}
void PriceSort(SqList &L){
//按图书价格升序排序,采用冒泡排序
for(int i =0;i<L.length;i++){
for(int j=0;j<L.length-i-1;j++){
if(L.elem[j].price>L.elem[j+1].price){
Book t=L.elem[j];
L.elem[j]=L.elem[j+1];
L.elem[j+1]=t;
}
}
}
}
void SaveFile(SqList &L, string filepath){
//把排序后的数据写入新文件"book-newsort.txt"中
fstream f;
f.open(filepath.c_str(),ios::out);
if(!f.is_open()){
cout<<"ERROR!"<<endl;
return;
}
f<<"北京林业大学图书馆计算机类图书采购列表"<<endl<<"ISBN\t"<<"书名\t"<<"定价"<<endl;
for(int i =0;i<L.length;i++){
f<<L.elem[i].no<<"\t"<<L.elem[i].name<<"\t"<<L.elem[i].price<<endl;
}
f.close();
}
void FreeList(SqList &L){
//释放内存
delete[] L.elem;
L.length=0;
}
int main()
{
SqList L;
SqList L1;
InitList(L);
ReadFile(L,"/data/workspace/myshixun/book.txt");
PriceSort(L);
SaveFile(L, "/data/workspace/myshixun/1.7-采用数组实现图书价格排序/book-newsort.txt"); InitList(L1);
ReadFile(L1, "/data/workspace/myshixun/1.7-采用数组实现图书价格排序/book-newsort.txt");
int pos[3] = {3, 20, 23}; //验证排序后的数组
for(int k = 0; k < 3; k++){
int i = pos[k];
cout << "输出数组中下标为" << i << "的图书信息" << endl;
cout << "书号:" << L1.elem[i].no << endl;
cout << "书名:" << L1.elem[i].name << endl;
cout << "价格:" << L1.elem[i].price<<endl;
}
FreeList(L);
FreeList(L1);
return 0;
}
第8关:采用数组实现图书信息逆序存储
任务描述
将book.txt中的全部图书信息逆序存储(即最后一本置于原第一本位置)并写入新文件book-newinverse.txt中。
编程要求
输入要求
book.txt
输出要求
输出指定下标的图书信息(书号、书名、价格)。
测试说明
平台会对你编写的代码进行测试:
输入样例:
无
输出样例:
输出数组中下标为3的图书信息
书号:9787811235890
书名:实用软件工程
价格:26
输出数组中下标为20的图书信息
书号:9787302254461
书名:VisualC#2008程序设计与应用教程
价格:38
输出数组中下标为23的图书信息
书号:9787302172246
书名:VisualBasic程序设计实用教程
价格:30
#include<bits/stdc++.h>
using namespace std;
typedef struct{
char no[20]; //书号
char name[100]; //书名
float price; //价格
}Book;
typedef struct{
Book *elem; //指向数组的长度
int length; //数组的长度
}SqList;
void InitList(SqList &L){
//使用动态内存分配new进行初始化
L.elem = new Book[200];
L.length=0;
}
void ReadFile(SqList &L, string filepath){
//从文件中读取图书信息,将其按顺序存入L.elem指向的数组中
fstream f;
f.open(filepath.c_str(),ios::in);
if(!f.is_open()){
cout<<"ERROR!"<<endl;
return;
}
string line;
//跳过已有的表头与字段名
getline(f,line);
getline(f,line);
while(getline(f,line)){
stringstream ss(line);
Book b;
ss >>b.no;
ss>>b.name;
ss >>b.price;
L.elem[L.length] = b;
L.length++;
}
f.close();
}
void ReverseBook(SqList &L){
//把图书信息逆序存储
for(int i =0;i<=L.length/2-1;i++){
Book b =L.elem[i];
L.elem[i]=L.elem[L.length-i-1];
L.elem[L.length-i]=b;
}
}
void SaveFile(SqList &L, string filepath){
//把逆序后的数据写入新文件"book-newinverse.txt"中
fstream f;
f.open(filepath.c_str(),ios::out);
if(!f.is_open()){
cout<<"ERROR!"<<endl;
return;
}
f<<"北京林业大学图书馆计算机类图书采购列表"<<endl<<"ISBN\t"<<"书名\t"<<"定价"<<endl;
for(int i =0;i<L.length;i++){
f<<L.elem[i].no<<"\t"<<L.elem[i].name<<"\t"<<L.elem[i].price<<endl;
}
f.close();
}
void FreeList(SqList &L){
//释放内存
delete[] L.elem;
L.length=0;
}
int main()
{
SqList L;
SqList L1;
InitList(L);
ReadFile(L, "/data/workspace/myshixun/book.txt");
ReverseBook(L);
SaveFile(L, "/data/workspace/myshixun/1.8-采用数组实现图书信息逆序存储/book-newinverse.txt");
InitList(L1);
ReadFile(L1, "/data/workspace/myshixun/1.8-采用数组实现图书信息逆序存储/book-newinverse.txt");
int pos[3] = {3, 20, 23}; //验证逆转后的数组
for(int k = 0; k < 3; k++){
int i = pos[k];
cout << "输出数组中下标为" << i << "的图书信息" << endl;
cout << "书号:" << L1.elem[i].no << endl;
cout << "书名:" << L1.elem[i].name << endl;
cout << "价格:" << L1.elem[i].price<<endl;
}
FreeList(L);
FreeList(L1);
return 0;
}