[c++]代码库
#ifndef VIDEO_H
#define VIDEO_H
#include<iostream>
#include<string>
using namespace std;
//class video
string vidname;
class video{ //影碟类
friend ostream &operator<<(ostream &os,video &vi);
friend class application;
public:
video(string="",string="",int=0,string="",string="",string="",int=0);
video(video &vi);
void input();
//private:
string Vname; //碟片名称
string Country; //国家
int Type; //类型
string Producer; //制片人
string Director; //导演
string Star; //主演明星
int Number; //库存量
video *next;
};
video::video(string vname,string country,int type,string producer,string director,string star,int number){
Vname=vname;
Country=country;
Type=type;
Producer=producer;
Director=director;
Star=star;
Number=number;
}
video::video(video &vi){
Vname=vi.Vname;
Country=vi.Country;
Type=vi.Type;
Producer=vi.Producer;
Director=vi.Director;
Star=vi.Star;
Number=vi.Number;
}
ostream &operator<<(ostream &os,video *vi){
os<<"****************************************************************"<<endl;
os<<"该影碟信息为:"<<endl;
os<<"名称:"<<vi->Vname<<"国家:"<<vi->Country<<"类型:";
if(vi->Type==1)os<<"喜剧";
else{
if(vi->Type==2)os<<"悲剧";
else{
if(vi->Type==3)os<<"科幻片";
else{
if(vi->Type==4)os<<"悬疑剧";
else{
if(vi->Type==5)os<<"惊悚片";
else{
if(vi->Type==6)os<<"其他";
}
}
}
}
}
os<<endl;
os<<"制片人:"<<vi->Producer<<"导演:"<<vi->Director<<"主演:"<<vi->Star<<"库存量:"<<vi->Number<<endl;
return os;
}
#endif
#ifndef CUSTOMER_H
#define CUSTOMER_H
#include<iostream>
#include<string>
#include"video.h"
using namespace std;
//class Customer
struct VideoInfo{ //碟片信息
string VideoName; //影片名称
int BeginTime; //影片借阅日期
int EndTime; //影片归还日期
};
string Cname;
class Customer{ //顾客类
friend ostream & operator<< (ostream &os,const Customer &cu);
friend class application;
friend class video;
public:
Customer(string="",string="",int=0,int=0,string="",int=0,int=0); //构造函数
int DeclineMoney(int ); //扣取消费额
bool RentVideo(string ); //出租影碟
bool ReturnVideo(string ); //归还影片
//private:
string idPerson; //会员账号
string Name; //会员姓名
int LeftMoney; //会员剩余金额
int nCount; //所借碟片数量
VideoInfo Video[10]; //每个会员最多借10张影碟
Customer *next1;
};
Customer::Customer(string idperson,string name,int leftmoney,int ncount,string videoname,int begintime,int endtime){
idPerson=idperson;
Name=name;
LeftMoney=leftmoney;
nCount=ncount;
for(int i=0;i<nCount;i++){
Video[i].VideoName=videoname;
Video[i].BeginTime=begintime;
Video[i].EndTime=endtime;
}
}
ostream & operator<< (ostream &os,const Customer *cu){
os<<"会员编号为:\t"<<cu->idPerson<<"会员姓名为:\t"<<cu->Name<<"会员剩余会费余额为:\t"<<cu->LeftMoney<<endl;
for (int i=0;i<cu->nCount;i++){
os<<"所借影片名为:\t"<<cu->Video[i].VideoName<<endl;
os<<"影片借阅日期为:\t"<<cu->Video[i].BeginTime<<endl;
os<<"影片归还日期为:\t"<<cu->Video[i].EndTime<<endl;
os<<"--------------------------------------------\n";
}
return os;
}
#endif
#ifndef MENU_H
#define MENU_H
#include<iostream>
using namespace std;
//class menu
class menu{ //菜单类
public:
int showMainMenu();
private:
};
int menu::showMainMenu(){
system("cls");
cout<<" ————————————————————————"<<endl;
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<<"| 9.注册会员 |"<<endl;
cout<<"| 10.注销会员 |"<<endl;
cout<<"| 11.更新会员信息与充值 |"<<endl;
cout<<"| 12.退出系统 |"<<endl;
cout<<" ————————————————————————"<<endl;
cout<<"请选择1-12:"<<endl;
int select;
cin>>select;
while(select<1||select>12){
cout<<"选择错误,请重新选择1-12"<<endl;
cin.clear(); //当输入字符,清空流错误状态
cin.sync(); //清空数据流
cin>>select;
}
return select;
}
#endif
#ifndef APPLICATION_H
#define APPLICATION_H
#include<iostream>
#include<string>
#include<fstream>
#include"video.h"
#include"Customer.h"
#include"menu.h"
using namespace std;
class application{ //数据应用类
public:
application();
~application();
void load(); //数据输出
void save(); //数据保存到文件
void Rent(); //1.影碟租赁
void Return(); //2.影碟归还
void Checkall(); //3.查看各类型影碟信息
void search(int Type); //根据类型查找
void Check(string vidname); //4.查看影碟信息:根据碟片名称查找
void Addvideo(); //5.添加影碟
void Deletevideo(); //6.删除影碟
void Modifyvideo(string Vname); //7.修改影碟信息
void Customerinfo(string Cname);//8.会员信息:根据姓名查找
void Add(); //9.注册会员
void Delete(); //10.注销会员
void Modify(string Name); //11.更新会员信息与充值
void run();
private:
string title;
menu myMenu;
video myvideo;
Customer mycustomer;
video *vid;
Customer *cus;
};
application::application(){
vid=0;
cus=0;
load();
}
application::~application(){
video *v;
v=vid;
Customer *c;
c=cus;
while(v){
v=v->next;
delete vid;
vid=v;
}
vid=0;
while(c){
c=c->next1;
delete cus;
cus=c;
}
vid=0;
}
void application::load(){
ifstream file; //碟片信息
video *v=vid;
string Vname; //碟片名称
string Country; //国家
int Type; //类型
string Producer; //制片人
string Director; //导演
string Star; //主演明星
int Number=0; //库存量
file.open("video.txt",ios::in);
file>>Vname>>Country>>Type>>Producer>>Director>>Star>>Number;
while(file.good()){
v=new video(Vname,Country,Type,Producer,Director,Star,Number);
v->next=0;
//碟片结点加入链表
if(vid) //若已经存在结点
{
video *v2;
v2=vid;
while(v2->next) //查找尾结点
{
v2=v2->next;
}
v2->next=v; //连接
}
else //若不存在结点(表空)
vid=v; //连接
file>>Vname>>Country>>Type>>Producer>>Director>>Star>>Number;
}
file.close();
ifstream file1; //会员信息
Customer *c=cus;
string idPerson; //会员账号
string Name; //会员姓名
int LeftMoney; //会员剩余金额
int nCount; //所借碟片数量
VideoInfo Video[10]; //每个会员最多借10张影碟
file1.open("Customer.txt",ios::in);
file1>>idPerson>>Name>>LeftMoney>>nCount;
for(int i=0;i<nCount;i++){
file1>>Video[i].BeginTime>>Video[i].EndTime>>Video[i].VideoName;
}
while(file1.good()) {
for(int i=0;i<nCount;i++){
c=new Customer(idPerson,Name,LeftMoney,nCount,Video[i].VideoName ,Video[i].BeginTime,Video[i].EndTime);
c->next1=0;
//顾客结点加入链表
if(cus) //若已经存在结点
{
Customer *c2;
c2=cus;
while(c2->next1) //查找尾结点
{
c2=c2->next1;
}
c2->next1=c; //连接
}
else //若不存在结点(表空)
cus=c; //连接
file1>>Video[i].BeginTime>>Video[i].EndTime>>Video[i].VideoName;
}
}
file1.close();
}
void application::save(){
ofstream file("video.txt",ios::out);
ofstream file1("Customer.txt",ios::out);
char c;
cout<<"\n是否保存数据?[Y/N]:";
cin>>c;
if(toupper(c)!='Y')
return;
video *v=vid;
while(v){
file<<v->Vname<<v->Country<<v->Type<<v->Producer<<v->Director<<v->Star<<v->Number<<endl;
v=v->next;
}
file.close();
Customer *cu=cus;
while(cu){
file1<<cu->idPerson<<cu->Name<<cu->LeftMoney<<cu->nCount<<cu->Video<<endl;
cu=cu->next1;
}
file1.close();
cout<<"\n保存成功...\n";
}
void application::Rent(){
int number;
Customer *c1;
c1=cus;
cout<<"请输入会员姓名:"<<endl;
cin>>Cname;
while(c1){
if(c1->Name==Cname)
break;
else{
c1=c1->next1;
}
}
if(c1!=NULL){
cout<<c1;
cout<<"请输入租赁碟片数目:";
cin>>number;
if(number>10-c1->nCount)
cout<<"租赁数目超过限定值!"<<endl;
else
if(number*20>c1->LeftMoney)
cout<<"余额不足,请充值或减少影碟数!"<<endl;
if(number<=10-c1->nCount&&number*20<=c1->LeftMoney){
c1->nCount=number;
for(int i=0;i<number;i++){
cout<<"请输入租赁日期(例:2013年08月08日写为20130808):";
cin>>c1->Video[i].BeginTime;
cout<<"请输入碟片名:";
cin>>c1->Video[i].VideoName;
}
c1->LeftMoney-=number*20;
cout<<"\t\t租赁成功!"<<endl;
}
}
else
cout<<"未找到该会员,请先注册!"<<endl;
}
void application::Return(){
int number,time;
Customer *c1;
c1=cus;
cout<<"请输入会员姓名:"<<endl;
cin>>Cname;
while(c1){
if(c1->Name==Cname)
break;
else{
c1=c1->next1;
}
}
if(c1!=NULL){
cout<<c1;
Customerinfo(Cname);
cout<<"请输入归还碟片数目:";
cin>>number;
c1->nCount=c1->nCount-number;
for(int i=0;i<number;i++){
cout<<"请输入碟片名称:";
cin>>c1->Video[i].VideoName;
cout<<"请输入归还日期:";
cin>>c1->Video[i].EndTime;
cout<<"请输入该碟片租赁天数:";
cin>>time;
if(time%3==0)
c1->LeftMoney=c1->LeftMoney+20-time/3;
else
c1->LeftMoney=c1->LeftMoney+20-time/3-1;
cout<<"\t\t扣费成功!"<<endl;
}
cout<<"\t\t***归还成功***\n";
}
else
cout<<"未找到该会员,请确认!"<<endl;
}
void application::search(int Type){ //根据类型查找
video *v1;
v1=vid;
while(v1){
if(v1->Type==Type)
break;
else{
v1=v1->next;
}
}
if(v1!=NULL){
cout<<v1;
}
else
cout<<"未找到该类型的碟片!"<<endl;
}
void application::Checkall(){
int Type;char c;
cout<<"选择要查找的碟片类型(1.喜剧2.悲剧3.科幻片4.悬疑剧5.历史剧6.惊悚片7.其他):"<<endl;
cin>>Type;
search(Type);
cout<<"********以上为类型所有碟片*********"<<endl;
cout<<"是否继续查找【Y/N】?"<<endl;
cin>>c;
if(toupper(c)=='Y'){
Checkall();
return ;
}
else
return ;
system("pause");
}
void application::Check(string vidname){
video *v1;
v1=vid;
while(v1){
if(v1->Vname==vidname)
break;
else{
v1=v1->next;
}
}
if(v1!=NULL){
cout<<v1;
}
else
cout<<"未找到该碟片!"<<endl;
}
void application::Addvideo(){
system("cls");
video *v,*v2; //新结点指针
string Vname; //碟片名称
string Country; //国家
int Type=7; //类型
string Producer; //制片人
string Director; //导演
string Star; //主演明星
int Number=0; //库存量
char c;
cout<<"\n** 新增碟片信息 **\n";
//输入碟片信息
cout<<"输入片名:\t";
cin>>Vname;
cout<<endl;
video *v1;
v1=vid;
while(v1){
if(v1->Vname==Vname){
break;
}
else{
v1=v1->next;
}
}
if(v1!=NULL){
cout<<"该碟片已存在,是否修改该碟片信息(Y/N) "<<endl;
cin>>c;
if(toupper(c)=='Y'){
cout<<"该碟片信息为:"<<endl;
Check(Vname);
cout<<endl;
Modifyvideo(Vname);
return ;
}
else
return ;
}
cout<<"国家:";cin>>Country;
cout<<"类型(1.喜剧2.悲剧3.科幻片4.悬疑剧5.惊悚片6.其他):";cin>>Type;
cout<<"制片人:";cin>>Producer;
cout<<"导演:";cin>>Director;
cout<<"主演:";cin>>Star;
cout<<"库存量:";cin>>Number;
v=new video(Vname,Country,Type,Producer,Director,Star,Number);
v->next=0;
//碟片结点加入链表
if(vid) //若已经存在结点
{
v2=vid;
while(v2->next) //查找尾结点
{
v2=v2->next;
}
v2->next=v; //连接
}
else //若不存在结点(表空)
{
vid=v; //连接
}
save();
cout<<"\t\t\t ***添加成功***\n"<<endl;
cout<<"是否继续添加(Y/N) "<<endl;
cin>>c;
if(toupper(c)=='Y')
{
Addvideo();
return ;
}
else
return ;
}
void application::Deletevideo(){
system("cls");
char c;
string Vname;
cout<<"\n** 删除信息 **\n";
cout<<"输入要删除的碟片名:\t";
cin>>Vname;
cout<<endl;
//查找要删除的结点
video *v1,*v2;
v1=vid;
while(v1){
if(v1->Vname==Vname)
break;
else{
v2=v1;
v1=v1->next;
}
}
//删除结点
if(v1!=NULL)//若找到结点,则删除
{
cout<<"所要删除的碟片信息如下:\n"<<endl;
cout<<v1;
cout<<"确定是否删除(Y/N): ";
cin>>c;
if(toupper(c)!='Y')
return;
if(v1==vid) //若要删除的结点是第一个结点
{
vid=v1->next;
delete v1;
}
else //若要删除的结点是后续结点
{
v2->next=v1->next;
delete v1;
}
save();
cout<<"\t\t***删除成功***\n";
cout<<"是否继续删除(Y/N) "<<endl;
cin>>c;
if(toupper(c)=='Y')
{
Deletevideo();
return ;
}
else
return ;
}
else //未找到结点
cout<<"未找到该碟片!\n";
}
void application::Modifyvideo(string vidname){
video *v1;
char c;
v1=vid;
while(v1){
if(v1->Vname==vidname)
break;
else{
v1=v1->next;
}
}
if(v1!=NULL)//若找到结点
{
system("cls");
cout<<"所要修改的碟片信息如下:\n"<<endl;
cout<<v1;
do{
cout<<"1.修改碟片名称 2.修改国家 3. 修改类型 4.修改制片人" ;
cout<<" 5. 修改导演 6.修改主演 7.修改库存量 8. 退出修改 \n"<<endl;
cout<<"请选择(1-8)要修改的信息\n"<<endl;
cin>>c;
if(c!='8')
cout<<"请输入新的信息: ";
switch(c){
case '1': cin>>v1->Vname; break;
case '2': cin>>v1->Country; break;
case '3': cin>>v1->Type; break;
case '4': cin>>v1->Producer; break;
case '5': cin>>v1->Director; break;
case '6': cin>>v1->Star; break;
case '7': cin>>v1->Number;break;
default: break;
}
}while(c!='8');
cout<<"\t ***修改成功***\n"<<endl;
cout<<"是否继续修改(Y/N): "<<endl;
cin>>c;
if(toupper(c)=='Y'){
cout<<"请输入要修改碟片的名称: ";
cin>>vidname;
cout<<endl;
Modifyvideo(vidname);
return ;
}
else
return ;
}
else //未找到结点
cout<<"未找到!\n";
}
void application::Customerinfo(string Cname){
Customer *c1;
c1=cus;
while(c1){
if(c1->Name==Cname)
break;
else{
c1=c1->next1;
}
}
if(c1!=NULL){
cout<<c1;
}
else
cout<<"未找到该会员!"<<endl;
}
void application::Add(){
system("cls");
Customer *c,*c2; //新结点指针
string idPerson; //会员账号
string Name; //会员姓名
int LeftMoney=0; //会员剩余金额
int nCount=0; //所借碟片数量
char a;
cout<<"\n** 新增会员信息 **\n";
cout<<"输入会员名:\t";
cin>>Name;
cout<<endl;
Customer *c1;
c1=cus;
while(c1){
if(c1->Name==Name){
break;
}
else{
c1=c1->next1;
}
}
cout<<"请输入会员编号:\n";
cin>>idPerson;
cout<<"初始金额:";
cin>>LeftMoney;
nCount=0;
c=new Customer(idPerson,Name,LeftMoney,nCount);
c->next1=0;
if(cus){
c2=cus;
while(c2->next1){
c2=c2->next1;
}
c2->next1=c;
}
else{
cus=c;
}
save();
cout<<"\t\t\t ***注册成功***\n"<<endl;
cout<<"是否继续(Y/N) "<<endl;
cin>>a;
if(toupper(a)=='Y'){
Add();
return ;
}
else
return ;
}
void application::Delete(){
system("cls");
char a;
string Name;
cout<<"\n** 删除信息 **\n";
cout<<"输入要删除的会员名:\t";
cin>>Name;
cout<<endl;
Customer *c1,*c2;
c1=cus;
while(c1){
if(c1->Name==Name)
break;
else{
c2=c1;
c1=c1->next1;
}
}
if(c1!=NULL){
cout<<"所要删除的会员信息如下:\n"<<endl;
cout<<c1;
cout<<"确定是否删除(Y/N): ";
cin>>a;
if(toupper(a)!='Y')
return;
if(c1==cus){
cus=c1->next1;
delete c1;
}
else{
c2->next1=c1->next1;
delete c1;
}
save();
cout<<"\t\t***删除成功***\n";
cout<<"是否继续删除(Y/N) "<<endl;
cin>>a;
if(toupper(a)=='Y'){
Delete();
return ;
}
else
return ;
}
else //未找到结点
cout<<"未找到该会员!\n";
}
void application::Modify(string Cname){
Customer *c1;
char a;
c1=cus;
while(c1){
if(c1->Name==Cname)
break;
else{
c1=c1->next1;
}
}
if(c1!=NULL)//若找到结点
{
system("cls");
cout<<"所要修改的会员信息如下:\n"<<endl;
cout<<c1;
do{
cout<<"1.修改姓名 2.修改编号 3.充值 4.退出 \n"<<endl;
cout<<"请选择(1-4)要修改的信息\n"<<endl;
cin>>a;
if(a!='4')
cout<<"请输入新的信息: ";
switch(a){
case '1': cin>>c1->Name; break;
case '2': cin>>c1->idPerson; break;
case '3': cin>>c1->LeftMoney;break;
default: break;
}
}while(a!='4');
cout<<"\t ***修改成功***\n"<<endl;
cout<<"是否继续修改(Y/N): "<<endl;
cin>>a;
if(toupper(a)=='Y'){
cout<<"请输入要修改会员的姓名: ";
cin>>Cname;
cout<<endl;
Modify(Cname);
return ;
}
else
return ;
}
else //未找到结点
cout<<"未找到!\n";
}
void application::run(){
bool userExcited=false;
while(!userExcited){
int userSelection=myMenu.showMainMenu();
switch(userSelection){
case 1:
Rent(); //1.影碟出租
break;
case 2:
Return(); //2.影碟归还
break;
case 3:
Checkall(); //3.查看各类型影碟信息
break;
case 4:{
system("cls");
cout<<"请输入碟片名称: ";
cin>>vidname;
cout<<endl;
Check(vidname); //4.查看影碟信息
}
break;
case 5:
Addvideo(); //5.添加影碟
break;
case 6:
Deletevideo(); //6.删除影碟
break;
case 7:{
cout<<"请输入要修改碟片的名称: ";
cin>>vidname;
cout<<endl;
Modifyvideo(vidname);
} //7.修改影碟
break;
case 8:{
system("cls");
cout<<"请输入会员姓名: ";
cin>>Cname;
cout<<endl;
Customerinfo(Cname);
}
break; //8.查看会员信息
case 9:
Add();
break; //9.注册会员
case 10:
Delete();
break; //10.注销会员
case 11:{
cout<<"请输入要修改会员的姓名: ";
cin>>Cname;
cout<<endl;
Modify(Cname);
}
break; //11.更新会员信息与充值
case 12:
userExcited=true; //12.退出
}
if(userSelection!=12){
cout<<"流程将返回主界面,";
system("pause");
}
else
cout<<"你选择了退出功能,程序将结束运行!";
}
}
#endif
#include<iostream>
#include"application.h"
using namespace std;
int main(){
application myApp;
myApp.run();
char s;
cout<<"是否要保存您的所有操作(Y/N): "<<endl;
cin>>s;
if(toupper(s)=='Y')
myApp.save();
return 0;
}