sy6_文件的应用_5_BookFile
(3)在实验五的基础上加入存储图书信息到文件和读取文件中的图书信息到程序两部分功能。
主要设计:
BookEList.h
void readFile();
void writeFile();
BookEList.cpp
void BookEList::readFile()
{
float price;
int number;
string name, isbn, publisher, author;
ifstream infile("BookList.txt");
if (!infile)
{
cerr << "未打开BookList.txt文件!";
return;
}
while (infile >> name)
{
infile >> price;
infile >> isbn;
infile >> publisher;
infile >> author;
BookE b(name,price, isbn, publisher, author);
insert(b);
}
cout << "从BookList.txt读出已有图书图下:" << endl;
showAll();
infile.close();
}
void BookEList::writeFile()
{
ofstream outfile("BookList.txt", ios::out);
if (!outfile)
{
cerr << "未打开 BookList.txt文件" << endl;
exit(-1);
}
for (int i = 0; i < bookNum; i++)
{
outfile << setw(15) << bookList[i].get_name();
outfile << setw(15) << bookList[i].get_price();
outfile << setw(15) << bookList[i].get_isbn();
outfile << setw(15) << bookList[i].get_publisher();
outfile << setw(15) << bookList[i].get_author() << endl;
}
outfile.close();
}
整段代码:
包含文件:
BookList.txt
Book.h
BookE.h
BookList.h
Book.cpp
BookE.cpp
BookEList.cpp
main.cpp
BookList.txt
狂人日记 31 9787505738478 中国友谊出版 鲁迅
朝花夕拾 20 9787538763010 时代文艺出版社 鲁迅
阿Q正传 10 9787550241077 北京联合出版社 鲁迅
Book.h
#pragma once
#ifndef BOOK_H
#define BOOK_H
#include <iostream>
#include <iomanip>
using namespace std;
class Book
{
protected:
string name;
float price;
public:
Book();
Book(const string Bname, const float Bprice);
string get_name();
void setName(const string newName);
float get_price();//获取价格
void setPrice(const float newPrice);
};
#endif // !BOOK_H
BookE.h
#pragma once
#ifndef BOOKE_H
#define BOOKE_H
#include <iostream>
#include <iomanip>
#include "Book.h"
using namespace std;
class BookE :public Book
{
protected:
string isbn;
string publisher;
string author;
public:
BookE();
BookE(const string Bname, const float Bprice, const string Bisbn, const string Bpuvlisher, const string Bauthor);
string get_isbn();
string get_publisher();
string get_author();
void setIsbn(const string newIsbn);
void setPublisher(const string newPublisher);
void setAuthor(const string newAuthor);
friend istream& operator>>(istream& input, BookE& booke);
friend ostream& operator<<(ostream& output, BookE& booke);
};
#endif // !BOOKE_H
BookList.h
#pragma once
#ifndef BOOKLIST_H
#define BOOKLIST_H
#include <iostream>
#include <iomanip>
#include<fstream>
#include "BookE.h"
using namespace std;
class BookEList :public BookE
{
protected:
BookE bookList[100];
public:
int bookNum;
BookEList();
void insert(BookE b);
void showBook(int i);
void showBook_title();
void showAll();
int search_name(string name);
int search_price(float price);
int search_isbn(string isbn);
int search_publisher(string publisher);
int search_author(string author);
BookE bookInfor(int i);
void update(int i, BookE b);
void deleted(int i);
void readFile();
void writeFile();
};
#endif
Book.cpp
#include "Book.h"
Book::Book()//构造函数Book()
{
}
Book::Book(const string Bname, const float Bprice)//重载构造函数:形参初始化数据成员
{
name = Bname;
price = Bprice;
}
string Book::get_name() {
return name;
}
void Book::setName(const string newName) {
name = newName;
}
float Book::get_price() {
return price;
}
void Book::setPrice(const float newPrice) {
price = newPrice;
}
BookE.cpp
#include "BookE.h"
BookE::BookE()//构造函数Book()
{
}
BookE::BookE(const string Bname, const float Bprice, const string Bisbn, const string Bpublisher, const string Bauthor) :Book(Bname, Bprice)//重载构造函数:形参初始化数据成员
{
isbn = Bisbn;
publisher = Bpublisher;
author = Bauthor;
}
string BookE::get_isbn()
{
return isbn;
}
void BookE::setIsbn(const string newIsbn)
{
isbn = newIsbn;
}
string BookE::get_publisher()
{
return publisher;
}
void BookE::setPublisher(const string newPublisher)
{
publisher = newPublisher;
}
string BookE::get_author()
{
return author;
}
void BookE::setAuthor(const string newAuthor)
{
author = newAuthor;
}
istream& operator>>(istream& input, BookE& book)
{
cout << "请输入书名:";
input >> book.name;
cout << "请输入图书价格:";
input >> book.price;
cout << "请输入图书ISBN:";
input >> book.isbn;
cout << "请输入图书的出版社:";
input >> book.publisher;
cout << "请输入作者:";
input >> book.author;
return input;
}
ostream& operator<<(ostream& output, BookE& book)
{
output << left << setw(15) << book.name << setw(15) << book.price << setw(15) << book.isbn << setw(15) << book.publisher << setw(15) << book.author << endl;
return output;
}
BookEList.cpp
#include "BookEList.h"
BookEList::BookEList()
{
bookNum = 0;
}
void BookEList::insert(BookE b)//创建图书
{
bookList[bookNum].setName(b.get_name());//依次添加图书
bookList[bookNum].setPrice(b.get_price());
bookList[bookNum].setIsbn(b.get_isbn());
bookList[bookNum].setPublisher(b.get_publisher());
bookList[bookNum].setAuthor(b.get_author());
bookNum++;//图书添加成功后,现有图书数目bookNum+1
}
void BookEList::showBook(int i)//打印某一本书
{
//cout << left << setw(15) << bookList[i].get_name() << setw(15) << bookList[i].get_price() << setw(15) << bookList[i].get_isbn() << setw(15) << bookList[i].get_publisher() << setw(15) << bookList[i].get_author() << endl;
cout << bookList[i];//使用输出流重载
}
void BookEList::showBook_title()//打印表头
{
cout << left << setw(15) << "书名" << setw(15) << "价格" << setw(15) << "ISBN" << setw(15) << "出版社" << setw(15) << "作者" << endl;
}
void BookEList::showAll()//打印所有书本
{
int j;
j = bookNum;
if (j == 0)
{
cout << "本系统暂未存储图书" << endl;
}
else
{
cout << "共有 " << j << " 本图书存储系统中," << "如下:" << endl;
showBook_title();
}
for (bookNum = 0; bookNum < j; bookNum++)
{
showBook(bookNum);
}
}//查找图书信息↓
int BookEList::search_name(string Name)//书名查找
{
int i;
for (i = 0; i < bookNum; i++)
{
if (bookList[i].get_name() == Name)
return i;
}
if (i == bookNum)
{
return -1;
}
}
int BookEList::search_price(float Price)//价格查找
{
int i;
for (i = 0; i < bookNum; i++)
{
if (bookList[i].get_price() == Price)
return i;
}
if (i == bookNum)
{
return 0;
}
}
int BookEList::search_isbn(string Isbn)//isbn查找
{
int i;
for (i = 0; i < bookNum; i++)
{
if (bookList[i].get_isbn() == Isbn)
return i;
}
if (i == bookNum)
{
return 0;
}
}
int BookEList::search_publisher(string Publisher)//出版社查找
{
int i;
for (i = 0; i < bookNum; i++)
{
if (bookList[i].get_publisher() == Publisher)
return i;
}
if (i == bookNum)
{
return 0;
}
}
int BookEList::search_author(string Author)//作者查找
{
int i;
for (i = 0; i < bookNum; i++)
{
if (bookList[i].get_author() == Author)
return i;
}
if (i == bookNum)
{
return 0;
}
}
//查找图书信息↑
BookE BookEList::bookInfor(int i)//获取书本信息
{
return bookList[i];
}
void BookEList::update(int i, BookE b)//修改图书信息,指定下标
{
bookList[i].setName(b.get_name());//需要修改书的书名来自对象b输入的书名
bookList[i].setPrice(b.get_price());
bookList[i].setIsbn(b.get_isbn());
bookList[i].setPublisher(b.get_publisher());
bookList[i].setAuthor(b.get_author());
}
void BookEList::deleted(int i)
{
int j;
for (j = i; j < (bookNum - 1); j++)
{
bookList[j].setName(bookList[j + 1].get_name());//删除后第j本书的新书名来自j+1本的书名,其他同理
bookList[j].setPrice(bookList[j + 1].get_price());
bookList[j].setIsbn(bookList[j + 1].get_isbn());
bookList[j].setPublisher(bookList[j + 1].get_publisher());
bookList[j].setAuthor(bookList[j + 1].get_author());
}
bookNum--;
}
void BookEList::readFile()
{
float price;
int number;
string name, isbn, publisher, author;
ifstream infile("BookList.txt");
if (!infile)
{
cerr << "未打开BookList.txt文件!";
return;
}
while (infile >> name)
{
infile >> price;
infile >> isbn;
infile >> publisher;
infile >> author;
BookE b(name,price, isbn, publisher, author);
insert(b);
}
cout << "从BookList.txt读出已有图书图下:" << endl;
showAll();
infile.close();
}
void BookEList::writeFile()
{
ofstream outfile("BookList.txt", ios::out);
if (!outfile)
{
cerr << "未打开 BookList.txt文件" << endl;
exit(-1);
}
for (int i = 0; i < bookNum; i++)
{
outfile << setw(15) << bookList[i].get_name();
outfile << setw(15) << bookList[i].get_price();
outfile << setw(15) << bookList[i].get_isbn();
outfile << setw(15) << bookList[i].get_publisher();
outfile << setw(15) << bookList[i].get_author() << endl;
}
outfile.close();
}
main.cpp
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <stdlib.h>
#include "Book.h"
#include "BookE.h"
#include "BookEList.h"
using namespace std;
void Show_Menu()
{
cout << "\n===========================\n";
cout << " <<欢迎使用图书管理系统>> \n";
cout << "===========================\n";
cout << "[ 主菜单 ]\n";
cout << "===========================\n";
cout << "【1.添加图书】【2.打印书库】\n\n";
cout << "【3.查找图书】【4.删除图书】\n\n";
cout << "【5.修改图书】【6.退出系统】\n";
cout << "===========================\n";
cout << "请输入对应功能序号:\n";
}
void Show_LowMenu_select()
{
cout << "\n===========================\n";
cout << "[ 二级菜单 ]\n";
cout << "===========================\n";
cout << "1.[书名查询] 2.[价格查询] \n\n";
cout << "3.[ISBN查询] 4.[出版社查询]\n\n";
cout << "5.[作者查询] 6.[序号查询]\n\n";
cout << "7.[返回主菜单] \n";
cout << "===========================\n";
cout << "请输入对应功能序号:\n";
}
void Show_LowMenu_delect()
{
cout << "\n===========================\n";
cout << "[ 二级菜单 ]\n";
cout << "===========================\n";
cout << "1.[书名删除] 2.[ISBN删除] \n\n";
cout << "3.[序号删除] 4.[返回主菜单]\n";
cout << "===========================\n";
cout << "请输入对应功能序号:\n";
}
void Show_LowMenu_update()
{
cout << "\n===========================\n";
cout << "[ 二级菜单 ]\n";
cout << "===========================\n";
cout << "1.[修改书名] 2.[修改价格] \n\n";
cout << "3.[修改ISBN] 4.[修改出版社]\n\n";
cout << "5.[修改作者] 6.[返回主菜单]\n";
cout << "===========================\n";
cout << "请输入对应功能序号:\n";
}
int main()
{
string book_name, book_isbn, book_publisher, book_author;
string new_bookname, new_bookisbn, new_bookpublisher, new_bookauthor;
float book_price;
float new_bookprice;
BookE b1;//初始化b1对象,书本
BookEList list;//初始化list对象,书本的各项信息
char flag_insert = 'Y', flag_search = 'Y', flag_update = 'Y', flag_delete = 'Y';//设置flag值,用于控制循环
int k;
while (1)
{
//利用switch语句实现图书管理系统菜单
list.readFile();
LOOP:Show_Menu();
cin >> k;
if (k == 1 || k == 2 || k == 3 || k == 4 || k == 5 || k == 6)
{
switch (k)
{
case 1:
{
cout << "功能:1.【图书录入】" << endl;
system("cls");
cin >> b1;//插入流重载
cout << "是否录入该图书(Y/N):";
cin >> flag_insert;
if (((flag_insert == 'Y') || (flag_insert == 'y')))
{
list.insert(b1);
system("cls");
cout << "已经录入该书本" << endl;
break;
}
else
{
system("cls");
cout << "未录入" << endl;
break;
}
}
case 2:
{
cout << "功能:2.【打印书库】" << endl;
system("cls");
list.showAll();
break;
}
case 3:
{
cout << "功能:3.【查找图书】" << endl;
system("cls");
while (1)
{
LOOP3:Show_LowMenu_select();
cin >> k;
if (k == 1 || k == 2 || k == 3 || k == 4 || k == 5 || k == 6 || k == 7)
{
switch (k)
{
case 1:
{
system("cls");
cout << "功能:1.[书名查找]" << endl;
cout << "请输入需要查找的图书名:";
cin >> book_name;
int sel_1 = list.search_name(book_name);
if (sel_1 >= 0)
{
system("cls");
cout << "查询到以下图书:" << endl;
list.showBook_title();
list.showBook(sel_1);
}
else
{
system("cls");
cout << "没有找到该图书信息" << endl;
}
break;
}
case 2:
{
system("cls");
cout << "功能:2.[价格查找]" << endl;
cout << "请输入需要查找的图书价格:";
cin >> book_price;
int sel_2 = list.search_price(book_price);
if (sel_2 >= 0)
{
system("cls");
cout << "查询到以下图书:" << endl;
list.showBook_title();
list.showBook(sel_2);
}
else
{
system("cls");
cout << "没有找到该图书信息" << endl;
}
break;
}
case 3:
{
system("cls");
cout << "功能:3.[ISBN查找]" << endl;
cout << "请输入需要查找的ISBN:";
cin >> book_isbn;
int sel_3 = list.search_isbn(book_isbn);
if (sel_3 >= 0)
{
system("cls");
cout << "查询到以下图书:" << endl;
list.showBook_title();
list.showBook(sel_3);
}
else
{
system("cls");
cout << "没有找到该图书信息" << endl;
}
break;
}
case 4:
{
system("cls");
cout << "功能:4.[出版社查找]" << endl;
cout << "请输入需要查找的出版社名:";
cin >> book_publisher;
int sel_4 = list.search_publisher(book_publisher);
if (sel_4 >= 0)
{
system("cls");
cout << "查询到以下图书:" << endl;
list.showBook_title();
list.showBook(sel_4);
}
else
{
system("cls");
cout << "没有找到该图书信息" << endl;
}
break;
}
case 5:
{
system("cls");
cout << "功能:5.[作者查找]" << endl;
cout << "请输入需要查找的作者名:";
cin >> book_author;
int sel_5 = list.search_author(book_author);
if (sel_5 >= 0)
{
system("cls");
cout << "查询到以下图书:" << endl;
list.showBook_title();
list.showBook(sel_5);
}
else
{
system("cls");
cout << "没有找到该图书信息" << endl;
}
break;
}
case 6:
{
system("cls");
cout << "功能:6.[序号查询]" << endl;
cout << "请输入需要查找的图书序号:";
int sel_6;
cin >> sel_6;
system("cls");
cout << "查询到以下图书:" << endl;
list.showBook(sel_6 - 1);
break;
}
case 7:
{
system("cls");
goto LOOP;
}
int n = getchar();
}
}
else
{
system("cls");
cout << "请输入正确的功能序号\n";
goto LOOP3;
}
}
}
case 4:
{
cout << "功能:4.【删除图书】" << endl;
system("cls");
while (1)
{
LOOP4: Show_LowMenu_delect();
cin >> k;
if (k == 1 || k == 2 || k == 3 || k == 4)
{
switch (k)
{
case 1:
{
system("cls");
cout << "功能:1.[书名删除]" << endl;
cout << "请输入需要删除的书名:";
cin >> book_name;
int del_1 = list.search_name(book_name);
if (del_1 >= 0)
{
list.deleted(del_1);
system("cls");
cout << "图书已删除" << endl;
}
else
{
system("cls");
cout << "没有找到该图书" << endl;
}
break;
}
case 2:
{
system("cls");
cout << "功能:2.[ISBN删除]" << endl;
cout << "请输入需要删除的ISBN:";
cin >> book_isbn;
int del_2 = list.search_isbn(book_isbn);
if (del_2 >= 0)
{
list.deleted(del_2);
system("cls");
cout << "图书已删除" << endl;
}
else
{
system("cls");
cout << "没有找到该图书" << endl;
}
break;
}
case 3:
{
system("cls");
cout << "功能:3.[序号删除]" << endl;
cout << "请输入需要删除的图书序号:";
int del_3;
cin >> del_3;
list.deleted(del_3 - 1);
system("cls");
cout << "该图书已删除";
break;
}
case 4:
{
system("cls");
goto LOOP;
}
int n = getchar();
}
}
else
{
system("cls");
cout << "请输入正确的功能序号\n";
goto LOOP4;
}
}
}
case 5:
{
cout << "功能:5.【修改图书】" << endl;
system("cls");
while (1)
{
LOOP5: Show_LowMenu_update();
cin >> k;
if (k == 1 || k == 2 || k == 3 || k == 4 || k == 5 || k == 6)
{
switch (k)
{
case 1:
{
system("cls");
cout << "功能:1.[修改书名]" << endl;
cout << "请输入需要修改的图书名:";
cin >> book_name;
int upd_1 = list.search_name(book_name);//查找要修改的相应图书信息
BookE b = list.bookInfor(upd_1);//返回该本图书的信息
if (upd_1 >= 0)
{
cout << "请输入该图书新的图书名:";
cin >> new_bookname;
b.setName(new_bookname);
list.update(upd_1, b);
system("cls");
cout << "已修改,信息如下:" << endl;
list.showBook_title();
list.showBook(upd_1);
}
else
{
system("cls");
cout << "没有找到该图书信息" << endl;
}
break;
}
case 2:
{
system("cls");
cout << "功能:2.[修改价格]" << endl;
cout << "请输入需要修改价格的图书ISBN:";//因为isbn能够确定是哪本书
cin >> book_isbn;
int upd_2 = list.search_isbn(book_isbn);//查找要修改的相应图书信息
BookE b = list.bookInfor(upd_2);//返回该本图书的信息
if (upd_2 >= 0)
{
cout << "请输入该图书新的价格:";
cin >> new_bookprice;
b.setPrice(new_bookprice);
list.update(upd_2, b);
system("cls");
cout << "已修改,信息如下:" << endl;
list.showBook_title();
list.showBook(upd_2);
}
else
{
system("cls");
cout << "没有找到该图书信息" << endl;
}
break;
}
case 3:
{
system("cls");
cout << "功能:3.[修改isbn]" << endl;
cout << "请输入需要修改的ISBN:";//因为isbn能够确定是哪本书
cin >> book_isbn;
int upd_3 = list.search_isbn(book_isbn);//查找要修改的相应图书信息
BookE b = list.bookInfor(upd_3);//返回该本图书的信息
if (upd_3 >= 0)
{
cout << "请输入该图书新的ISBN:";
cin >> new_bookisbn;
b.setIsbn(new_bookisbn);
list.update(upd_3, b);
system("cls");
cout << "已修改,信息如下:" << endl;
list.showBook_title();
list.showBook(upd_3);
}
else
{
system("cls");
cout << "没有找到该图书信息" << endl;
}
break;
}
case 4:
{
system("cls");
cout << "功能:4.[修改出版社]" << endl;
cout << "请输入需要修改出版社的图书ISBN:";//因为isbn能够确定是哪本书
cin >> book_isbn;
int upd_4 = list.search_isbn(book_isbn);//查找要修改的相应图书信息
BookE b = list.bookInfor(upd_4);//返回该本图书的信息
if (upd_4 >= 0)
{
cout << "请输入该图书新的出版社名:";
cin >> new_bookpublisher;
b.setPublisher(new_bookpublisher);
list.update(upd_4, b);
system("cls");
cout << "已修改,信息如下:" << endl;
list.showBook_title();
list.showBook(upd_4);
}
else
{
system("cls");
cout << "没有找到该图书信息" << endl;
}
break;
}
case 5:
{
system("cls");
cout << "功能:5.[修改作者]" << endl;
cout << "请输入需要修改作者的图书ISBN:";//因为isbn能够确定是哪本书
cin >> book_isbn;
int upd_5 = list.search_isbn(book_isbn);//查找要修改的相应图书信息
BookE b = list.bookInfor(upd_5);//返回该本图书的信息
if (upd_5 >= 0)
{
cout << "请输入该图书新的作者名:";
cin >> new_bookauthor;
b.setAuthor(new_bookauthor);
list.update(upd_5, b);
system("cls");
cout << "已修改,信息如下:" << endl;
list.showBook_title();
list.showBook(upd_5);
}
else
{
system("cls");
cout << "没有找到该图书信息" << endl;
}
break;
}
case 6:
{
system("cls");
goto LOOP;
}
int n = getchar();
}
}
else
{
system("cls");
cout << "请输入正确的功能序号\n";
goto LOOP5;
}
}
}
case 6:
{
system("cls");
list.writeFile();
cout << "已退出系统" << endl;
exit(0);
}
int n = getchar();
}
}
else
{
system("cls");
cout << "请输入正确的功能序号\n";
goto LOOP;
}
}
return 0;
}