中南学
面象编程(C++)
实验报告
学生姓名
学 院 信息科学工程学院
专业班级 信息安全班
完成时间 2014年X月X日
目 录
1实验容 2
2实验1 3
21 设计思路 3
22 实验程 3
3实验2 3
31 设计思路 3
33 实验程 3
4实验3 3
41 设计思路 3
42 实验程 3
5总结 3
面象编程(C++)
1实验容
实验1:构建类
实验2:编写糖果机系统够实现购买糖果付款找零等功
实验3:编写程序够根输入学生信息动生成成绩报告
2实验1:类象
21 设计思路
构建学生类进行初始化输入学生学号姓名信息输出信息
22 实验程
3实验2 :糖果机
31 设计思路
构建三类初始化三类构造函数实现投币找零显示商品等功然设计欢迎界面欢迎界面会显示商品名称价格 户投币购买商品(重复投币)选择需商品动找零退出
33 实验程
4实验3 :成绩报告
41 设计思路
构建学生类课程类通函数输入学生姓名学号课程数目等信息输出成绩报告果未缴纳学费显示成绩报告
42 实验程
5总结
实验刚刚接触类象时编写非常简单程序正通程序开始接触面象编程实验二编写糖果机应该实现连续购买功次调试然直接跳段else语句没找错误实验三编写艰难前没接触文件方面容没做直接读取txt文档输出结果
通次实验发现身足没书写惯考虑问题周面相象理解够深入等编程程中体验分耕耘分收获喜悦次调试程序成功运行时候欢乐前法想象果然学门课程学心中体会学快乐进步想必点点敲入编译器代码中获
6代码附录
实验1:类象
头文件:
#include
using namespace std
class student
{
private
int num
char name[10]
public
void setdata()
{
cin >> num
cin >> name
}
void display()
{
cout << num << endl
cout << name << endl
}
}
student student1 student2
源文件:
#include001h
int main()
{
cout << 请输入两位学生学号姓名 << endl
student1setdata()
student2setdata()
cout << 学生信息: << endl
student1display()
student2display()
return 0
}
实验2:糖果机
头文件:
#include
#include
using namespace std
class moneycounter
{
public
moneycounter() input_money(00f){}
~moneycounter(){}
void getgukemoney()
float money_from_buyer()
void clear()
void return_money(float)
private
float input_money
}
class goodsinfo
{
public
goodsinfo() name() price(00f) total(0){}
~goodsinfo(){}
void set_goods(string float int)
string goods_name()
float goods_price()
int goods_number()
private
string name
float price
int total
}
class candymachine
{
public
candymachine()
~candymachine(){}
void showchoices()
void inputmoney()
bool goodsitem(int)
void return_allmoney()
private
moneycounter moneyctr
goodsinfo v_goods[5]
}
void moneycountergetgukemoney()
{
float money
cout << endl << 请投入钱币 << endl
cin >> money
input_money + money
cout << endl << 您投入金额 << input_money << 元 << endl
return
}
float moneycountermoney_from_buyer()
{
return input_money
}
void moneycounterclear()
{
input_money 00f
return
}
void moneycounterreturn_money(float change)
{
cout << endl << 找零 << change << 元 << endl
}
void goodsinfoset_goods(string n float p int num)
{
name n
price p
total num
}
string goodsinfogoods_name()
{
return name
}
float goodsinfogoods_price()
{
return price
}
int goodsinfogoods_number()
{
return total
}
candymachinecandymachine()
{
v_goods[0]set_goods(薯片 3 20)
v_goods[1]set_goods(巧克力 5 0)
v_goods[2]set_goods(口香糖 15 20)
v_goods[3]set_goods(牛轧糖 2 30)
v_goods[4]set_goods(水果糖 2 28)
return
}
void candymachineshowchoices()
{
coutprecision(2)
coutsetf(iosfixed)
cout << endl << 您投入金额 << moneyctrmoney_from_buyer() << 元 << endl
cout << endl << 请选择商品代码 << endl
for (int i 0 i<5 i++)
{
cout << i << << v_goods[i]goods_name()
<< << v_goods[i]goods_price() << 元 << endl
}
cout << 5 退款退出 << endl
return
}
void candymachineinputmoney()
{
cout << endl << 机接受10元5元2元1元05元纸币硬币 << endl
moneyctrgetgukemoney()
return
}
bool candymachinegoodsitem(int select)
{
int number v_goods[select]goods_number()
if (number>0)
{
if (moneyctrmoney_from_buyer() > v_goods[select]goods_price())
{
float change moneyctrmoney_from_buyer() v_goods[select]goods_price()
cout << endl << 您选择 << v_goods[select]goods_name() << 请出口处取 << endl
if (change>0)
{
moneyctrreturn_money(change)
}
return true
}
else
{
cout << endl << 您投入金额足 << endl
}
}
else
{
cout << endl << 您选择饮料已售完 << endl
}
return false
}
void candymachinereturn_allmoney()
{
cout << endl << 退款 << moneyctrmoney_from_buyer() << 元 << endl
return
}
源文件:
#include 标头h
#include
void main()
{
system(COLOR b0)
candymachine candy
string buf
bool go_on(true) cash_on(true) got_it(true)
cout << endl << 欢迎光糖果甜心售货机 << endl << endl
cout << 价目表 << endl
cout << 薯片 300元 << endl << 巧克力 500元 << endl << 口香糖 150元 << endl << 牛轧糖 200元 << endl << 水果糖 200元 << endl
while (go_on)
{
while (cash_on)
{
candyinputmoney()
cout << endl << 继续投币?(yn)
cin >> buf
if (buf n || buf no)
{
cash_on false
}
}
candyshowchoices()
cin >> buf
int select atoi(bufc_str())
if (select 5)
{
candyreturn_allmoney()
go_on false
}
else
{
got_it candygoodsitem(select)
if (got_it)
{
go_on false
}
}
}
cout << endl << 谢谢光欢迎次哦 << endl << endl
}
实验3:学生成绩报告
头文件:
#include
#includecourseh
#include
using namespace std
class Student
{
public
string name
string xuehao
float average
float sum
float zongxuefei
float zong
int choose
Course a[5]
float xuefei[5]
Student()
void setdata()
void sort()
void show()
}
class Course
{
private
string name
int id
int p
public
float xuefen
float money
float mark
void set_data(string int float float)
void display()
}
源文件:
#include
#include
#includestudenth
#include
#include
using namespace std
StudentStudent()
{
for (int i 0 i<5 i++)
{
a[i]mark 0
xuefei[i] 800
}
zong 0
zongxuefei 0
choose 0
sum 0
}
void Studentsetdata()
{
cout << 请输入学生姓名: cin >> name
cout << 请输入学生学号: cin >> xuehao
}
void Courseset_data(string n int i float x float m)
{
name n
id i
xuefen x
money m
}
void Coursedisplay()
{
cout << 课程名称 << '\t' << 编号 << '\t' << 学分 << '\t' << 学费 << endl
cout << name << '\t' << '\t' << id << '\t' << xuefen << '\t' << money << endl << endl
}
int main()
{
system(color 0a)
int s
int count0
Course b[5]
b[0]set_data(高数15400)
b[1]set_data(英语22200)
b[2]set_data(语文32300)
b[3]set_data(物理43400)
b[4]set_data(电路52100)
star cout<<***********************************************************************<
cout<<请选择操作12:
cin>>m
system(cls)
switch(m)
{
case 1for(int i0i<5i++)
{
b[i]display()
}
getch()
system(cls)
goto star
case 2
for(int i0i<5i++)
{
b[i]display()
}
cout<<请输入录入学生数:
cin>>s
Student *ptnew Student[s]
for(int i0i
pt[i]setdata()
cout<
int c
do{
int n
int k
cout<<请输入课程编号:
cin>>k
nk1
pt[i]a[n]b[k1]
cout<<请输入学生该课成绩:
cin>>pt[i]a[n]mark
cout<
cout<<请缴纳< cin>>pt[i]xuefei[n]
if(pt[i]xuefei[n] pt[i]zong+pt[i]a[n]xuefen
pt[i]zongxuefei+pt[i]a[n]money
pt[i]choose++
cout<<继续选课请1退出请0:
cin>>c
}while(c1)
pt[i]averagept[i]sumpt[i]choose
}
system(cls)
cout<<学生分数报告:<
cout<
{
if(pt[i]xuefei[j]b[j]money){cout<
}
cout<
getch()
system(cls)
goto star
}
return 0
}
文档香网(httpswwwxiangdangnet)户传
《香当网》用户分享的内容,不代表《香当网》观点或立场,请自行判断内容的真实性和可靠性!
该内容是文档的文本内容,更好的格式请下载文档