西电软件学院c++上机实验报告.docx
《西电软件学院c++上机实验报告.docx》由会员分享,可在线阅读,更多相关《西电软件学院c++上机实验报告.docx(33页珍藏版)》请在冰豆网上搜索。
![西电软件学院c++上机实验报告.docx](https://file1.bdocx.com/fileroot1/2023-2/22/4039fe0b-1b6a-4831-9631-3aa97245125b/4039fe0b-1b6a-4831-9631-3aa97245125b1.gif)
西电软件学院c++上机实验报告
C++实验报告
XX:
妍
学院:
软件学院
学号:
一、实验要求
•完成一个完整的简化系统:
一卡通乘车系统。
•一卡通乘车系统主要完成的功能是,支持一卡通刷卡乘车;对每一卡通的使用情况及费用进行统计;对每辆班车的运行情况进行统计。
•在实现该系统的功能时,不要现图形界面,控制台输出即可。
•必须采用面向对象程序设计的方式实现。
二、程序中所设计的类
classStuCard;
ClassPeople;
classbus;
三、类中的关键数据成员和成员函数
1、StuCard类
classStuCard
{
public:
voidpay();//上车刷卡时调用此函数,根据卡的类型扣除上车费用并给出相应提示
intGetRest();//获取卡余额
StuCard(stringname,stringid,intmoney,inttype,intcount);
stringget_name(){returnname;}
stringget_id(){returnid;}
intget_type(){returntype;}
intget_money(){returnmoney;}
intget_count(){returncount;}//乘车次数
private:
stringname;//持卡人
stringid;//卡号
intmoney;//余额
inttype;//卡的类型(type=1,学生卡;type=2,教师卡;type=3,限制卡)
intcount;//乘车次数
};//定义了一个StuCard的类,用type来区分卡的类型,用get_X函数在主函数中来获得数据成员的值
2、bus类
structtime{
inthour;
intminute;
};
classbus{
stringbnumber;//车牌号
stringxnumber;//型号
intpeople;//载乘人数
stringname;//驾驶员
structtimestime;//发车时间
structtimeetime;//到站时间
structtimet_stime;//实际发车时间
structtimet_etime;//实际到站时间
public:
bus(stringbn,stringxn,intp,stringn,intsh,intsm,inteh,intem){
bnumber=bn;
xnumber=xn;
people=p;
name=n;
stime.hour=sh;
stime.minute=sm;
etime.hour=eh;
etime.minute=em;
}//构造函数
stringget_b(){returnbnumber;}//获得车牌号
stringget_x(){returnxnumber;}//获得车的型号
intget_p(){returnpeople;}//获取上车人数
stringget_n(){returnname;}//获取驾驶员
voidStime(inta,intb);//获取预计发车时间
voidEtime(intc,intd);//获取预计到站时间
voidshow(stringbnumber,stringxnumber,intpeople,stringname);
voidT_stime(intta,inttb);//获取实际发车时间
voidT_etime(inttc,inttd);//获取实际到站时间
floatpeoplebi(floate,floatf);//载乘人数比
intget_sthour();//获得发车时间的小时
intget_stminute();//获得发车时间的分钟
intget_ethour();//获得到站时间的小时
intget_etminute();//获得到站时间的分钟
voidstshow(intp,intq);
voidetshow(intx,inty);
};
3、people类
classPeople{
stringname;//
stringsex;//性别
stringwork;//职务
stringoffice;//所属单位
stringnumber;//学号/工号
public:
People(stringn,strings,stringw,stringo,stringb);//构造函数
voidreadPeople();//用于在主函数中读入人的信息
voidshow(stringname,stringsex,stringwork,stringoffice,stringnumber);
voidaddPeople();//申请一新卡
voiddeletePeople();//注销一卡通
stringget_name(){returnname;}
stringget_sex(){returnsex;}
stringget_work(){returnwork;}
stringget_office(){returnoffice;}
stringget_number(){returnnumber;}
};
四、运行流程的说明
主程序分为两个系统:
乘车系统和一卡通管理系统。
1、乘车系统主要实现的功能如下:
首先,采用随机函数,生成本站会上车的人数,这个人数不超过车的最大载乘人数。
其次,生成几个人上车,就依次输入几个人的卡号,持卡人在持卡上车时一卡通余额、乘车次数会发生变更,并且根据余额的多少会有相应提示信息,再将变更后的信息写入文件
公交车在每上一个人的时候乘车人数加一,到达最大载乘人数时提示不能再刷卡上车,并且采用随机函数,随机生成预计发车时间、预计到站时间,实际发车时间和实际到站时间,计算发车准时率和到站准时率,再将变更后的的信息写入文件
2、一卡通管理系统主要实现的功能如下:
在文件中保存人的基本信息,记录每个人的乘车次数,增加申请新卡,注销一卡通的功能。
在程序中有一个people的txt文件,里面储存了人的信息,卡的主体是人,办卡的前提是人在people文件中存在。
当选择办卡时,输入people中存在的人名,从文件头开始查找,到文件尾结束,当输入的人名与people中已存的信息相匹配的时候,就将这组信息(包括,学号,卡的类型,余额(新办的卡余额初始化为50),乘车次数(新卡为0))写入mycard.txt文件中去,办卡成功。
当需要删除一个卡的时候,人的信息没有改动,只是将mycard.txt中办卡的信息删去了。
以vectorcards(200)的方式,定义了一个类的数组,当输入需要删除的人的之后,从文件头开始查找,当输入的人名与mycard中已存的信息不匹配的时候,就将这一组信息写入数组,到文件尾结束,再将数组里面的写入mycard中,这样子就把想要删掉的容删掉了。
五、运行结果及分析
文档设置:
Mycard容分别对应,学号,余额,乘车次数,卡的类型(1=学生卡;2=教师卡,3=限制卡)
Bus的容分别对应为车牌号,车类型,最大载乘人数,驾驶员,预计出发时间(时、分)和预计到达时间(时、分)
当运行程序时,会看到如下界面
选择1,进入乘车系统
首先输入车牌信息,随机产生等车人数,刷卡上车,即输入卡号
输入1402012,即四上车,四的乘车次数加1,余额减2,写入文件,可以看到
其中,四的乘车信息相比初始时发生了正确的改变。
并且随机产生的30组发车时间和到站时间,发车准时率和到站准时率,载乘人数比也正确的算出。
选择2,进入一卡通管理系统
选择1,办新卡
输入三,为三办一新卡,三的卡号从people文件读取,卡的类型,根据三的职业的定义初始化为1=学生卡;2=教师卡,3=限制卡,新卡余额默认100元,乘车次数为0。
若选择2,删除一卡
如图,将刚刚插入的三的信息删除,并且更新文件信息,三在mycard的记录已经不见了。
删卡成功。
六、源代码
#include
#include
#include
#include
#include
#include
#include
#include
usingnamespacestd;
structtime{
inthour;
intminute;
};
classStuCard
{
public:
voidpay();
intGetRest();
voidshow();
StuCard(stringname,stringid,intmoney,inttype,intcount);
stringget_name(){
returnname;
}
stringget_id(){
returnid;
}
StuCard(){
name="null";
id="null";
money=0;
count=0;
type=0;
}
intget_type(){
returntype;
}
intget_money(){
returnmoney;
}
intget_count(){
returncount;
}
private:
stringname;//持卡人
stringid;//卡号
intmoney;//余额
inttype;//卡的类型(type=1,学生卡;type=2,教师卡;type=3,限制卡)
intcount;//乘车次数
};
voidStuCard:
:
pay(){
//StuCardstucard1(charname[16],charID[17],intmoney,inttype,intcount);
if(type==1)
{
money-=2;
count=count+1;
cout<<"你的卡余额为:
"<if(money<5)
cout<<"你的余额过低,请及时充值!
"<<'\n';
}
if(type==2)
{
count=count+1;
cout<<"你的卡余额为:
"<if(money<5)
cout<<"你的余额过低,请及时充值!
"<<'\n';
}
if(type==3)
{
if(count<20){
count=count+1;
cout<<"你的卡余额为:
"<if(money<5)
cout<<"你的余额过低,请及时充值!
"<<'\n';
}
if(count==20){
count=count+1;
cout<<"这是本学期最后一次免费乘车!
\n";
cout<<"你的卡余额为:
"<if(money<5)
cout<<"你的余额过低,请及时充值!
"<<'\n';
}
if(count>20){
cout<<"本次开始收费!
\n";
money-=2;
count=count+1;
cout<<"你的卡余额为:
"<if(money<5)
cout<<"你的余额过低,请及时充值!
"<<'\n';
}
}
}
intStuCard:
:
GetRest(){
returnmoney;
}
voidStuCard:
:
show()
{
cout<<"名字:
"<cout<<"卡的类型:
"<cout<<"乘车次数:
"<}
StuCard:
:
StuCard(stringname,stringid,intmoney,inttype,intcount){
this->name=name;
this->id=id;
this->money=money;
this->type=type;
this->count=count;
}
//People
classPeople{
stringname;
stringsex;
stringwork;
stringoffice;
stringnumber;
public:
People(stringn,strings,stringw,stringo,stringb);
voidreadPeople();
voidshow(stringname,stringsex,stringwork,stringoffice,stringnumber);
voidaddPeople();
voiddeletePeople();
stringget_name(){returnname;}
stringget_sex(){returnsex;}
stringget_work(){returnwork;}
stringget_office(){returnoffice;}
stringget_number(){returnnumber;}
};
People:
:
People(stringn,strings,stringw,stringo,stringb)
{
name=n;
sex=s;
work=w;
office=o;
number=b;
}
voidPeople:
:
readPeople()
{
cout<<"请输入:
"<cin>>name;
cout<<"请输入性别:
"<cin>>sex;
cout<<"请输入职务:
"<cin>>work;
cout<<"请输入所属单位:
"<cin>>office;
if(work=="学生")
{
cout<<"请输入学号:
"<cin>>number;
}
else
{
cout<<"请输入工号:
"<cin>>number;
}
}
voidPeople:
:
addPeople()//定义添加持卡人函数
{
Peoplepeople1("a1","b2","c3","d4","e5");//实例化对象
ofstreamfileout("people.txt",ios:
:
app);//打开文件
if(!
fileout)
{
cout<<"不能打开文件!
"<<"people.txt"<}
else
{
cout<<"请输入持卡人基本信息:
"<people1.readPeople();//读入信息
cout<fileout<<<""<}
fileout.close();//关闭文件
}
voidPeople:
:
deletePeople()
{
ifstreamfilein("people.txt");//打开文件
if(!
filein)
{
cout<<"打开文件失败!
"<}
else
{
stringt,m;
intn=0;//标签,是否有这个读者
cout<<"请输入删除持卡人的的:
"<cin>>m;
cout<<"请输入删除持卡人的的学号或工号:
"<cin>>t;
do
{
stringx,y,z,u,w;//定义变量
strings;
getline(filein,s);//读入每一行
istringstreamsin(s);
sin>>x>>y>>z>>u>>w;//把s中第一个字符串给a
ofstreamfileout("people1.txt",ios:
:
app);//写到新文件中去
if(m!
=x||t!
=w)//判断是否为删除的地方
fileout<
if(m==x&&t==w)//判断是否为删除的地方
{
n=1;
cout<<"原持卡人信息为:
"<cout<<":
"<cout<<"性别:
"<cout<<"职务:
"<cout<<"所属单位:
"<
if(z=="学生")cout<<"学号:
"<elsecout<<"工号:
"<cout<<"是否删除?
(Y:
是,N:
否)"<chara;//定义变量
cin>>a;
if(a=='Y')
{
cout<<"删除成功!
"<}
if(a=='N')
{
cout<<"未删除!
"<fileout<}
fileout.close();//关闭文件
}
}
while(!
filein.eof());//判断是否到文件尾
if(n==0)//不存在这个学生
cout<<"未找到要删除的持卡人!
"<}
filein.close();//关闭
ifstreamfilei("people1.txt");//打开文档
ofstreamfileout("people.txt",ios:
:
out);//打开文档并清空容
for(strings;getline(filei,s);)//将修改后的容写到文件中去
{
fileout<
}
filei.close();//关闭文件
fileout.close();//关闭文件
system("delpeople1.txt");//删除文件副本
}
classbus{
stringbnumber;//车牌号
stringxnumber;//型号
intpeople;//载乘人数
stringname;//驾驶员
structtimestime;//发车时间
structtimeetime;//到站时间
structtimet_stime;//实际发车时间
structtimet_etime;//实际到站时间
public:
bus(stringbn,stringxn,intp,stringn,intsh,intsm,inteh,intem)
{
bnumber=bn;
xnumber=xn;
people=p;
name=n;
stime.hour=sh;
stime.minute=sm;
etime.hour=eh;
etime.minute=em;
}
stringget_b(){returnbnumber;}
stringget_x(){returnxnumber;}
intget_p(){returnpeople;}
stringget_n(){returnname;}
voidStime(inta,intb);
voidEtime(intc,intd);
voidshow(stringbnumber,stringxnumber,intpeople,stringname);
voidT_stime(intta,inttb);
voidT_etime(inttc,inttd);
floatpeoplebi(floate,floatf);
intget_sthour();
intget_stminute();//获得实际发车时间
intget_ethour();
intget_etminute();//获得实际到站时间
voidstshow(intp,intq);
voidetshow(intx,inty);
};
voidbus:
:
Stime(inta,intb)
{
stime.hour=a;
stime.minute=b;
cout<<"发车时间为:
";
cout<"<}
voidbus:
:
Etime(intc,intd)
{
etime.hour=c;
etime.minute=d;
cout<<"到站时间为:
";
cout<"<}
voidbus:
:
show(stringbnumber,stringxnumber,intpeople,stringname)
{
cout<<"车牌号:
"<cout<<"型号:
"<cout<<"载乘人数:
"<cout<<"驾驶员名字:
"<}
voidbus:
:
T_stime(intta,inttb)
{
t_stime.hour=ta;
t_stime.minute=tb;
cout<<"实际发车时间为:
";
cout<"<}
voidbus:
:
T_etime(inttc,inttd)
{
t_etime.hour=tc;
t_etime.minute=td;
c