c++面向对象程序设计:
媒体库信息管理系统的设计和实现
一、系统分析:
1.开发背景:
图书馆中的资料很多,如果能分类对其资料流通进行管理,将会带来很多方便,因此需要有一个媒体库管理系统。 图书馆共有三大类物品资料:图书、视频光盘、图画。 这三类物品共同具有的属性有:编号、标题、作者、评级(未评级,一般,成人,儿童) 等。其中图书类增加出版社、ISBN 号、页数等信息;视频光盘类增加出品者的名字、出品 年份和视频时长等信息;图画类增加出品国籍、作品的长和宽(以厘米计,整数)等信息。
2.系统开发的意义:
(1)记录图书馆的物品信息
(2)对图书馆物品信息的查询和修改
(3)显示图书馆所有物品的信息
(4)删除图书馆某物品的信息
(5)实现从文件读入物品的信息或将物品信息存入到文件中
3.功能要求:
(1)添加物品:主要完成图书馆三类物品信息的添加,要求编号唯一。当添加了重复
的编号时,则提示数据添加重复并取消添加;当物品库已满,则提示不能再添加新的数据。
(2)查询物品
可按照三种方式来查询物品,分别为:
按标题查询:输入标题,输出所查询的信息,若不存在该记录,则提示“该标题不存在!”;
按编号查询:输入编号,输出所查询的信息,若不存在该记录,则提示“该编号不存在!”;
按类别查询:输入类别,输出所查询的信息,若不存在记录,则提示“该类别没有物品!”;
(3)显示物品库:输出当前物品库中所有物品信息,每条记录占据一行。
(4)编辑物品:可根据查询结果对相应的记录进行修改,修改时注意编号的唯一性。
(5)删除物品:主要完成图书馆物品信息的删除。如果当前物品库为空,则提示“物
品库为空!”,并返回操作;否则,输入要删除的编号,根据编号删除该物品的记录,如果该
编号不在物品库中,则提示“该编号不存在”。
(6)统计信息
输出当前物品库中总物品数,以及按物品类别,统计出当前物品中各类别的物品数并显
示。
(7)物品存盘:将当前程序中的物品信息存入文件中。
(8)读出物品:从文件中将物品信息读入程序。
二、系统设计:
1.程序流程图:
2.项目设计思路:
(1)因为图书馆的物品有三类:图书、视频光盘、图画,所以定义了三个类:
BOOK(图书)
Picture(图画)
Video(视频光盘)
三个类含有公有属性:
string number;
string title;
string writer;
string grade;
分别表示编号、标题、作者、评级。
并且由于这三个类需要完成多种相同的功能,所以定义一个基类:project,并通过这个基类派生出:
Book(图书)
Picture(图画)
Video(视频光盘)
在基类中定义了
1. virtual void add() {};//添加函数
2.virtual void findnum(){};//查找编号函数
3.virtual void findtitle() {};//查找标题函数
4. virtual void findtype() {};//查找类别函数
5.virtual void output() {};//输出信息函数
6.virtual void baocun() {};//物品存盘函数
7.virtual void duqu() {};//物品读盘函数
七个虚函数,并在派生类中进行重写。
(2)添加三个类的头文件,在头文件中定义类和类的成员函数
Book类:
1.void add(Book* book, int* count); //用于添加图书类物品
2.bool findtitle(Book* book, string title,int *count); //用于确定是否存在该标题的图书
3.bool findnum(Book* book, string num, int* count); //用于确定是否存在该编号的图书
4.void findtype(Book* book, string type, int* count); //用于确定是否存在该类型的物品
5.void output(Book* book, int* count); //用于输出该类别的物品
6.void baocun(Book* book, int* count);
7.void duqu(Book* book, int* count);
Picture类:
1.void add(Picture* picture, int* count);//用于添加图画类物品
2.bool findtitle(Picture* picture, string title, int* count);//用于确定是否存在该标题的图片
3. bool findnum(Picture* picture, string num, int* count);//用于确定是否存在该编号的图片
4.void findtype(Picture* picture, string type, int* count);//用于确定是否存在该类型的物品
5.void output(Picture* picture, int* count);//用于输出该类别的物品
6.void baocun(Picture* picture, int* count);
7.void duqu(Picture* picture, int* count);
Video类:
1.void add(Video* video, int* count);
2.bool findtitle(Video* video, string title, int* count);//用于确定是否存在该标题的书籍
3.bool findnum(Video* video, string num, int* count);//用于确定是否存在该编号的视频光盘
4.void findtype(Video* video, string type, int* count);用于确定是否存在该类型的物品
5.void output(Video* video, int* count);//用于输出该类别的物品
6.void baocun(Video* video, int* count);
7.void duqu(Video* video, int* count);
因为需要对三个类的对象数组进行处理,对所有的编号进行查询,所以需要再定义一个总的功能类,设置为这三个类的友元类,通过这个功能类来对所有的对象数组操作,同时在该类设置主菜单,退出函数,
manage类:
1.void add(Book* book, int*count_b,Picture *picture,int *count_p,Video *video,int *count_v);
2.void showsubmenu();//子菜单
3.void esc();
4.void findtitle(Book* book, int* count_b, Picture* picture, int* count_p, Video* video, int* count_v,string title);
//查找标题的功能函数
5.void finnum(Book* book, int* count_b, Picture* picture, int* count_p, Video* video, int* count_v, string number);
//查找编号的功能函数
6.void findproject(Book* book, int* count_b, Picture* picture, int* count_p, Video* video, int* count_v, string type);
//根据类别查找的功能函数
7.void showobject(Book* book, int* count_b, Picture* picture, int* count_p, Video* video, int* count_v);
//输出物品库所有物品信息的功能函数
8.void remove(Book* book, int* count_b, Picture* picture, int* count_p, Video* video, int* count_v,string num);
//根据编号删除物品信息的功能函数
9.void xiugai(Book* book, int* count_b, Picture* picture, int* count_p, Video* video, int* count_v, string num);
//根据编号查询时,修改该物品的信息,同时注意编号唯一性
10.void baocun(Book* book, int* count_b, Picture* picture, int* count_p, Video* video, int* count_v);
11.void duqu(Book* book, int* count_b, Picture* picture, int* count_p, Video* video, int* count_v);
添加Book.h,Picture.h,Video.h,manage.h并定义函数,在Book.cpp,Picture.cpp,Video.cpp,manage.cpp中实现函数的主体
(3)在main函数中定义基类的指针对象,并通过该指针指向不同的函数,来实现不同的功能。
3.函数功能介绍及构想:
(1)添加函数:在manage主功能函数中,定义总的添加函数,并定义三个类的对象,通过判断添加物品的类别来调用不同类的添加函数,同时因为编号唯一,所以需要在添加编号的同时,对编号进行判重,若编号重复,则需要重新添加。
(2)查询函数:定义一个子菜单功能,根据操作者的选择,来判断调用哪个功能,三种查找的函数中,均需要定义三个类的对象,通过类的对象来调用相应的类的函数。
(3)显示函数:输出所有的物品信息,需要定义三个类的对象,通过类的对象来调用相应类别的输出函数。
(4)编辑函数:编辑物品的信息是根据输入的编号,来查找是否存在该编号的物品,若存在,则对该编号的物品信息进行重新录入,在录入的过程中需要注意编号的唯一性,即在录入新的编号时,需要判断该编号是否与以前的编号重复。
(5)删除函数:删除函数是通过查找到需要删除的物品的编号,用该物品以后的物品信息,全部对前一个物品的信息进行覆盖,若查找到的编号为最后一个物品的编号,则直接物品数量-1,则在输出时无法输出最后一个物品的信息。
(6)统计信息:定义三个变量,分别记录Book类,Picture类,Video类的数量,统计时分别输出即可。
(7)物品存盘和(8)物品存盘即为文件流操作,对文件信息的读取
(9)退出程序:当输入0时,就能退出程序。
在main函数中,整个流程由一个while死循环进行控制,只有在输入0时,才会跳出程序。
4.系统运行界面及结果(展示部分功能):
(1)添加物品(以图书类物品为例)
(2)查询物品(按编号查询为例):
(3)显示物品库:
(4)物品存盘:
三、程序源码:
project.h:
#pragma once
#include<string>
using namespace std;
class project
{
public:
virtual void add() {};
virtual void findnum(){};
virtual void findtitle() {};
virtual void findtype() {};
virtual void output() {};
virtual void baocun() {};
virtual void duqu() {};
string number, title, writer, grade;//几个类共有的成员变量
private:
protected:
};
Book.h:
#pragma once
#include<string>
#include"project.h"
#include"manage.h"
using namespace std;
class Book:public project
{
public:
friend class manage;
void add(Book* book, int* count);
bool findtitle(Book* book, string title,int *count);//用于确定是否存在该标题的书籍
bool findnum(Book* book, string num, int* count);
void findtype(Book* book, string type, int* count);
void output(Book* book, int* count);
void baocun(Book*book,int *count);
void duqu(Book*book,int *count);
private:
string publisher,ISBN;int page;
};
Book.cpp:
#include<string>
#include<iostream>
#include<fstream>
#include"book.h"
using namespace std;
void Book::add(Book* book, int* count) {
cout << "请输入书籍名称:" << endl;
cin >> title;
cout << "请输入书籍作者:" << endl;
cin >> writer;
bool xunhuan = true;
while (true)
{
if (xunhuan == false)
{
cout << "输入信息不符合要求,请重新书籍评级(未评级,一般,成人,儿童):" << endl;
cin >> grade;
}
else {
cout << "请输入书籍评级(未评级,一般,成人,儿童):" << endl;
cin >> grade;
}
if (grade.compare("未评级")==0 ||grade.compare("一般") ==0||grade.compare("成人")==0 ||grade.compare("儿童")==0)
{
break;
}
else {
xunhuan = false;
continue;
}
}
cout << "请输入出版社:" << endl;
cin >> publisher;
cout << "请输入ISBN号:" << endl;
cin >> ISBN;
cout << "请输入该书籍页数" << endl;
cin >> page;
book[*count].title = title;
book[*count].writer = writer;
book[*count].grade = grade;
book[*count].publisher = publisher;
book[*count].ISBN = ISBN;
book[*count].page = page;
//对象的属性等于读入的相应的值
(*count)++;
}
//添加函数,直接添加除编号以外的其他信息,在这里不添加编号是为了方便判断编号是否重复
bool Book::findtitle(Book* book, string title,int *count)
{
if (book[*count].title.compare(title) == 0)//比较两个字符串是否相同
{
return true;
}
else
return false;
}
//定义bool类型函数,用于判断标题是否重复,在manage总函数中用循环,去判断每个Book对象的标题
bool Book::findnum(Book* book, string num, int* count)
{
if (book[*count].number.compare(num) == 0)//比较两个字符串是否相同
{
return true;
}
else
return false;
}
//定义bool类型函数,用于判断编号是否重复,在manage总函数中用循环,判断添加的编号或者重新添加的编号是否重复。
void Book::findtype(Book* book, string type, int* count)
{
if (*count == 1)
cout << "该类别没有物品" << endl;
else
{
for (int i = 1; i < *count; i++)
{
cout << "编号:" << book[i].number << " " << "名称:" << book[i].title << " " << "作者:" << book[i].writer << " "
<< "评级:" << book[i].grade << " " << "出版社:" << book[i].publisher << " " << "ISBN号:" << book[i].ISBN << " " << "页数:" << book[i].page << endl;
}
}
}
//用于查找函数中的根据类别输出,如果*count=1,则说明,Book类物品库不含有物品,若不等,则逐个输出
void Book::output(Book* book, int* count)
{
for (int i = 1; i < *count; i++)
{
cout << "编号:" << book[i].number << " " << "名称:" << book[i].title << " " << "作者:" << book[i].writer << " "
<< "评级:" << book[i].grade << " " << "出版社:" << book[i].publisher << " " << "ISBN号:" << book[i].ISBN<< " " << "页数:" << book[i].page << endl;
}
}
//输出Book物品库中的所有物品
void Book::baocun(Book* book, int* count)
{
ofstream ofs;
ofs.open("book.txt",ios::out);
for (int i = 1; i < *count ; i++)
{
ofs << book[i].number << " " << book[i].title << " " << book[i].writer << " " << book[i].grade << " " << book[i].ISBN << " " << book[i].publisher << " " << book[i].page << endl;
}
ofs.close();
}
void Book::duqu(Book* book, int* count)
{
ifstream ifs;
ifs.open("book.txt", ios::in);
if (!ifs.is_open()) {
cout << "Book类物品库打开失败!" << endl;
return;
}
while (ifs >>book[*count].number>>book[*count].title>>book[*count].writer >> book[*count].grade>> book[*count].ISBN >>book[*count].publisher>>book[*count].page)
{
(*count)++;
}
ifs.close();
}
Picture.h:
#pragma once
#include<string>
#include"project.h"
#include"manage.h"
using namespace std;
class Picture:public project
{
public:
friend class manage;
void add(Picture* picture, int* count);
bool findtitle(Picture* picture, string title, int* count);//用于确定是否存在该标题的图片
bool findnum(Picture* picture, string num, int* count);//用于确定是否存在该编号的图片
void findtype(Picture* picture, string type, int* count);//用于确定是否存在该类型的物品
void output(Picture* picture, int* count);//用于输出该类别的物品
void baocun(Picture* picture, int* count);
void duqu(Picture* picture, int* count);
private:
string nationality; int length, width;//长和宽,单位为厘米
};
Picture.cpp:
#include<string>
#include<iostream>
#include<fstream>
#include"Picture.h"
using namespace std;
void Picture::add(Picture* picture, int* count) {
cout << "请输入图画名称:" << endl;
cin >> title;
cout << "请输入图画作者:" << endl;
cin >> writer;
bool xunhuan = true;
while (true)
{
if (xunhuan == false)
{
cout << "输入信息不符合要求,请重新图画评级(未评级,一般,成人,儿童):" << endl;
cin >> grade;
}
else {
cout << "请输入图画评级(未评级,一般,成人,儿童):" << endl;
cin >> grade;
}
if (grade.compare("未评级") == 0 || grade.compare("一般") == 0 || grade.compare("成人") == 0 || grade.compare("儿童") == 0)
{
break;
}
else {
xunhuan = false;
continue;
}
}
cout << "请输入图画出品国籍:" << endl;
cin >> nationality;
cout << "请输入图画长度(单位为厘米):" << endl;
cin >> length;
cout << "请输入图画宽度(单位为厘米)" << endl;
cin >> width;
picture[*count].title = title;
picture[*count].writer = writer;
picture[*count].grade = grade;
picture[*count].nationality = nationality;
picture[*count].length = length;
picture[*count].width = width;
//对象的属性等于读入的相应的值
(*count)++;
}
//添加函数,直接添加除编号以外的其他信息,在这里不添加编号是为了方便判断编号是否重复
bool Picture::findtitle(Picture* picture, string title, int* count)
{
if (picture[*count].title.compare(title) == 0)//比较两个字符串是否相同
{
return true;
}
else
return false;
}
//定义bool类型函数,用于判断标题是否重复,在manage总函数中用循环,去判断每个picture对象的标题
bool Picture::findnum(Picture* picture, string num, int* count)
{
if (picture[*count].number.compare(num) == 0)//比较两个字符串是否相同
{
return true;
}
else
return false;
}
//定义bool类型函数,用于判断编号是否重复,在manage总函数中用循环,判断添加的编号或者重新添加的编号是否重复。
void Picture::findtype(Picture* picture, string type, int* count)
{
if (*count == 1)
{
cout << "该类别没有物品!" << endl;
}
else
{
for (int i = 1; i < *count; i++)
{
cout << "编号:" << picture[i].number << " " << "名称:" << picture[i].title << " " << "作者:" << picture[i].writer << " "
<< "评级:" << picture[i].grade << " " << "出品国籍:" << picture[i].nationality << " " << "图画长度:" << picture[i].length << "厘米" << " " << "图画宽度:" << picture[i].width << " 厘米" << endl;
}
}
}
//用于查找函数中的根据类别输出,如果*count=1,则说明,Picture类物品库不含有物品,若不等,则逐个输出
void Picture::output(Picture* picture, int* count)
{
for (int i = 1; i < *count; i++)
{
cout << "编号:" << picture[i].number << " " << "名称:" << picture[i].title << " " << "作者:" << picture[i].writer << " "
<< "评级:" << picture[i].grade << " " << "出品国籍:" << picture[i].nationality << " " << "图画长度:" << picture[i].length << "厘米" << " " << "图画宽度:" << picture[i].width << " 厘米" << endl;
}
}
//输出Picture物品库中的所有物品
void Picture::baocun(Picture* picture, int* count)
{
ofstream ofs;
ofs.open("picture.txt", ios::out);
for (int i = 1; i < *count; i++)
{
ofs << picture[i].number << " " << picture[i].title << " " << picture[i].writer << " " << picture[i].grade << " " << picture[i].nationality << " " << picture[i].length << " " << picture[i].width << endl;
}
ofs.close();
}
void Picture::duqu(Picture* picture, int* count)
{
ifstream ifs;
ifs.open("picture.txt", ios::in);
if (!ifs.is_open()) {
cout << "Picture类物品库打开失败!" << endl;
return;
}
while (ifs >> picture[*count].number >> picture[*count].title >> picture[*count].writer >> picture[*count].grade >> picture[*count].nationality>> picture[*count].length >> picture[*count].width)
{
(*count)++;
}
ifs.close();
}
Video.h:
#pragma once
#include<string>
#include"project.h"
#include"manage.h"
using namespace std;
class Video:public project
{
public:
friend class manage;
void add(Video* video, int* count);
bool findtitle(Video* video, string title, int* count);//用于确定是否存在该标题的书籍
bool findnum(Video* video, string num, int* count);
void findtype(Video* video, string type, int* count);
void output(Video* video, int* count);
void baocun(Video* video, int* count);
void duqu(Video* video, int* count);
private:
string publisher, duration;int year;
};
Video.cpp:
#include<string>
#include<iostream>
#include<fstream>
#include"video.h"
using namespace std;
void Video::add(Video* video, int* count) {
cout << "请输入视频光盘名称:" << endl;
cin >> title;
cout << "请输入视频光盘作者:" << endl;
cin >> writer;
bool xunhuan = true;
while (true)
{
if (xunhuan == false)
{
cout << "输入信息不符合要求,请重新视频光盘评级(未评级,一般,成人,儿童):" << endl;
cin >> grade;
}
else {
cout << "请输入视频光盘评级(未评级,一般,成人,儿童):" << endl;
cin >> grade;
}
if (grade.compare("未评级") == 0 || grade.compare("一般") == 0 || grade.compare("成人") == 0 || grade.compare("儿童") == 0)
{
break;
}
else {
xunhuan = false;
continue;
}
}
cout << "请输入出品者的名字:" << endl;
cin >> publisher;
cout << "请输入出品年份:" << endl;
cin >> year;
cout << "请输入视频长度(xx小时xx分钟xx秒)" << endl;
cin >> duration;
video[*count].title = title;
video[*count].writer = writer;
video[*count].grade = grade;
video[*count].publisher = publisher;
video[*count].year = year;
video[*count].duration = duration;
//对象的属性等于读入的相应的值
(*count)++;
}
//添加函数,直接添加除编号以外的其他信息,在这里不添加编号是为了方便判断编号是否重复
bool Video::findtitle(Video* video, string title, int* count)
{
if (video[*count].title.compare(title) == 0)//比较两个标题是否相同
{
return true;
}
else
return false;
}
//定义bool类型函数,用于判断标题是否重复,在manage总函数中用循环,去判断每个video对象的标题
bool Video::findnum(Video* video, string num, int* count)
{
if (video[*count].number.compare(num) == 0)//比较两个编号是否相同
{
return true;
}
else
return false;
}
//定义bool类型函数,用于判断编号是否重复,在manage总函数中用循环,判断添加的编号或者重新添加的编号是否重复。
void Video::findtype(Video* video, string type, int* count)
{
if (*count == 1)
{
cout << "该类别没有物品!" << endl;
}
else
{
for (int i = 1; i < *count; i++)
{
cout << "编号:" << video[i].number << " " << "名称:" << video[i].title << " " << "作者:" << video[i].writer << " "
<< "评级:" << video[i].grade << " " << "出版者的名字:" << video[i].publisher << " " << "出品年份:" << video[i].year << " " << "视频时长:" << video[i].duration << endl;
}
}
}
//用于查找函数中的根据类别输出,如果*count=1,则说明,Video类物品库不含有物品,若不等,则逐个输出
void Video::output(Video* video, int* count)
{
for (int i = 1; i < *count; i++)
{
cout << "编号:" << video[i].number << " " << "名称:" << video[i].title << " " << "作者:" << video[i].writer << " "
<< "评级:" << video[i].grade << " " << "出版者的名字:" << video[i].publisher << " " << "出品年份:" << video[i].year << " " << "视频时长:" << video[i].duration << endl;
}
}
//输出Video物品库中的所有物品
void Video::baocun(Video* video, int* count)
{
ofstream ofs;
ofs.open("video.txt", ios::out);
for (int i = 1; i < *count ; i++)
{
ofs << video[i].number << " " << video[i].title << " " << video[i].writer << " " << video[i].grade << " " << video[i].publisher << " " << video[i].year << " " << video[i].duration << endl;
}
ofs.close();
}
void Video::duqu(Video* video, int* count)
{
ifstream ifs;
ifs.open("video.txt", ios::in);
if (!ifs.is_open()) {
cout << "Video类物品库打开失败!" << endl;
return;
}
while (ifs >> video[*count].number >> video[*count].title >> video[*count].writer >> video[*count].grade >> video[*count].publisher >> video[*count].year >> video[*count].duration)
{
(*count)++;
}
ifs.close();
}
manage.h:
#pragma once
#include<string>
#include<iostream>
class Book;
class Video;
class Picture;
using namespace std;
class manage
{
public:
void add(Book* book, int*count_b,Picture *picture,int *count_p,Video *video,int *count_v);
void showsubmenu();//子菜单
void esc();
void findtitle(Book* book, int* count_b, Picture* picture, int* count_p, Video* video, int* count_v,string title);
//查找标题的功能函数
void finnum(Book* book, int* count_b, Picture* picture, int* count_p, Video* video, int* count_v, string number);
//查找编号的功能函数
void findproject(Book* book, int* count_b, Picture* picture, int* count_p, Video* video, int* count_v, string type);
//根据类别查找的功能函数
void showobject(Book* book, int* count_b, Picture* picture, int* count_p, Video* video, int* count_v);
//输出物品库所有物品信息的功能函数
void remove(Book* book, int* count_b, Picture* picture, int* count_p, Video* video, int* count_v,string num);
//根据编号删除物品信息的功能函数
void xiugai(Book* book, int* count_b, Picture* picture, int* count_p, Video* video, int* count_v, string num);
//根据编号查询时,修改该物品的信息,同时注意编号唯一性
void baocun(Book* book, int* count_b, Picture* picture, int* count_p, Video* video, int* count_v);
void duqu(Book* book, int* count_b, Picture* picture, int* count_p, Video* video, int* count_v);
private:
};
manage.cpp:
#include<string>
#include<iostream>
#include<fstream>
#include"manage.h"
#include"book.h"
#include"picture.h"
#include"video.h"
using namespace std;
//manage为总的功能类,因为需要对编号进行查重,查找等操作,有三个对象数组需要进行处理,所以需要
//定义一个总的功能类函数来对三个对象数组进行操作,将manage当做其他三个类的友元类
//来调用其他三个类的成员函数,完成要求的功能
void manage::showsubmenu()//查找物品的子菜单
{
cout << "*************************" << endl;
cout << "*******1.按标题查询******" << endl;
cout << "*******2.按编号查询******" << endl;
cout << "*******3.按类别查询******" << endl;
cout << "*************************" << endl;
}
//菜单功能
void manage::esc() {
cout << "欢迎下次使用!" << endl;
system("pause");
exit(0);//退出程序的函数
}
//退出功能(0)
void manage::add(Book *book, int*count_b,Picture* picture, int* count_p, Video* video, int*count_v)
{
Book book1; Picture picture1; Video video1;//定义三个类的对象,用来调用三个类的成员函数
while (true)//定义死循环,以防止用户输入的某些数据不符合要求
{
bool jump = false;
cout << "请输入您添加的物品种类(图书、图画、视频光盘):" << endl;
string type; cin >> type;
if (!type.compare("图书") && *count_b > 70)
{
cout << "图书类数据库已满,请勿添加新的数据!"<<endl;
return;
}
else if (!type.compare("视频光盘") && *count_v> 70)
{
cout << "视频光盘类数据库已满,请勿添加新的数据!" << endl;
return;
}
else if (!!type.compare("图画") && *count_p > 70)
{
cout << "图画类数据库已满,请勿添加新的数据!" << endl;
return;
}//三个if语句用于判断三个类的物品库是否已满
cout << "请输入需要添加该类物品的个数:" << endl;
int shu; cin >> shu;
for (int i = 1; i < shu + 1; i++)
{
bool cunzai = false;
if (!type.compare("图书"))
cout << "您添加的第" << i << "个图书类物品信息为:" << endl;
else if (!type.compare("视频光盘"))
cout<< "您添加的第" << i << "个视频光盘类信息为:" << endl;
else if(!type.compare("图画"))
cout<<"您添加的第" << i << "个图画类物品信息为:" << endl;
cout << "请输入该物品的编号:" << endl;
string num; cin >> num;
//三个for循环用于查找编号是否重复,如果存在则提示用户重新添加
for (int j = 1; j < *count_b + 1; j++)
{
if (book[j].number == num)
{
cunzai = true;
break;
}
}
for (int j = 1; j < *count_p + 1; j++)
{
if (picture[j].number == num)
{
cunzai = true;
break;
}
}
for (int j = 1; j < *count_v + 1; j++)
{
if (video[j].number == num)
{
cunzai = true;
break;
}
}
if (cunzai == true)
{
cout << "该物品编号已存在!请重新添加该物品编号" << endl;
i--;
}
else
{//如果该编号不存在,根据用户选择添加类型,调用不同的类的添加函数
if (!type.compare("图书"))
{
book[*count_b].number = num;
book1.add(book, count_b);
}
else if (!type.compare("图画"))
{
picture[*count_p].number = num;
picture1.add(picture, count_p);
}
else if (!type.compare("视频光盘"))
{
video[*count_v].number = num;
video1.add(video, count_v);
}
}
//添加完最后一个物品后,jump变为true,并跳出for循环和while循环
if (i == shu)
jump = true;
if (jump == true)
break;
}
if (jump == true)
break;
}
}
void manage::findtitle(Book* book, int* count_b, Picture* picture, int* count_p, Video* video, int* count_v, string title)
{
Book b1; Picture p1; Video v1;
bool findbook=false, findpicture=false, findvideo = false;//初始值设置为false,认为没有找到到,若找到则将其改为true
//若三个值同时为false,则说明在该物品库中不存在该标题的物品
//调用三个函数的查找标题的函数,如果找到,则相应的bool值变为true,最后判断该标题是否存在
for (int i = 1; i < *count_b + 1; i++)
{
if (b1.findtitle(book, title,&i))
{
cout << "编号:" << book[i].number << " " << "名称:" << book[i].title << " " << "作者:" << book[i].writer << " "
<< "评级:" << book[i].grade << " " << "出版社:" << book[i].publisher << " " << "ISBN号:" << book[i].ISBN << " " << "页数:" << book[i].page << endl;
findbook = true;
}
}
for (int i = 1; i < *count_v + 1; i++)
{
if (v1.findtitle(video, title, &i))
{
cout << "编号:" << video[i].number << " " << "标题:" << video[i].title << " " << "作者:" << video[i].writer << " " << "评级:"
<< video[i].grade << " " << "出品者姓名:" << video[i].publisher << " " << "出品年份:" << video[i].year << " " << "视频时长:" << video[i].duration << endl;
}
}
for (int i = 1; i < *count_p + 1; i++)
{
if (p1.findtitle(picture, title, &i))
{
cout << "编号:" << picture[i].number << " " << "名称:" << picture[i].title << " " << "作者:" << picture[i].writer << " "
<< "评级:" << picture[i].grade << " " << "出品国籍:" << picture[i].nationality << " " << "图画长度:" << picture[i].length << "厘米" << " " << "图画宽度:" << picture[i].width << " 厘米" << endl;
}
}
if (!findbook&& !findpicture && !findvideo)
cout << "该标题不存在!" << endl;
}
void manage::finnum(Book* book, int* count_b, Picture* picture, int* count_p, Video* video, int* count_v, string number)
{
Book b1; Picture p1; Video v1;
bool findbook = false, findpicture = false, findvideo = false;
//类似于查找标题的总函数
for (int i = 1; i < *count_b + 1; i++)
{
if (b1.findnum(book,number,&i))
{
cout << "编号:" << book[i].number << " " << "名称:" << book[i].title << " " << "作者:" << book[i].writer << " "
<< "评级:" << book[i].grade << " " << "出版社:" << book[i].publisher << " " << "IBSN号:" << book[i].ISBN << " " << "页数:" << book[i].page << endl;
findbook = true;
}
}
for (int i = 1; i < *count_p + 1; i++)
{
if (p1.findnum(picture, number, &i))
{
cout << "编号:" << picture[i].number << " " << "名称:" << picture[i].title << " " << "作者:" << picture[i].writer << " "
<< "评级:" << picture[i].grade << " " << "出品国籍:" << picture[i].nationality << " " << "图画长度:" << picture[i].length << "厘米" << " " << "图画宽度:" << picture[i].width << " 厘米" << endl;
findpicture = true;
}
}
for (int i = 1; i < *count_v + 1; i++)
{
if (v1.findnum(video, number, &i))
{
cout << "编号:" << video[i].number << " " << "名称:" << video[i].title << " " << "作者:" << video[i].writer << " "
<< "评级:" << video[i].grade << " " << "出版者的名字:" << video[i].publisher << " " << "出品年份:" << video[i].year << " " << "视频时长:" << video[i].duration << endl;
findvideo = true;
}
}
if (!findbook && !findpicture &&!findvideo)
cout << "该编号不存在!" << endl;
}
void manage::findproject(Book* book, int* count_b, Picture* picture, int* count_p, Video* video, int* count_v, string type)
{
Book b1; Picture p1; Video v1;
if (!type.compare("图书"))
b1.findtype(book, type, count_b);
else if (!type.compare("图画"))
p1.findtype(picture, type, count_p);
else if (!type.compare("视频光盘"))
v1.findtype(video, type, count_v);
//根据需要查找的物品类别来进行查找
}
void manage::showobject(Book* book, int* count_b, Picture* picture, int* count_p, Video* video, int* count_v) {
Book b1; Picture p1; Video v1;
//输出三个类别的物品库含有的物品
cout << "图书类物品:" << endl;
b1.output(book, count_b);
cout << "图画类物品:" << endl;
p1.output(picture, count_p);
cout << "视频光盘类物品:" << endl;
v1.output(video, count_v);
}
void manage::remove(Book* book, int* count_b, Picture* picture, int* count_p, Video* video, int* count_v, string num)
{
//根据编号查询结果来删除物品的信息,利用后一个物品信息来覆盖需要删除物品的信息,而最后一个
//物品的信息无需删除,因为删除一个物品后,该类物品的数量会减少1,若删除的为最后一个物品信息
//则正好无法显示
Book b1; Picture p1; Video v1;
bool findbook = false, findpicture = false, findvideo = false;
for (int i = 1; i < *count_b + 1; i++)
{
if (b1.findnum(book, num, &i))
{
for (int j = i + 1; j < *count_b + 1; j++)
{
book[j - 1].number = book[j].number;
book[j - 1].title = book[j].title;
book[j - 1].writer = book[j].title;
book[j - 1].grade = book[j].grade;
book[j - 1].publisher = book[j].publisher;
book[j - 1].ISBN = book[j].ISBN;
book[j - 1].page = book[j].page;
}
findbook = true;
(*count_b)--;
}
}
for (int i = 1; i < *count_p + 1; i++)
{
if (p1.findnum(picture, num, &i))
{
for (int j = i + 1; j < *count_p + 1; j++)
{
picture[j - 1].number = picture[j].number;
picture[j - 1].title = picture[j].title;
picture[j - 1].writer = picture[j].title;
picture[j - 1].grade = picture[j].grade;
picture[j - 1].nationality = picture[j].nationality;
picture[j - 1].length = picture[j].length;
picture[j - 1].width = picture[j].width;
}
findpicture = true;
(*count_p)--;
}
}
for (int i = 1; i < *count_v + 1; i++)
{
if (v1.findnum(video, num, &i))
{
for (int j = i + 1; j < *count_v + 1; j++)
{
video[j - 1].number = video[j].number;
video[j - 1].title = video[j].title;
video[j - 1].writer = video[j].title;
video[j - 1].grade = video[j].grade;
video[j - 1].publisher = video[j].publisher;
video[j - 1].year = video[j].year;
video[j - 1].duration = video[j].duration;
}
findvideo = true;
(*count_v)--;
}
}
if (!findbook && !findpicture && !findvideo)
cout << "该编号不存在!" << endl;
}
void manage::xiugai(Book* book, int* count_b, Picture* picture, int* count_p, Video* video, int* count_v, string num)
{
Book b1; Picture p1; Video v1; bool found = true;
while (true)
{//在一个大的死循环中定义三个小的for循环,每个for循环用于检测每个类中是否有编号重复
for (int i = 1; i < *count_b + 1; i++)
{
bool found1 = false;
if (b1.findnum(book, num, &i))
{
cout << "请输入修改后的书籍编号:" << endl;
string number; cin >> number;
for (int j = 1; j < *count_b + 1; j++)
{
if (b1.findnum(book, number, &j))
{
cout << "该编号已存在!请重新添加" << endl;
found1 = true;
break;//定义found1,用于检验该编号是否已存在,若存在则跳出小循环
//再重新输入书籍编号,直至该编号不重复,修改该物品的其他信息
//修改完其他信息后,直接跳出死循环。
}
else if (p1.findnum(picture, number, &j))
{
cout << "该编号已存在!请重新添加" << endl;
found1 = true;
break;
}
else if (v1.findnum(video, number, &j))
{
cout << "该编号已存在!请重新添加" << endl;
found1 = true;
break;
}
}
if (found1)break;
book[i].number = number;
cout << "请输入修改后的书籍名称:" << endl;
cin >> book[i].title;
cout << "请输入修改后的书籍作者:" << endl;
cin >> book[i].writer;
cout << "请输入修改后的书籍评级(未评级,一般,成人,儿童)" << endl;
cin >> book[i].grade;
cout << "请输入修改后的出版社:" << endl;
cin >> book[i].publisher;
cout << "请输入修改后的ISBN号:" << endl;
cin >> book[i].ISBN;
cout << "请输入修改后的该书籍页数" << endl;
cin >> book[i].page;
found = false;
break;
}
if (!found)break;
}
for (int i = 1; i < *count_p + 1; i++)
{
bool found1 = false;
if (p1.findnum(picture, num, &i))
{
cout << "请输入修改后的图画编号:" << endl;
string number; cin >> number;
for (int j = 1; j < *count_p + 1; j++)
{
if (b1.findnum(book, number, &j))
{
cout << "该编号已存在!请重新添加" << endl;
found1 = true;
break;//定义found1,用于检验该编号是否已存在,若存在则跳出小循环
//再重新输入书籍编号,直至该编号不重复,修改该物品的其他信息
//修改完其他信息后,直接跳出死循环。
}
else if (p1.findnum(picture, number, &j))
{
cout << "该编号已存在!请重新添加" << endl;
found1 = true;
break;
}
else if (v1.findnum(video, number, &j))
{
cout << "该编号已存在!请重新添加" << endl;
found1 = true;
break;
}
}
if (found1)break;
picture[i].number = number;
cout << "请输入修改后的图画名称:" << endl;
cin >> picture[i].title;
cout << "请输入修改后的图画作者:" << endl;
cin >> picture[i].writer;
cout << "请输入修改后的图画评级(未评级,一般,成人,儿童)" << endl;
cin >> picture[i].grade;
cout << "请输入修改后的出品国籍:" << endl;
cin >> picture[i].nationality;
cout << "请输入修改后的图画长度(单位:厘米):" << endl;
cin >> picture[i].length;
cout << "请输入修改后的图画宽度(单位:厘米):" << endl;
cin >> picture[i].width;
found = false;
break;
}
if (!found)break;
}
for (int i = 1; i < *count_v + 1; i++)
{
bool found1 = false;
if (v1.findnum(video, num, &i))
{
cout << "请输入修改后的视频光盘编号:" << endl;
string number; cin >> number;
for (int j = 1; j < *count_v + 1; j++)
{
if (v1.findnum(video, number, &j))
{
cout << "该编号已存在!请重新添加" << endl;
found1 = true;
break;//定义found1,用于检验该编号是否已存在,若存在则跳出小循环
//再重新输入书籍编号,直至该编号不重复,修改该物品的其他信息
//修改完其他信息后,直接跳出死循环。
}
else if (p1.findnum(picture, number, &j))
{
cout << "该编号已存在!请重新添加" << endl;
found1 = true;
break;
}
else if (v1.findnum(video, number, &j))
{
cout << "该编号已存在!请重新添加" << endl;
found1 = true;
break;
}
}
if (found1)break;
video[i].number = number;
cout << "请输入修改后的视频光盘名称:" << endl;
cin >> video[i].title;
cout << "请输入修改后的视频光盘作者:" << endl;
cin >> video[i].writer;
cout << "请输入修改后的视频光盘评级(未评级,一般,成人,儿童)" << endl;
cin >> video[i].grade;
cout << "请输入修改后的视频光盘出品者姓名:" << endl;
cin >> video[i].publisher;
cout << "请输入修改后的视频光盘年份:" << endl;
cin >> video[i].year;
cout << "请输入修改后的视频光盘时长:" << endl;
cin >> video[i].duration;
found = false;
break;
}
if (!found)break;
}
if (!found)break;
}
}
void manage::baocun(Book* book, int* count_b, Picture* picture, int* count_p, Video* video, int* count_v)
{
Book b1; Picture p1; Video v1;
b1.baocun(book, count_b);
p1.baocun(picture, count_p);
v1.baocun(video, count_v);
cout << "物品信息已存入文件!" << endl;
}
void manage::duqu(Book* book, int* count_b, Picture* picture, int* count_p, Video* video, int* count_v)
{
Book b1; Picture p1; Video v1;
b1.duqu(book, count_b);
p1.duqu(picture, count_p);
v1.duqu(video, count_v);
cout << "物品读取成功!" << endl;
}
main.cpp:
#include<iostream>
#include<string>
#include"book.h"
#include"picture.h"
#include"video.h"
#include"manage.h"
using namespace std;
const int max = 210;
const int b_max = 70;
const int p_max = 70;
const int v_max = 70;
int b_count = 1;
int v_count = 1;
int p_count = 1;
Book b[b_max+1]; Picture p[p_max+1]; Video v[v_max+1];
void show() {
cout << "**************************************" << endl;
cout << "******* 欢迎使用媒体库管理系统 *******" << endl;
cout << "************ 1、添加物品 ***********" << endl;
cout << "************ 2、查询物品 ***********" << endl;
cout << "************ 3、显示物品库 ***********" << endl;
cout << "************ 4、编辑物品 ***********" << endl;
cout << "************ 5、删除物品 ***********" << endl;
cout << "************ 6、统计信息 ***********" << endl;
cout << "************ 7、物品存盘 ***********" << endl;
cout << "************ 8、读出物品 ***********" << endl;
cout << "************ 0、退出系统 ***********" << endl;
cout << "**************************************" << endl;
}
int main() {
manage *a = NULL;
int select;
while (true)
{
show(); cin >> select;
switch (select)
{
case 1:
{
a->add(b, &b_count, p, &p_count, v, &v_count);//将b_count的地址传递给指针, 通过对相应地址值得改变,改变b_count的值
system("pause");
system("cls");
break;
}
case 2:
{
string title; string number; string type;
a->showsubmenu();
int choice;
while (true)
{
cin >> choice;
if (choice == 1)
{
cout << "请输入该物品的标题:" << endl;
cin >> title;
a->findtitle(b, &b_count, p, &p_count, v, &v_count, title);
break;
}
else if (choice == 2)
{
cout << "请输入该物品的编号:" << endl;
cin >> number;
a->finnum(b, &b_count, p, &p_count, v, &v_count, number);
break;
}
else if (choice == 3)
{
cout << "请输入查询类别:" << endl;
cin >> type;
a->findproject(b, &b_count, p, &p_count, v, &v_count, type);
break;
}
else
{
cout << "请输入1-3内的数字进行选择!" << endl;
}
}
system("pause");
system("cls");
break;
}
case 3:
{
a->showobject(b, &b_count, p, &p_count, v, &v_count);
system("pause");
system("cls");//已完成
break;
}
case 4:
{
cout << "请输入所需要编辑物品的编号:" << endl;
string number; cin >> number;
a->xiugai(b, &b_count, p, &p_count, v, &v_count, number);
system("pause");
system("cls");
break;
}
case 5:
{
if (b_count == 1 && p_count == 1 && v_count == 1)
{
cout << "物品库为空!" << endl;
system("pause");
system("cls");
break;
}
else {
cout << "请输入需要删除物品的编号" << endl;
string num; cin >> num;
a->remove(b, &b_count, p, &p_count, v, &v_count,num);
cout << "删除成功!" << endl;
}
system("pause");
system("cls");
break;
}
case 6:
{
cout << "物品库总数量为:" << b_count - 1 + v_count - 1 + p_count - 1 << endl;
cout << "图书类物品的数量为:" << b_count - 1 << endl;
cout << "视频光盘类物品的数量为:" << v_count - 1 << endl;
cout << "图画类物品的数量为:" << p_count - 1 << endl;
system("pause");
system("cls");
break;//已完成
}
case 7:
a->baocun(b, &b_count, p, &p_count, v, &v_count);
system("pause");
system("cls");
break;
case 8:
a->duqu(b, &b_count, p, &p_count, v, &v_count);
system("pause");
system("cls");
break;
case 0:
a->esc();
break;//已完成
default:
cout << "请输入0-8以内的数字!" << endl;
system("pause");
system("cls");
break;//已完成
}
}
}
四、心得体会:
1.在c++理论课程中学习的知识,需要通过制作一些简单的小程序来进行巩固,通过大一下这次的三级项目,我对基类和派生类之间的联系、类的友元类以及多态的理解更加深刻。
2.我们通过定义一个指向类的对象的指针来访问类的对象数组,通过传递类的对象数组首地址来对对象数组操作,从而实现物品信息的添加、删除、查找等的操作。由于题目中编号唯一的要求,所以需要定义一个功能类,来将三个类的对象数组进行组合,在同一个添加函数中定义类的对象,调用三个类的添加函数,并进行判重,需要将总功能类定义为其他三个类的友元类,通过一步一步的调试和尝试,最终实现了题目要求的功能。
3.在完成三级项目的过程中,对c++有了更深刻的理解,在一定程度上锻炼了开发程序的能力,在遇到难以解决的问题的时候,和队友进行商讨,查阅csdn的资料和其他的网上资料,锻炼了合作的能力。通过这次的三级项目,我们同样也认识到了自己的不足,在以后的学习中,我们会更加努力将自己所学的知识应用于实践当中,更加牢固的掌握知识。