c++数据结构上机题及实现代码.docx
- 文档编号:6004432
- 上传时间:2023-01-02
- 格式:DOCX
- 页数:147
- 大小:60.33KB
c++数据结构上机题及实现代码.docx
《c++数据结构上机题及实现代码.docx》由会员分享,可在线阅读,更多相关《c++数据结构上机题及实现代码.docx(147页珍藏版)》请在冰豆网上搜索。
c++数据结构上机题及实现代码
1:
时钟类与对象的定义2:
平均数
3:
定义学生类4:
打卡机
5:
找出每组数据中的最大值(函数重载)
6:
长方形类与对象的定义7:
圆的面积
8:
输出类对象9:
构造函数的重载
10:
构造函数重载11:
实现自定义的析构函数
12:
构造函数--定义矩形13:
对象数组
14:
构造函数与拷贝构造函数——定义圆柱体类
15:
构造函数、拷贝构造和析构函数——定义学生类
16:
统计学生人数和成绩17:
拷贝构造函数
18:
三角形面积
19:
类的静态与友元——职工薪水
20:
统计学生信息21:
学生成绩等级
22:
类的继承——定义学生类及其派生类
23:
类的继承——定义点类及其派生的圆类
24:
长方形的面积和长方体的体积
25:
多继承——Time类和Date类派生出Birthtime类
26:
RoundTable类27:
类的继承
28:
奇葩后代29:
ATM
30:
虚基类-在职读书的教师类
31:
类的虚基类——客货两用汽车
32:
类的虚基类——主任工程师类
33:
NBA2KOL34:
总价多少35:
时钟类
36:
平均数37:
社会保障卡
38:
运算符重载——重载+和-对复数类对象进行加减运算
39:
复数类的运算符重载40:
三角形面积之和
41:
运算符重载——T重载运算符“>”,用于对给定的两个字符串进行比较
42:
复数43:
计数器44:
复整数相加
45:
运算符重载矩阵相加
46:
数列中n个整数排序
47:
运算符重载——重载赋值运算符=用于字符串赋值
48:
字符串类49:
图形的面积50:
求矩阵之和
51:
矩阵取反
1:
时钟类与对象的定义
Time/MemoryLimit:
1000MS/32768K
Submitted:
60Accepted:
45
ProblemDescription
定义一个时钟类,它的数据成员包括:
时、分、秒,它的函数成员有设置时间和显示时间,如果设置时钟的时、分、秒超过有效的范围(24小时制),则输出“error!
”。
编程并测试这个类。
Input
输入数据有多组,每组占一行,每行中有三个数,分别代表时、分、秒,用空格分隔。
Output
对于每组输入数据,输出一行,每个数据间用:
分隔。
SampleInput
1250
SampleOutput
12:
5:
0
#include
usingnamespacestd;
classTIME
{
private:
intH,M,S;
public:
TIME(inth=0,intm=0,ints=0)
{
H=h;M=m;S=s;
}
voidshow()
{
if(H<0||H>=24||M<0||M>60||S<0||S>60)
cout<<"error!
"< else cout< "< "< } }; intmain() { inth,m,s; while(cin>>h>>m>>s) { TIMEone(h,m,s); one.show(); } return0; } 2: 平均数 Time/MemoryLimit: 1000MS/32768K Submitted: 59Accepted: 45 ProblemDescription 定义一个学生类记录学生的学号、姓名、成绩。 要求使用用静态成员变量和静态成员函数求若干个学生的平均成绩。 Input 输入数据有多行。 每行一个浮点数。 Output 输出数据有多行,每行输出当前分数总和除以当前总人数的结果(保留两位小数); SampleInput 1000zhangsan85 1001lisi74 1002wangwu92 1003zhaoliu67 1004zhaoqian56 SampleOutput 85.00 79.50 83.67 79.50 74.80 #include #include #include usingnamespacestd; classstudent { private: stringname; stringnum; doublescore; staticdoubleave; staticdoublesum; staticintcount; public: student(stringnum="",stringname="",doubles=0) { this->num=num;this->name=name;score=s; count++; sum=sum+score; ave=sum/count; } staticvoidshow() { cout< : fixed); cout< (2)< } }; doublestudent: : sum=0.0; doublestudent: : ave=0.0; intstudent: : count=0; intmain() { doubles; stringnum; stringname; while(cin>>num>>name>>s) { studentone(num,name,s); student: : show(); } return0; } 3: 定义学生类 Time/MemoryLimit: 1000MS/32768K Submitted: 67Accepted: 46 ProblemDescription 定义学生类student,类的结构如下 classstudent { public: student(int,char*,int,float);//需提示"Constructing..." ~student();//需提示"Destructing..." voidprintstu(); private: intid; char*name; intage; floatscore; }; 将类定义完整,并在主函数中进行测试。 Input 输入数据有多行。 Output 对于每个测试数据,输出有多行。 SampleInput 1zhangsan1980 2lisi2091 3wangwu1885 SampleOutput Constructing... Num: 1Name: zhangsanAge: 19Score: 80 Destructing... Constructing... Num: 2Name: lisiAge: 20Score: 91 Destructing... Constructing... Num: 3Name: wangwuAge: 18Score: 85 Destructing... #include #include usingnamespacestd; classstudent { private: intid; char*name; intage; floatscore; public: student(inti=0,char*na=NULL,inta=0,floats=0.0) { id=i; if(na! =NULL) { name=newchar[strlen(na)+1]; strcpy(name,na); } else name=NULL; age=a;score=s; cout<<"Constructing..."< } ~student() { if(name! =NULL) delete[]name; cout<<"Destructing..."< } voidprintstu() { if(name! =NULL) cout<<"Num: "< "< "< "< } }; intmain() { inti,a; floats; charn[30]; while(cin>>i>>n>>a>>s) { studentone(i,n,a,s); one.printstu(); } return0; } 4: 打卡机 Time/MemoryLimit: 1000MS/32768K Submitted: 42Accepted: 27 ProblemDescription LPRJ小工厂是刚兴起不久的标准工厂,每天早上八点开始上班,每天工作八小时,但是由于LPRJ小工厂近来员工懈怠于工作,经常迟到,于是经理LP决定用考勤打卡机来记录员工的上班时间,经理为了整顿一下员工的上班态度,决定员工每迟到半小时将扣除10元的工资,若不迟到员工每天的工资有80元,计算每位员工在今天可获得的工资。 (要求用类完成) Input 输入数据有多组,每组第一行输入一个整数T,代表接下去有T行员工测试数据 每位员工的资料包含工号,姓名,打卡时间(hh: mm); Output 输出每位员工的信息以及当天可获得的工资。 SampleInput 3 12078LP07: 45 12080shik08: 00 12012junl08: 31 2 12078LP07: 45 12080shik08: 00 SampleOutput 12078LP07: 4580 12080shik08: 0080 12012junl08: 3160 12078LP07: 4580 12080shik08: 0080 #include #include usingnamespacestd; classstu { private: stringnum; stringname; intH,M; public: stu(stringnum="",stringname="",inthh=0,intmm=0) { this->num=num;this->name=name; H=hh;M=mm; } intSS() { intsalary; if(H<8||(H==8&&M==0)) salary=80; else { if(M%30! =0) salary=80-(H-8)*2*10-(M/30+1)*10; else salary=80-(H-8)*2*10-M/30*10; } if(salary<=0) salary=0; returnsalary; } voidshow() { cout< if(H<10) cout<<"0"; cout< "; if(M<10) cout<<"0"; cout< } }; intmain() { intT,hh,mm; stringnum; stringname; charc1,c2; while(cin>>T) { while(T--) { cin>>num>>name>>hh; cin.get();cin>>mm; stuone(num,name,hh,mm); one.show(); } } return0; } 5: 找出每组数据中的最大值(函数重载) Time/MemoryLimit: 1000MS/32768K Submitted: 68Accepted: 46 ProblemDescription 定义函数Max实现找出每组测试数据中的最大值,在主函数中进行调用,要求Max函数能够实现分别在2个int型数据、2个字符串(不包括空格,长度不超过50)、3个double型数据中找到最大值。 Input 输入数据有三组,每组占一行。 Output 对于每组输入数据,输出一行。 SampleInput 12 55.625.788.8 goodmorning SampleOutput max: 2 max: 88.8 max: morning #include #include usingnamespacestd; voidMax(inta,intb) { intmax; if(a>=b) max=a; else max=b; cout<<"max: "< } voidMax(doublex,doubley,doublez) { doublemax; if(x>=y) max=x; else max=y; if(max max=z; cout<<"max: "< } voidMax(charn1[50],charn2[50]) { charmax3[50]; if(strcmp(n1,n2)>0) strcpy(max3,n1); else strcpy(max3,n2); cout<<"max: "< } intmain() { doublex,y,z; inta,b; charn1[50],n2[50]; cin>>a>>b; cin>>x>>y>>z; cin>>n1>>n2; Max(a,b); Max(x,y,z); Max(n1,n2); return0; } 6: 长方形类与对象的定义 Time/MemoryLimit: 1000MS/32768K Submitted: 57Accepted: 54 ProblemDescription 定义一个类area,它有两个整形的私有数据代表长方形的长和宽,有三个成员函数init、print、areas,init用来给长方形初始化,print用来输出长方形的面积,areas用来计算长方形的面积,要求将类定义完整,并通过main函数定义相应的长方形对象,输出对象的面积。 Input 输入数据有多组,每组占一行,每行中有两个数,用空格分隔。 Output 对于每组输入数据,输出一行 SampleInput 52 64 75 82 SampleOutput 10 24 35 16 #include usingnamespacestd; classArea { private: intl,w; public: voidinit(intl1,intw1) { l=l1; w=w1; } intSS() { ints; s=l*w; returns; } voidprint(); }; voidArea: : print() { cout< } intmain() { Areaa; intl2,w2; while(cin>>l2>>w2) { a.init(l2,w2); a.print(); } return0; } 7: 圆的面积 Time/MemoryLimit: 1000MS/32768K Submitted: 100Accepted: 48 ProblemDescription 设计一个Circle类,可以求圆的面积。 Input 输入数据有多组,每组占一行,每行包括一个实数r,表示圆的半径。 Output 输出圆的面积。 其中PI=3.14。 SampleInput 1 SampleOutput 3.14 #include usingnamespacestd; classCircle { private: doubler; public: voidinit(doubler1) { r=r1; } doubleSS() { doubles; s=r*r*3.14; returns; } voidprint(); }; voidCircle: : print() { cout< } intmain() { Circlex; doubler2; while(cin>>r2) { x.init(r2); x.print(); } return0; } 8: 输出类对象 Time/MemoryLimit: 1000MS/32768K Submitted: 59Accepted: 47 ProblemDescription 定义一个类Student,用成员变量name、no、age描述一个学生的姓名、学号和年龄的信息。 现从键盘获取三个不同学生的信息。 按输入的顺序将它们存入相应的对象中,并显示它们(一行一条记录)。 Input 数据之间用空格分割,每行输入一个记录。 共3条记录。 Output 数据之间用一个空格分割,每行输出一个记录。 共3条记录。 SampleInput zhangsan121 lishi219 wangwu321 SampleOutput zhangsan121 lishi219 wangwu321 #include #include usingnamespacestd; classStudent { private: charname[20]; intno,age; public: voidinit(charn1[20],intno1,intage1) { strcpy(name,n1); no=no1; age=age1; } voidprint() { cout< } }; intmain() { Studentx; charname2[20]; intn,a,j=0; while(cin>>name2>>n>>a) { x.init(name2,n,a); x.print(); } return0; } #include #include usingnamespacestd; classstudent { private: intno,age; char*name; public: student(char*na="",intn=0,inta=0) { intlen=0; len=strlen(na); if(len! =0) { name=newchar[len+1]; strcpy(name,na); } else name=NULL; no=n; age=a; } ~student() { if(strlen(name)! =0) delete[]name; } voidshow() { cout< } }; intmain() { charn[20]=""; intn1,a1; while(cin>>n>>n1>>a1) { students(n,n1,a1); s.show(); } return0; } 9: 构造函数的重载 Time/MemoryLimit: 1000MS/32768K Submitted: 71Accepted: 50 ProblemDescription 为日期类定义4个构造函数,分别是: 不带参数,日期为2000/1/1;带一个参数,仅初始化日数据,年数据为2000,月数据为1;带两个参数,初始化月、日数据,年数据为2000;带三个参数,初始化年、月、日;此外日期类还有一个输出成员函数,用来输出年月日。 在主函数中进行测试。 Input 输入数据有3行,第一行只有一个数据,代表日的数值;第二行有两个数据,代表月与日的数值,第三行有三个数据,代表年、月、日的数值。 Output 输出有4行,每行代表一个日期。 SampleInput 1 101 201211 SampleOutput 2000/1/1 2000/10/1 2012/1/1 2000/1/1 #include usingnamespacestd; classDATE { private: intyear,month,day; public: DATE() { year=2000; month=1; day=1; } DATE(intda) {year=2000; month=1; day=da; } DATE(intm,intd) { year=2000; month=m; day=d; } DATE(inty,intm,intd) { year=y; month=m; day=d; } voidshow() { cout< } }; intmain() { inty,m,d; cin>>d; DATEdate2(d); d
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- c+ 数据结构 上机 实现 代码