C++编程题,急,定义定义并实现一个矩形类类,数据成员有宽度、高度和组成矩形的字符

c++(定义长方形类)_百度文库
您的浏览器Javascript被禁用,需开启后体验完整功能,
赠送免券下载特权
10W篇文档免费专享
部分付费文档8折起
每天抽奖多种福利
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
c++(定义长方形类)
&&设计一个程序,定义一个矩形类,包括数据成员和函数成员。要求有构造函数、析构函数,完成赋值、修改、显示等功能的接口,并编写main 函数测试,要求用一个对象初始化另一对象。提示: 要确定一个矩形(四边都是水平或垂直方向,不能倾斜),只要确定其左上角和右下角的x 和y 坐标即可,因此应包括四个数据成员,left,right,top,bottom,即左右上下四个边界值。
阅读已结束,下载本文需要
想免费下载本文?
定制HR最喜欢的简历
你可能喜欢当前位置: >>
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:NBA2KOL 34:总价多少 35:时钟类 36:平均数 37:社会保障卡 38:运算符重载――重载+和-对复数类对象进 行加减运算 39:复数类的运算符重载 40:三角形面积之和 41:运算符重载――T 重载运算符“&”,用于 对给定的两个字符串进行比较 42:复数 43:计数器 44:复整数相加 45:运算符重载矩阵相加 46:数列中 n 个整数排序 47: 运算符重载――重载赋值运算符 = 用于字 符串赋值 48:字符串类 49:图形的面积 50:求矩阵之和 51:矩阵取反 1:时钟类与对象的定义Time/Memory Limit:1000 MS/32768 K Submitted: 60 Accepted: 45Problem Description 定义一个时钟类,它的数据成员包括:时、分、秒,它的函数成员有设置时间和显示时间, 如果设置时钟的时、分、秒超过有效的范围(24 小时制),则输出“error!”。编程并测试这个 类。Input输入数据有多组,每组占一行,每行中有三个数,分别代表时、分、秒,用空格分隔。Output对于每组输入数据,输出一行,每个数据间用:分隔。Sample Input12 5 0Sample Output12:5:0 #include&iostream& class TIME { private: int H,M,S; public: TIME(int h=0,int m=0,int s=0) { H=h;M=m;S=s; } void show() { if(H&0||H&=24||M&0||M&60||S&0||S&60) cout&&&error!&&& else cout&&H&&&:&&&M&&&:&&&S&& } }; int main() { int h,m,s; while(cin&&h&&m&&s) { TIME one(h,m,s); one.show(); } return 0; }2:平均数Time/Memory Limit:1000 MS/32768 K Submitted: 59 Accepted: 45Problem Description定义一个学生类记录学生的学号、姓名、成绩。要求使用用静态成员变量和静态 成员函数求若干个学生的平均成绩。Input输入数据有多行。每行一个浮点数。Output输出数据有多行,每行输出当前分数总和除以当前总人数的结果(保留两位小 数);Sample Input02
zhangsan 85 lisi 74 wangwu 92 zhaoliu 67 zhaoqian 56Sample Output85.00 79.50 83.67 79.50 74.80 #include&iostream& #include&iomanip& #include&string& class student { private: public: student(string num=&&,string name=&&,double s=0) { this-&num=this-&name=score=s; count++; sum=sum+ ave=sum/ } static void show() { cout&&setiosflags(ios::fixed); cout&&setprecision(2)&&ave&& } }; double student::sum=0.0; double student::ave=0.0; int student::count=0; int main() { while(cin&&num&&name&&s) { student one(num,name,s); student::show(); } return 0; }3:定义学生类Time/Memory Limit:1000 MS/32768 K Submitted: 67 Accepted: 46Problem Description 定义学生类 student,类的结构如下 class student { public: student(int,char *,int,float);//需提示&Constructing...& ~student();//需提示&Destructing...& void printstu(); private: char * }; 将类定义完整,并在主函数中进行测试。Input输入数据有多行。Output对于每个测试数据,输出有多行。Sample Input1 zhangsan 19 80 2 lisi 20 91 3 wangwu 18 85Sample OutputConstructing... Num:1 Name:zhangsan Age:19 Score:80 Destructing... Constructing... Num:2 Name:lisi Age:20 Score:91 Destructing... Constructing... Num:3 Name:wangwu Age:18 Score:85 Destructing... #include&iostream& #include&string.h& class student { private: char * public: student(int i=0,char *na=NULL,int a=0,float s=0.0) { id=i; if(na!=NULL) { name=new char[strlen(na)+1]; strcpy(name,na); } else name=NULL; age=a;score=s; cout&&&Constructing...&&& } ~student() { if(name!=NULL) delete [] cout&&&Destructing...&&& } void printstu() { if(name!=NULL) cout&&&Num:&&&id&&& Name:&&&name&&& Score:&&&score&& } }; int main() { int i,a; char n[30]; while(cin&&i&&n&&a&&s) { student one(i,n,a,s); one.printstu(); } return 0;Age:&&&age&&& }4:打卡机Time/Memory Limit:1000 MS/32768 K Submitted: 42 Accepted: 27Problem DescriptionLPRJ 小工厂是刚兴起不久的标准工厂,每天早上八点开始上班,每天工作八小 时,但是由于 LPRJ 小工厂近来员工懈怠于工作,经常迟到,于是经理 LP 决定用 考勤打卡机来记录员工的上班时间,经理为了整顿一下员工的上班态度,决定员 工每迟到半小时将扣除 10 元的工资,若不迟到员工每天的工资有 80 元,计算每 位员工在今天可获得的工资。(要求用类完成)Input输入数据有多组,每组第一行输入一个整数 T,代表接下去有 T 行员工测试数据 每位员工的资料包含工号,姓名,打卡时间(hh:mm);Output输出每位员工的信息以及当天可获得的工资。Sample Input3
78 12080 LP 07:45 shik 08:00 junl 08:31 LP 07:45 shik 08:00Sample Output
12080 LP 07:45 80 shik 08:00 80 junl 08:31 60 LP 07:45 80 shik 08:00 80#include&iostream& #include&string& class stu { private: int H,M; public: stu(string num=&&,string name=&&,int hh=0,int mm=0) { this-&num=this-&name= H=M= } int SS() { 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; } void show() { cout&&this-&num&&& &&&this-&name&&& &; if(H&10) cout&&&0&; cout&&H&&&:&; if(M&10) cout&&&0&; cout&&M&&& &&&SS()&& } }; int main() { int T,hh, char c1,c2; while(cin&&T) { while(T--) { cin&&num&&name&& cin.get();cin&& stu one(num,name,hh,mm); one.show(); } } return 0; }5:找出每组数据中的最大值(函数重载)Time/Memory Limit:1000 MS/32768 K Submitted: 68 Accepted: 46Problem Description定义函数 Max 实现找出每组测试数据中的最大值,在主函数中进行调用,要求 Max 函数能够实现分别在 2 个 int 型数据、2 个字符串(不包括空格,长度不超 过 50)、3 个 double 型数据中找到最大值。Input输入数据有三组,每组占一行。Output对于每组输入数据,输出一行。Sample Input1 2 55.6 25.7 88.8 good morningSample Outputmax:2 max:88.8 max:morning #include&iostream& #include&string.h&
void Max(int a,int b) { if(a&=b) max=a; else max=b; cout&&&max:&&&max&& } void Max(double x,double y,double z) { if(x&=y) max=x; else max=y; if(max&z) max=z; cout&&&max:&&&max&& } void Max(char n1[50],char n2[50]) { char max3[50]; if(strcmp(n1,n2)&0) strcpy(max3,n1); else strcpy(max3,n2); cout&&&max:&&&max3&& } int main() { double x,y,z; int a,b; char n1[50],n2[50]; cin&&a&&b; cin&&x&&y&&z; cin&&n1&&n2; Max(a,b); Max(x,y,z); Max(n1,n2); return 0; } 6:长方形类与对象的定义Time/Memory Limit:1000 MS/32768 K Submitted: 57 Accepted: 54Problem Description定义一个类 area,它有两个整形的私有数据代表长方形的长和宽,有三个成员 函数 init、print、areas,init 用来给长方形初始化,print 用来输出长方形 的面积,areas 用来计算长方形的面积,要求将类定义完整,并通过 main 函数 定义相应的长方形对象,输出对象的面积。Input输入数据有多组,每组占一行,每行中有两个数,用空格分隔。Output对于每组输入数据,输出一行Sample Input5 6 7 8 2 4 5 2Sample Output10 24 35 16 #include&iostream& class Area { private: int l,w; public: void init(int l1,int w1) { l=l1; w=w1; } int SS() { s=l*w; } void print(); }; void Area::print() { cout&&SS()&& } int main() { A int l2,w2; while(cin&&l2&&w2) { a.init(l2,w2); a.print(); } return 0; }7:圆的面积Time/Memory Limit:1000 MS/32768 K Submitted: 100 Accepted: 48Problem Description设计一个 Circle 类,可以求圆的面积。Input输入数据有多组,每组占一行,每行包括一个实数 r,表示圆的半径。Output输出圆的面积。 其中 PI=3.14。Sample Input1 Sample Output3.14 #include&iostream& class Circle { private: public: void init(double r1) { r=r1; } double SS() { s=r*r*3.14; } void print(); }; void Circle::print() { cout&&SS()&& } int main() { C double r2; while(cin&&r2) { x.init(r2); x.print(); } return 0; }8:输出类对象Time/Memory Limit:1000 MS/32768 K Submitted: 59 Accepted: 47Problem Description 定义一个类 Student,用成员变量 name、no、age 描述一个学生的姓名、学号和 年龄的信息。 现从键盘获取三个不同学生的信息。按输入的顺序将它们存入相应 的对象中,并显示它们(一行一条记录)。Input数据之间用空格分割,每行输入一个记录。共 3 条记录。Output数据之间用一个空格分割,每行输出一个记录。共 3 条记录。Sample Inputzhangsan 1 21 lishi 2 19 wangwu 3 21Sample Outputzhangsan 1 21 lishi 2 19 wangwu 3 21 #include&iostream& #include&string.h& class Student { private: char name[20]; int no, public: void init(char n1[20],int no1,int age1) { strcpy(name,n1); no=no1; age=age1; } void print() { cout&&name&&& &&&no&&& &&&age&& } }; int main() { S char name2[20]; int n,a,j=0; while(cin&&name2&&n&&a) { x.init(name2,n,a); x.print(); } return 0; } #include&iostream& #include&string.h& class student { private: int no, char * public: student(char *na=&&,int n=0,int a=0) { int len=0; len=strlen(na); if(len!=0) { name=new char[len+1]; strcpy(name,na); } else name=NULL; no=n; age=a; } ~student() { if(strlen(name)!=0) delete [] } void show() { cout&&name&&& &&&no&&& &&&age&& } }; int main() { char n[20]=&&; int n1,a1; while(cin&&n&&n1&&a1) { student s(n,n1,a1); s.show(); } return 0; }9:构造函数的重载Time/Memory Limit:1000 MS/32768 K Submitted: 71 Accepted: 50Problem Description为日期类定义 4 个构造函数,分别是:不带参数,日期为 ;带一个参 数,仅初始化日数据,年数据为 2000,月数据为 1;带两个参数,初始化月、日 数据,年数据为 2000;带三个参数,初始化年、月、日;此外日期类还有一个输 出成员函数,用来输出年月日。在主函数中进行测试。Input输入数据有 3 行, 第一行只有一个数据,代表日的数值;第二行有两个数据, 代表月与日的数值,第三行有三个数据,代表年、月、日的数值。Output输出有 4 行,每行代表一个日期。Sample Input1 10 1 Sample Output
#include&iostream& class DATE { private: int year,month, public: DATE() { year=2000; month=1; day=1; } DATE(int da) { year=2000; month=1; day= } DATE(int m,int d) { year=2000; month=m; day=d; } DATE(int y,int m,int d) { year=y; month=m; day=d; } void show() { cout&&year&&&/&&&month&&&/&&&day&& } }; int main() { int y,m,d; cin&&d; DATE date2(d); date2.show(); cin&&m&&d; DATE date3(m,d); date3.show(); cin&&y&&m&&d; DATE date4(y,m,d); date4.show(); DATE date1; date1.show(); return 0; }10:构造函数重载Time/Memory Limit:1000 MS/32768 K Submitted: 60 Accepted: 54Problem Description自定义类一个类 Audiengce,类 Audiengce 内仅有一个整型、一个字符数组数据 成员,请分别定义两个构造函数,一个仅以整型数据作为行参,另外一个仅以字 符指针作为行参。 从键盘输入一个字符串和一个整型数据,用这些数据去创建两 个对象,使其分别调用两个不同的构造函数。并分别把对象中的数据显示出来。Input任意一个字符串,空格之后紧跟一个整数。Output第一个对象中字符串,第二个对象中整数,各占一行。Sample Inputguest 7Sample Outputguest 7 #include&iostream& class Audiengce { private: char * public: Audiengce(char *q) { int len=0; len=strlen(q); if(len!=0) { str=new char[len+1]; strcpy(str,q); } else str=NULL; x=0; } Audiengce(int y) { x=y; str=NULL; } ~Audiengce() { if(str!=NULL) delete [] } void showone() { cout&&str&& } void showtwo() { cout&&x&& } }; int main() { char str[100]; while(cin&&str&&x) { Audiengce one(str); one.showone(); Audiengce two(x); two.showtwo(); } return 0; } 11:实现自定义的析构函数Time/Memory Limit:1000 MS/32768 K Submitted: 66 Accepted: 48Problem Description自定义一个类 Person,类中仅有一个字符指针成员变量 name,用于保存从键盘 接收一任意长度的字符串。 要求根据该串的大小在创建对象时候动态申请一片空 间, 在析构该类的每个对象之前释放该对象拥有的堆中内存。请编写类析构函数 以实现这一意图。(为了观察,请在析构对象时候显示 deleted。)Input输入一字符串,不含空格。Output该对象中字符串内容 deletedSample InputabcdSample Outputabcd deleted #include&iostream& class Person { private: char * public: Person(char *str=&&) { int len=0; len=strlen(str); if(len!=0) { name=new char[len+1]; strcpy(name,str); } else str=NULL; } ~Person() { if(name!=NULL) delete [] cout&&&deleted&&& } void show() { cout&&name&& } }; int main() { char n[1000]; while(cin&&n) { Person one(n); one.show(); } return 0; }12:构造函数--定义矩形Time/Memory Limit:1000 MS/32768 K Submitted: 53 Accepted: 50Problem Description定义一个矩形类,要求定义成员函数实现:构造长方形、计算长方形的面积、周 长,假定长和宽分别由两个整型变量 high 和 width 表示。Input输入数据有多行,每行有两个数据,代表矩形的长和宽。Output每组输出占一行,为矩形的面积和周长并用空格隔开.Sample Input 1 2 10 20 100 200Sample Output2 6 200 60
#include&iostream& class Rectangle { private: double high, public: Rectangle(double h=0,double w=0) { high=h; width=w; } void area(); void perimeter(); }; void Rectangle::area() { s=high* cout&&s&&& &; } void Rectangle::perimeter() { l=2*(high+width); cout&&l&& } int main() { double h,w; while(cin&&h&&w) { Rectangle one(h,w); one.area(); one.perimeter(); } return 0; }13:对象数组Time/Memory Limit:1000 MS/32768 K Submitted: 77 Accepted: 50Problem Description定义一个学生类,数据成员包括:学号(整数),姓名,年龄,成绩;成员函数 包括设置值和输出显示。 在主函数中定义学生数组,表示多个学生,给每个学生设置值,然后输出显示。Input输入数据有多行,第一行为整数 n,表示一共有 n 个学生数据,接下来的 n 行为 学生数据信息,每行一个学生。Output输出有 n 行,每行代表一个学生,输出要有相应的提示,每行的信息之间用空格 分隔。Sample Input4 1 2 3 4 &wang& 18 86 &li& 18 72 &zhao& 17 80 &guo& 18 84Sample OutputId:1 Id:2 Id:3 Id:4 Name:&wang& Age:18 Score:86 Name:&li& Age:18 Score:72 Name:&zhao& Age:17 Score:80 Name:&guo& Age:18 Score:84#include&iostream& #include&string& class student { private: int num,age, public: student(int n1=0,string name=&&,int a1=0,int s1=0) { num=n1;this-&name=age=a1;score=s1; } void show() { cout&&&Id:&&&num&&& Name:&&&this-&name&&& Score:&&&score&& } }; int main() { student one[100]; int no,n,a,s,i; cin&&n; for(i=0;i&n;i++) { cin&&no&&name&&a&&s; one[i]=student(no,name,a,s); } for(i=0;i&n;i++) one[i].show(); return 0; }Age:&&&age&&&14:构造函数与拷贝构造函数――定义圆柱体 类Time/Memory Limit:1000 MS/32768 K Submitted: 57 Accepted: 44Problem Description为圆柱体编写一个 Cylinder 类,圆柱体由底面半径和高描述(float 型),这个 类具有如下的函数: a)得出圆柱体的体积:圆柱的体积=底面积×高 b)得出圆柱体的表面积:圆柱的表面积=上下底面面积+侧面积 c)能以如下方式创建一个圆柱体对象: (1) Cylinder c1;//构造一个默认底面半径为 10,高为 10 的圆柱体 (2) Cylinder c2(20,100);//构造一个底面半径为 20,高为 100 的圆柱体 (3) Cylinder c3(c1);//用一个已有的圆柱体构造一个新的圆柱体 在主函数中进行测试。Input输入数据有多行,每行有两个数据,代表圆柱体的底面半径和高。Output输出有多行,每行有两个数据,代表圆柱体的体积和表面积,最后两行分别表示 调用默认构造函数和拷贝构造函数的圆柱体对象的体积和表面积。Sample Input20 100 4 5 2 7Sample Output72 251.2 226.08 87.92 113.04 40 1256 #include&iostream& class Cylinder { private: float h,r; public: Cylinder() { r=10;h=10; } Cylinder(float r1,float h1) { r=r1;h=h1; } float VV() { v=3.14*r*r*h;
} float SS() { s=2*3.14*r*h+2*3.14*r*r; } void show() { cout&&VV()&&& &&&SS()&& } }; int main() { Cylinder c2(20,100); Cylinder c1; Cylinder c3(c1); float h2,r2; while(cin&&r2&&h2) { Cylinder c2(r2,h2); c2.show(); } c1.show();c3.show(); return 0; }15:构造函数、拷贝构造和析构函数――定义 学生类Time/Memory Limit:1000 MS/32768 K Submitted: 57 Accepted: 40Problem Description定义学生类,该类包含的数据成员有:学生编号,姓名,年龄,成绩;成员函数 有:构造函数,拷贝构造函数,析构函数,输出学生信息函数。其中构造函数要 输出&Constructing...&提示;拷贝构造函数要输出&Copy Constructing...&提 示;析构函数要输出&Destructing...&提示。在主函数中进行测试:主函数中先 定义一个学生对象 s1,数据从键盘中输入,再定义一个对象 s2,用对象 s1 对 s2 进行初始化。Input 输入数据只有一行,分别代表学生的编号,姓名,年龄,成绩。Output输出两个学生对象的信息,具体如下所示。Sample Input10 wang 19 88Sample OutputConstructing... Copy Constructing... Id:10 Name:wang Age:19 Score:88 Id:10 Name:wang Age:19 Score:88 Destructing... Destructing... #include&iostream& #include&string& class student { private: int num,age, char * public: student(int n1=0,char *na=&&,int a1=0,int s1=0) { num=n1; name=new char[strlen(na)+1]; strcpy(name,na); age=a1;score=s1; cout&&&Constructing...&&& } student(student &p) { num=p. name=new char[strlen(p.name)+1]; strcpy(name,p.name);age=p.score=p. cout&&&Copy Constructing...&&& } ~student() { delete [] cout&&&Destructing...&&& } void show() { cout&&&Id:&&&num&&& Name:&&&name&&& Age:&&&age&&& Score:&&&score&& } }; int main() { int n,a,s; char na[20]; cin&&n&&na&&a&&s; student s1(n,na,a,s); student s2(s1); s1.show(); s2.show(); return 0; }16:统计学生人数和成绩Time/Memory Limit:1000 MS/32768 K Submitted: 57 Accepted: 47Problem Description定义一个 Student 类记录学生的学号和 C++课程的成绩。要求使用静态成员变量 和静态成员函数计算全班学生 C++课程的总成绩和平均成绩。Input输入每个学生的学号和成绩,每个学生的信息占一行,直到文件结束。Output输出包括两行,第一行全班人数和总成绩,用空格隔开; 第二行平均成绩。Sample Input101 30 102 50 103 90 104 60 105 70Sample Output5 300 60 #include&iostream& #include&string& class student { private: public: student(string num,double score) { this-&num=this-&score= count++; sum=sum+ ave=double(sum/count); } static void show() { cout&&count&&& &&&sum&& cout&&ave&& } }; int student::count=0; double student::sum=0.0; double student::ave=0.0; int main() { while(cin&&num&&s) { student stu(num,s); } student::show(); return 0; }17:拷贝构造函数Time/Memory Limit:1000 MS/32768 K Submitted: 52 Accepted: 40Problem Description自定义一个类 User,类中有两 int 型成员变量 no、grade。请定义一个拷贝构造 函数,以实现用一个已经存在的对象去初始化一个新建的对象。为了方便观察, 在拷贝构造函数被调用时显示&copy ctr called&,在普通构造函数被调用时显 示&ctr called&(&:此处纯粹用于表示定界,实际输出时不必显示它,另外相 邻的两个单词之间有一个空格)。现从键盘获取 2 个 int 数据,先用它们创建一 个对象,然后用该对象再创建出一个新对象。Input两个数据之间用空格分割,回车结束。Output第一次创建对象时显示输出,占一行。 第二次调用拷贝构造函数时显示输出,占一行。Sample Input12 3Sample Outputctr called copy ctr called #include&iostream& class User { private: int no, public: User(int n1=0,int g1=0) { no=n1;grade=g1; cout&&&ctr called&&& } User(User &p) { no=p.grade=p. cout&&&copy ctr called&&& } }; int main() { int n,g; while(cin&&n&&g) { User stu1(n,g); User stu2(stu1); } return 0; }18:三角形面积Time/Memory Limit:1000 MS/32768 K Submitted: 66 Accepted: 48Problem Description设计一个三角形类 Triangle,包含三角形三条边长的私有数据成员。定义一个 友元函数,以实现求两个三角形对象的面积之和。Input输入数据有多组,每组占二行,每行包括 3 个整数,表示三角形的三条边(保证 能构成三角形)。Output对于每组数据,输出占一行。先是第 1 个三角形的面积,然后是第 2 个三角形的 面积,接下来是两个面积之和。中间用空格隔开。Sample Input3 4 5 4 5 6Sample Output 6.00 9.92 15.92Hints提示:已知三角形三条边求面积,一般采用海仑公式: S=sqrt(p(p-a)(p-b)(p-c)); a,b,c, ------三角形边长 p=(a+b+c)/2 #include&iostream& #include&iomanip& #include&cmath& class Triangle { private: int a,b,c; public: Triangle(int x=0,int y=0,int z=0) { a=x;b=y;c=z; } friend void Calculate(const Triangle &p1,const Triangle &p2); }; void Calculate(const Triangle &p1,const Triangle &p2) { double c1=0.0,c2=0.0,s1=0.0,s2=0.0; c1=double((p1.a+p1.b+p1.c)/2.0); c2=double((p2.a+p2.b+p2.c)/2.0); s1=double(sqrt(c1*(c1-p1.a)*(c1-p1.b)*(c1-p1.c))); s2=double(sqrt(c2*(c2-p2.a)*(c2-p2.b)*(c2-p2.c))); cout&&setiosflags(ios::fixed); cout&&setprecision(2)&&s1&&& &&&s2&&& &&&s1+s2&& } int main() { int x1,y1,z1; int x2,y2,z2; while(cin&&x1&&y1&&z1) { cin&&x2&&y2&&z2; Triangle one(x1,y1,z1); Triangle two(x2,y2,z2); Calculate(one,two); } return 0; }19:类的静态与友元――职工薪水Time/Memory Limit:1000 MS/32768 K Submitted: 65 Accepted: 48Problem Description设计一个职工类, 职工类中包括职工姓名、 薪水和所有职工薪水总和 (静态成员) 三个数据成员,成员函数包括有构造函数、析构函数、及返回所有职工薪水总和 的静态成员函数,此外还包含有一个友元函数,用来比较两个员工的薪水,结果 返回薪水大的那个员工。要求将类定义完整并且用主函数进行测试。Input输入包括 3 个测试数据Output输出包括 5 行,分别是 3 个职工的信息,总的薪水总和,还有薪水最高的那个职 工的信息。Sample InputZhangsan 1200 Lisi 2400 Wangwu 1800Sample OutputZhangsan 1200 Lisi 2400 Wangwu 1800 Allsalary is:5400 The highest salary is:Lisi 2400 #include&iostream& #include&string.h& class STAFF { private: char * public: STAFF(char *n=NULL,int s=0) { if(n!=NULL) { name=new char[strlen(n)+1]; strcpy(name,n); salary=s; sum=sum+ } else name=NULL; } STAFF(STAFF &x) { name=new char[strlen(x.name)+1]; strcpy(name,x.name); salary=x. } ~STAFF() { if(name!=NULL) delete [] } void show1() { if(name!=NULL) cout&&name&&& &&&salary&& } static void show2() { cout&&&Allsalary is:&&&sum&& } friend STAFF JUDGE(STAFF &p1,STAFF &p2); void show3() { if(name!=NULL) cout&&&The highest salary is:&&&name&&& &&&salary&& } }; int STAFF::sum=0; STAFF JUDGE(STAFF &p1,STAFF &p2) { if(p1.salary&=p2.salary) return p1; else return p2; } int main() { char n[30]; cin&&n&&s; STAFF one(n,s); cin&&n&&s; STAFF two(n,s); cin&&n&&s; STAFF three(n,s); one.show1(); two.show1(); three.show1(); STAFF::show2(); STAFF A=JUDGE(one,two); STAFF B=JUDGE(A,three); B.show3(); return 0; }20:统计学生信息Time/Memory Limit:1000 MS/32768 K Submitted: 78 Accepted: 49Problem Description现如今人人都已经统计完了综测分而轻松,然而 LP 却接到学委赋予的伟大任务 而手忙脚乱:统计班级综测分的情况,好吧,你懂的,他在向你求助。Input输入的数据有多行,每行为一个同学的姓名,学号及个人综测分,输入至文件尾 结束。Output对于每行输入的数据都进行输出,每次输出的数据有 2 行: 第一行为该同学的姓名,学号及综测分; 第二行为当前已统计的同学人数及当前已统计的所有同学的平均综测分。 (具体输出详见 Sample)Sample InputLinping
100Sample OutputName:Linping No: Score:80 Count:1 Ave:80 Name:Jinkun No: Score:100 Count:2 Ave:90 #include&iostream& #include&string& class student { private: public: student(string name=&&,string num=&&,double score=0.0) { this-&name= this-&num= this-&score= count++; sum=sum+this-& ave=sum/ } void showone() { cout&&&Name:&&&this-&name&&& No:&&&this-&num&&& Score:&&&this-&score&& } static void showtwo() { cout&&&Count:&&&count&&& Ave:&&&ave&& } }; int student::count=0; double student::sum=0.0; double student::ave=0.0; int main() { while(cin&&name&&num&&s) { student one(name,num,s); one.showone(); student::showtwo(); } return 0; }21:学生成绩等级Time/Memory Limit:1000 MS/32768 K Submitted: 56 Accepted: 47Problem Description有一个学生类 student,包括学生姓名、成绩,设计一个友元函数,输出成绩对 应的等级:大于等于 90:A;80~90:B;70~79:C;60~69:D;小于 60:E。Input先输入数据 n,表示有 n 个学生,接下是 n 个学生信息,包括姓名和成绩。Output对于所有的输入数据,输出学生数据(包括姓名、成绩和等级)。Sample Input5 st1 st2 st3 st4 st5 67 80 90 56 88 Sample Outputst1 st2 st3 st4 st5 67 80 90 56 88 D B A E B#include&iostream& #include&string& class student { private: public: student(string name=&&,double score=0.0) { this-&name= this-&score= } void showone() { cout&&this-&name&&& &&&this-&score&&& &; } friend void JUDGE(student &); }; void JUDGE(student &p) { if(p.score&=90) cout&&&A&&& else if(p.score&=80) cout&&&B&&& else if(p.score&=70) cout&&&C&&& else if(p.score&=60) cout&&&D&&& else cout&&&E&&& } int main() {
cin&&n; while(n--) { cin&&name&&s; student one(name,s); one.showone(); JUDGE(one); } return 0; }22:类的继承――定义学生类及其派生类Time/Memory Limit:1000 MS/32768 K Submitted: 85 Accepted: 49Problem Description定义一个学生类 Student,它包括学生名、住址和出生年份等私有数据以及输出 函数 show(),从它派生出一个大学生 Academician 类及一个小学生类 Primary_scholar。在大学生类中增加数据成员大学校名 university_name,在 小学生类中所增加的数据成员是两门课的成绩, 两个派生类所有有关的输出都由 函数 show()来实现。Input输入数据有多行,第一行有两个整数 n 和 m,分别代表大学生对象的个数 n 和小 学生对象的个数 m,接下来的 n 行为大学生的数据信息,m 行为小学生的数据信 息Output输出每个学生的信息,一个学生一行Sample Input2 3 zhang Fuzhou 1988 ZhangzhouNormalUniversity li Xiamen 1987 FuzhouUniversity wang Zhangzhou
su Quanzhou
Sample OutputName:zhang,Address:Fuzhou,Year:1988,University_Name:ZhangzhouNorm alUniversity Name:li,Address:Xiamen,Year:1987,University_Name:FuzhouUniversity Name:wang,Address:Zhangzhou,Year:1998,Score:75 89 Name:su,Address:Quanzhou,Year:1997,Score:90 90 Name:wu,Address:Putian,Year:1996,Score:78 87 #include&iostream& #include&string& class Student { private: string A public: Student(string name1,string Address1,int age1) { name=name1; Address=Address1; age=age1; } void show() { cout&&&Name:&&&name&&&,Address:&&&Address&&&,Year:&&&age&&&,&; } }; class Academician:public Student { private: string university_ public: Academician(string name1,string Address1,int university_name1):Student(name1,Address1,age1) { university_name=university_name1; } void show() { Student::show(); cout&&&University_Name:&&&university_name&& } };age1,string class Primary_scholar:public Student { private: double score1,score2; public: Primary_scholar(string name1,string Address1,int s2):Student(name1,Address1,age1) { score1=s1; score2=s2; } void show() { Student::show(); cout&&&Score:&&&score1&&& &&&score2&& } }; int main() { int n,m; string n1,A1,u1; int a1; double s1,s2; while(cin&&n&&m) { while(n--) { cin&&n1&&A1&&a1&&u1; Academician one(n1,A1,a1,u1); one.show(); } while(m--) { cin&&n1&&A1&&a1&&s1&&s2; Primary_scholar two(n1,A1,a1,s1,s2); two.show(); } } return 0; }age1,doubles1,double23:类的继承――定义点类及其派生的圆类Time/Memory Limit:1000 MS/32768 K Submitted: 64 Accepted: 51 Problem Description定义一个点类 point,它带有两个私有数据成员:x,y(整数);还有构造函数(x 和 y 的默认值为 0),显示坐标函数;point 类共有派生出圆类 circle,circle 类具备 point 类的全部特征,同时自身也有自己的特点:圆有半径成员(可以是 小数),求面积成员函数,显示成员函数,该显示函数可以显示圆的圆心坐标, 半径,和圆的面积。在主函数中对 circle 类进行测试。Input输入数据有多行, 第一行有一个整数 n, 表示一共有 n 个圆对象, 接下来的 n 行, 每行包括一共圆对象的圆心坐标和它的半径。Output输出有 n 行,每行为一个圆对象的圆心坐标、半径和它的面积。Sample Input3 0 0 2 1 2 3 2 3 6.5Sample OutputCentre:(0,0) Radius:2 Area:12.56 Centre:(1,2) Radius:3 Area:28.26 Centre:(2,3) Radius:6.5 Area:132.665 #include&iostream& class point { private: int x,y; public: point(int x1=0,int y1=0) { x=x1; y=y1; } void show() { cout&&&Centre:(&&&x&&&,&&&y&&&) &; } }; class circle:public point { private: public: circle(int x1=0,int y1=0,double r1=0.0):point(x1,y1) { r=r1; } double SS() { return 3.14*r*r; } void show() { point::show(); cout&&&Radius:&&&r&&& Area:&&&SS()&& } }; int main() { int n,x1,y1; double r1; while(cin&&n) { while(n--) { cin&&x1&&y1&&r1; circle one(x1,y1,r1); one.show(); } } return 0; }24:类的继承Time/Memory Limit:1000 MS/32768 K Submitted: 67 Accepted: 49Problem Description 编写一个学生和教师数据输入和显示程序, 学生数据有编号、 姓名、 班号和成绩, 教师数据有编号、姓名、职称和部门。要求设计一个 Person 类,包含编号、姓 名数据成员及构造和显示成员函数,该类作为学生类和教师类的基类。Input输入包括两行,第一行是学生的相关信息,第二行是教师的相关信息Output输出包括两行,第一行是学生的相关信息,第二行是教师的相关信息Sample Input Marry Computer(1) 89 02017 Tomy Lecturer ComputerSample OutputNum: Name:Marry Class:Computer(1) Score:89 Num:02017 Name:Tomy profession:Lecturer Department:Computer #include&iostream& #include&string& class person { private: public: person(string num1,string name1) { num=num1;name=name1; } void show() { cout&&&Num:&&&num&&& Name:&&&name&&& &; } }; class student:public person { private: public: student(string num1,string name1,string no1,double s1):person(num1,name1) { no=no1;score=s1; } void show() { person::show(); cout&&&Class:&&&no&&& Score:&&&score&& } }; class teacher:public person { private: public: teacher(string num1,string name1,string p1,string d1):person(num1,name1) { pro=p1; department=d1; } void show() { person::show(); cout&&&profession:&&&pro&&& Department:&&&department&& } }; int main() { string n1,nu1,no1; string n2,nu2,p,d; cin&&n1&&nu1&&no1&&s; cin&&n2&&nu2&&p&&d; student one(n1,nu1,no1,s); one.show(); teacher two(n2,nu2,p,d); two.show(); return 0; }24:长方形的面积和长方体的体积Time/Memory Limit:1000 MS/32768 K Submitted: 229 Accepted: 159 Problem Description先创建一个长方形类,有带参的构造函数,能够计算长方形的面积;在派生出长 方体类,有带参的构造函数,能够计算长方体的表面积和体积。Input输入数据有多组,每组包含 2 行数据,第 1 行 2 个实数,表示长方形的长和宽; 第 2 行 3 个实数,表示长方体的长、宽和高。Output输出长方形的面积和长方体的表面积和体积;具体格式见样例。Sample Input2 3 4 5 6Sample Output6 148 120 #include&iostream& class A { protected: double l,w; public: A(double l1=0.0,double w1=0.0) { l=l1; w=w1; } double SS() { return l*w; } void show() { cout&&SS()&& } }; class B:public A { private: public: B(double l1=0.0,double w1=0.0,double h1=0.0):A(l1,w1) { h=h1; } double S() { s=2*SS()+2*l*h+2*w*h; } double VV() { return h*SS(); } void show() { cout&&S()&&& &&&VV()&& } }; int main() { double l1,w1,l2,w2,h2; while(cin&&l1&&w1) { cin&&l2&&w2&&h2; A one(l1,w1); one.show(); B two(l2,w2,h2); two.show(); } return 0; }25:多继承――Time 类和 Date 类派生出 Birthtime 类Time/Memory Limit:1000 MS/32768 K Submitted: 66 Accepted: 56 Problem Description定义一个 Time 类,其数据成员包括时、分、秒,成员函数包括构造函数和输出 函数;定义一个 Date 类,其数据成员包括年、月、日,成员函数包括构造函数 和输出函数;定义一个派生类,它继承类 Time 和 Date,并且增加一个数据成员 childname 用于表示小孩的名字;设计主程序显示孩子的姓名和出生时间。Input输入数据有多行,每行包括一个字符串和六个整数,分别表示姓名、出生年月日 和出生时分秒。Output输出有多行,对应每个输入输出一行。输出格式见 Sample Output.Sample InputJack
10 10 10 Marry
8 0 30Sample OutputName:Jack Birthday: Time:10:10:10 Name:Marry Birthday: Time:8:0:30 #include&iostream& #include&string& class TIME { private: int hour,minute, public: TIME(int h,int m,int s) { hour=h;minute=m;second=s; } void show() { cout&&&Time:&&&hour&&&:&&&minute&&&:&&&second&& } }; class DATE { private: int year,month, public: DATE(int y,int m1,int d) { year=y;month=m1;day=d; } void show() { cout&&&Birthday:&&&year&&&/&&&month&&&/&&&day&&& &; } }; class CHILD:public TIME,public DATE { private: public: CHILD(string n,int y,int m1,int d,int h,int m,int s):DATE(y,m1,d),TIME(h,m,s) { childname=n; } void show() { cout&&&Name:&&&childname&&& &; DATE::show(); TIME::show(); } }; int main() { int y,m1,d,h,m,s; while(cin&&n&&y&&m1&&d&&h&&m&&s) { CHILD one(n,y,m1,d,h,m,s); one.show(); } return 0; }26:RoundTable 类Time/Memory Limit:1000 MS/32768 K Submitted: 83 Accepted: 54 Problem Description定义并描述一个 Table 类,一个 Circle 类和一个 RoundTable 类,Table 类包括 长、宽、高基本信息,Circle 类包含有半径基本信息。RoundTable 是一个圆形 桌子类。 要求将相应的类定义完整,并在主函数中定义一个 RoundTable 的对象, 输出该对象的所有信息。Input输入包括多组测试数据Output每组测试数据输出两行Sample Input1 2 3 4 10.5 6 1 0.6Sample OutputTable's Table's Table's Table's long,width,height are:1 2 3 radius is:4 long,width,height are:10.5 6 1 radius is:0.6Hints思考:圆桌与圆形、桌子之间是继承还是组合的关系? #include&iostream& #include&string& class Table { private: double l,w,h; public: Table(double l1,double w1,double h1) { l=l1;w=w1;h=h1; } void show() { cout&&&Table's long,width,height are:&&&l&&& &&&w&&& &&&h&& } }; class Circle { private: public: Circle(double r1) { r=r1; } void show() { cout&&&Table's radius is:&&&r&& } }; class RoundTable:public Table,public Circle { public: RoundTable(double l1,double w1,double h1,double r1):Table(l1,w1,h1),Circle(r1) { } void show() { Table::show(); Circle::show(); } }; int main() { double l1,w1,h1,r1; while(cin&&l1&&w1&&h1&&r1) { RoundTable one(l1,w1,h1,r1); one.show(); } return 0; } #include&iostream& #include&iomanip& class Table { private: double L,W,H; public: Table(double l=0.0,double w=0.0,double h=0.0) { L=l;W=w;H=h; } void show() { cout&&&Table's long,width,height are:&&&L&&& &&&W&&& &&&H&& } }; class Circle { private: double R; public: Circle(double r=0.0) { R=r; } void show() { cout&&&Table's radius is:&&&R&& } }; class RoundTable { private: T C public: RoundTable(double l,double w,double h,double r):one(l,w,h),two(r) { } void show() { one.show(); two.show(); } }; int main() { double l,w,h,r; while(cin&&l&&w&&h&&r) { RoundTable three(l,w,h,r); three.show(); } return 0; }27:类的继承Time/Memory Limit:1000 MS/32768 K Submitted: 52 Accepted: 48Problem Description定义一个类 point,它有两个私有数据成员:点的横坐标 x 和纵坐标 y。 三个公有的成员函数:构造、修改和显示,用于操作数据成员 x 和 y。类 point 的定义如下: class point { private: public: point(double x,double y); void set_point(double a,double b); void show(); }; 我们用这个类派生出一个类 circle。其新增数据成员为圆的半径,及求圆面积 成员函数。根据以上信息, 用 point 类派生 circle 类, 并给出圆的面积。 PI=3.14Input测试数据有多行,第一行表示有几个测试数据,接下来的每行有三个浮点数,分 别表示圆心坐标和圆的半径。Output按上面要求,输出圆的面积。结果保留两位小数。Sample Input2 1 1 520. 3.1415926Sample Output 30.99 #include&iostream& #include&iomanip& class point { private: double x,y; public: point(double x1,double y1) { x=x1;y=y1; } void set_point(double a,double b) { x=a;y=b; } void show() { cout&&x&&& &&&y&& } }; class circle:public point { private: public: circle(double x1,double y1,double r1):point(x1,y1) { r=r1; } double SS() { return 3.14*r*r; } void show() { cout&&setiosflags(ios::fixed); cout&&setprecision(2)&&SS()&& } }; int main() { double x1,y1,r1; int T; while(cin&&T) { while(T--) { cin&&x1&&y1&&r1; circle one(x1,y1,r1); one.show(); } } return 0; }28:奇葩后代Time/Memory Limit:1000 MS/32768 K Submitted: 85 Accepted: 51Problem Description某天,小 Y 带上女朋友逛宠物店。女朋友让小 Y 写一个 Animal 的基类,包含名 字,月龄。函数成员包含一个叫声行为(即输出它们的叫声表示他正在叫),一个 输出基本信息函数和构造函数并派生出 Dog,Cat,但已知它们叫声不同,狗的叫 声是“wang~wang”,猫的是“miao~miao”。这两个类,已知 Dog 类包含一天吃 骨头的根数,Cat 类包含猫一天吃鱼的条数,每个类都包含输出函数。当女友看 到一只贴着猫狗兽标签的奇葩动物时,瞬间石化了,其实那只奇葩还真的有猫和 狗的所有特征(除了叫声是“miao~~wangwang&)。故小 Y 又得从 Dog 和 Cat 派 生出 DOG_CAT 类。这下小 Y 凌乱了,只好请来聪明的你来帮忙。Input输入多组数据,每组数据三行,第一行为狗的信息,第二行为猫的信息,第三行 为他们的“奇葩后代”的信息。具体请看输入样例。Output每组输出占三行,第一行为狗的信息,第二行为猫的信息,第三行为他们的“奇 葩后代”的信息。每个信息间含两个空格。具体输出请看输出样例。 Sample Inputwangcai 9 10 Mimi 8 4 qipa 2 2 5Sample OutputName:wangcai Age:9 Bone:10 wang~wang Name:Mimi Age:8 Fish:4 miao~miao Name:qipa Age:2 Bone:2 Fish:5 miao~~wangwang #include&iostream& #include&string& class Animal { private: public: Animal(string n,int age1) { name=n;age=age1; } void show() { cout&&&Name:&&&name&&& &&&&Age:&&&age&&& &; } }; class Dog:virtual public Animal { private: public: Dog(string n,int age1,int b):Animal(n,age1) { bone=b; } int BB() { } void show() { Animal::show(); cout&&&Bone:&&&bone&&& wang~wang&&& } }; class Cat:virtual public Animal { private: public: Cat(string n,int age1,int f):Animal(n,age1) { fish=f; } int FF() { } void show() { Animal::show(); cout&&&Fish:&&&fish&&& miao~miao&&& } }; class DOG_CAT:public Dog,public Cat { public: DOG_CAT(string n,int age1,int b,int f):Animal(n,age1),Dog(n,age1,b),Cat(n,age1,f) { } void show() { Animal::show(); cout&&&Bone:&&&BB()&&& Fish:&&&FF()&&& miao~~wangwang&&& } }; int main() { int age1,b,f; while(cin&&n&&age1&&b) { Dog one(n,age1,b); cin&&n&&age1&&f; Cat two(n,age1,f); cin&&n&&age1&&b&&f; DOG_CAT three(n,age1,b,f); one.show(); two.show(); three.show(); } return 0; }29:ATMTime/Memory Limit:1000 MS/32768 K Submitted: 70 Accepted: 50Problem Description校园 IC 卡木有钱了,LP 前往自动存取款机取钱,长久的排队让他开始思 考(假设他是一个爱思考的人好了): 自动存取款机内部是如何运行用户的指令的呢?Input有多组测试数据,每组第一行为帐户的信息(卡号(长度不超过 19)及姓名(长 度不超过 19),Ps:初始有开户金额:10),而后有多行 命令,命令有如下几种形式: “save 100”形式为存入金额; “draw 100”形式为取出金额; “show”形式为显示当前账户信息,包含卡号、姓名及金额; “end”形式为结束该组测试数据。Output对每行命令输出当前操作对应的结果。(具体格式详见输出样例)Sample Input123456 linping show save 1000 draw 700 show draw 400 show end Sample Output123456 linping 10 123456 linping 310 error! 123456 linping 310 #include&iostream& #include&string& class ATM { private: public: ATM(string nu=&&,string na=&&) { num=name=sum=10; } double SAVE(int x) { sum=sum+x; } double DRAW(int x) { if(x&sum) cout&&&error!&&& else sum=sum-x; } void show() { cout&&num&&& &&&name&&& &&&sum&& } }; int main() { string s1=&show&; string s2=&draw&; string s3=&save&; string s4=&end&; string nu,na, while(cin&&nu&&na) { ATM one(nu,na); while(cin&&str) { if(str==s4) else if(str==s1) one.show(); else { cin&&x; if(str==s2) one.DRAW(x); else if(str==s3) one.SAVE(x); } } } return 0; }30:虚基类-在职读书的教师类Time/Memory Limit:1000 MS/32768 K Submitted: 67 Accepted: 48Problem Description定义并描述一个人员类 Person,它派生出学生类 Student 和教师类 Teacher,学 生类和教师类又共同派生出在职读书的教师类 StuTech。人员类有姓名、性别、 身份证号、出生年月等信息;学生类除继承来的以外有学号、成绩等信息;教师 类有职称等信息。要求:将相应的类定义完整,并在 main 函数中定义 StuTech 的对象,输出对象的所有信息。Input输入包括多组测试数据,每组数据包括:姓名,性别,身份证,出生年,月,学 号,成绩,职称Output每组测试数据输出多行Sample Input Tom Man 5 5
85 LecturerSample OutputThe imformation of study-teacher is: Name:Tom Sex:Man Identity card:020035 Birthday:1985.5 Num: Score:85 profession:Lecturer #include&iostream& #include&string& class Person { private: string ID; int year, public: Person(string na,string se,string id,int y,int m) { name=sex=ID=year=y;month=m; } void show() { cout&&&Name:&&&name&&& Sex:&&&sex&&& Identity card:&&&ID&&& Birthday:&&&year&&&.&&&month&& } }; class Student:virtual public Person { private: public: Student(string na,string se,string id,int y,int m,string nu,double s):Person(na,se,id,y,m) { num=score=s; } void show() { cout&&&Num:&&&num&&& Score:&&&score&& } }; class Teacher:virtual public Person { private: public: Teacher(string na,string se,string id,int y,int m,string pr):Person(na,se,id,y,m) { pro= } void show() { cout&&&profession:&&&pro&& } }; class StuTech:public Student,public Teacher { public: StuTech(string na,string se,string id,int y,int m,string nu,double s,string pr): Person(na,se,id,y,m),Student(na,se,id,y,m,nu,s),Teacher(na,se,id,y,m,pr) { } void show() { cout&&&The imformation of study-teacher is:&&& Person::show(); Student::show(); Teacher::show(); } }; int main() { string na,id,nu,se, int y,m; while(cin&&na&&se&&id&&y&&m&&nu&&s&&pr) { StuTech one(na,se,id,y,m,nu,s,pr); one.show(); } return 0; }31:类的虚基类――客货两用汽车 Time/Memory Limit:1000 MS/32768 K Submitted: 58 Accepted: 46Problem Description设计一个 Automobile(汽车)类,派生出 Car 类和 Wagon 类,而后两类又派生 出 StationWagon 类, 将四个类定义完整,并在主函数中定义 StationWagon 类的 对象进行测试。 其中:Automobile 类包含有 power 数据成员,及 show 成员函数 Car 类包含有 seat 数据成员,及 show 成员函数 Wagon 包含有 load 数据成员,及 show 成员函数 StationWagon 包含有 show 成员函数。Input输入有多组数据,每组数据包含:动力,座位数,载重量Output每组数据输出多行Sample Input105 5 200 120 5 250Sample OutputAutomobile constructing... Car constructing... Wagon constructing... StationWagon constructing... StationWagon's power:105 seat:5 load:200 Automobile constructing... Car constructing... Wagon constructing... StationWagon constructing... StationWagon's power:120 seat:5 load:250 #include&iostream& #include&string& class Automobile { private:
public: Automobile(double p) { power=p; cout&&&Automobile constructing...&&& } void show() { cout&&&StationWagon's power:&&&power&&& &; } }; class Car:virtual public Automobile { private: public: Car(double p,int s):Automobile(p) { seat=s; cout&&&Car constructing...&&& } void show() { cout&&&seat:&&&seat&&& &; } }; class Wagon:virtual public Automobile { private: public: Wagon(double p,double l):Automobile(p) { load=l; cout&&&Wagon constructing...&&& } void show() { cout&&&load:&&&load&& } }; class Station:public Car,public Wagon { public: Station(double p,int s,double l):Automobile(p),Car(p,s),Wagon(p,l) { cout&&&StationWagon constructing...&&& } void show() { Automobile::show(); Car::show(); Wagon::show(); } }; int main() { double p,l; while(cin&&p&&s&&l) { Station one(p,s,l); one.show(); } return 0; }32:类的虚基类――主任工程师类Time/Memory Limit:1000 MS/32768 K Submitted: 54 Accepted: 46Problem Description设计一个虚基类 base, 包含有姓名和年龄是有数据成员以及相关的成员函数 (构 造、显示),由它派生出领导类 leader,包含职务和部门私有数据成员以及相 关的成员函数。再由 base 类派生出工程师类 engineer,包含职称和专业私有数 据成员以及相关的成员函数, 然后由 leader 类和 engineer 类派生出主任工程师 类 chairman。将类定义完整并在主函数中测试。Input输入有多组数据,每组数据包括:姓名,年龄,职务,部门,职称,专业Output每组数据输出多行 Sample InputJacky 40 CEO ManagerDepartment Engineer ElectronicsSample OutputChief engineer's Name:Jacky Age:40 Post:CEO Department:ManagerDepartment Profession:Engineer Specialty:Electronics #include&iostream& #include&string& class base { private: public: base(string na,int ag) { name=age= } void show() { cout&&&Chief engineer's Name:&&&name&&& Age:&&&age&& } }; class leader:virtual public base { private: public: leader(string na,int ag,string po,string de):base(na,ag) { post=depart= } void show() { cout&&&Post:&&&post&&& Department:&&&depart&& } }; class engineer:virtual public base { private: public: engineer(string na,int ag,string pr,string sp):base(na,ag) { pro=spe= } void show() { cout&&&Profession:&&&pro&&& Specialty:&&&spe&& } }; class chairman:public leader,public engineer { public: chairman(string na,int ag,string po,string de,string pr,string sp): base(na,ag),leader(na,ag,po,de),engineer(na,ag,pr,sp) { } void show() { base::show(); leader::show(); engineer::show(); } }; int main() { string na,po,de,pr, while(cin&&na&&ag&&po&&de&&pr&&sp) { chairman one(na,ag,po,de,pr,sp); one.show(); } return 0; }33:NBA2KOLTime/Memory Limit:1000 MS/32768 K Submitted: 64 Accepted: 46Problem Description 小 k 最近迷上了 NBA2KOL 游戏。 几局游戏下来,他想知道自己的各项数据的场均 值。现在由你来编写一个代码来帮助小 k。定义一个类 person,他有两个私有数 据成员,姓名 name(不包含空格)和球场上的位置 position。再由这个类派生 出一个类 count,其中新增 4 个数据成员,比赛场数、得分、篮板数、助攻数, 以及输入各项数据的函数与求各项数据的场均值的函数。Input输入数据有多组, 每组的第一行表示该组有 n 个测试数据, 接下来分别输入姓名, 位置,比赛 m 场,接下来有 m 行,每行 3 个数据,分别表示每场的得分、篮板数、 助攻数。Output输出姓名,位置,场均得分、场均篮板数、场均助攻数。输出格式按示例,保留 1 位小数。每个输出后都有十个“*”。Sample Input2 Lin PG 3 10 3 3 8 4 7 6 2 10 Bryant SG 4 15 1 3 18 0 4 12 4 8 10 6 5 1 Wang PF 2 8 4 3 7 5 1Sample OutputName:Lin Position:PG Averaging score:8.0 Averaging rebound:3.0 Averaging assists:6.7 ********** Name:Bryant Position:SG Averaging score:13.8 Averaging rebound:2.8 Averaging assists:5.0 ********** Name:Wang Position:PF Averaging score:7.5 Averaging rebound:4.5 Averaging assists:2.0 ********** #include&iostream& #include&string& #include&iomanip& class person { private: public: person(string na,string po) { name=position= } void show() { cout&&&Name:&&&name&&& Position:&&&position&& } }; class Cout:public person { private: double * double * double * public: Cout(string na,string po,int m,double a[][3]):person(na,po) { sum=m; int i=0,j=0; score=new double[sum]; rebounds=new double[sum]; attack=new double[sum]; for(i=0;i&i++) for(j=0;j&3;j++) { score[i]=a[i][0];rebounds[i]=a[i][1];attack[i]=a[i][2]; } } ~Cout() {
} double AVES() { int i=0; double aves=0.0; for(i=0;i&i++) aves=aves+score[i]; return aves/ } double AVER() { int i=0; double aver=0.0; for(i=0;i&i++) aver=aver+rebounds[i]; return aver/ } double AVEA() { int i=0;double avea=0.0; for(i=0;i&i++) avea=avea+attack[i]; return avea/ } void show() { person::show(); cout&&setiosflags(ios::fixed)&&setprecision(1); cout&&&Averaging score:&&&AVES()&&& Averaging assists:&&&AVEA()&& cout&&&**********&&& } }; int main() { int n,m,i,j; string na, double a[100][3]; while(cin&&n) { while(n--) { cin&&na&&po&&m; for(i=0;i&m;i++) for(j=0;j&3;j++)Averagingrebound:&&&AVER()&&& cin&&a[i][j]; Cout one(na,po,m,a); one.show(); } } return 0; }34:总价多少Time/Memory Limit:1000 MS/32768 K Submitted: 37 Accepted: 11Problem DescriptionMichael 最近开了一家网店,经营优乐美袋装奶茶(单价 0.68 元,每包 20 克)。 由于运费与买家距离有关,所以现在,Michael 想请你帮忙写一个程序,能够帮 他计算出每单订单的实际交易额。 定义一个类 Customer, 含有成员 name, address, 构造函数与输出函数; 类 Date, 含有成员 year, month, day 以及构造函数与输出函数。 由类 Customers 与类 Date 派生出类 Oders。类 Oders 中新增成员:重量 weight(包数 n*每包的重量,以 克为单位) ,距离 distance (以公里为单位) , 数量 n (以包为单位) , 运费 fara。 要求类 Oders 中有一个友元函数,来计算最终交易额。 class Customer { private: char name,//定义姓名,地址 public: //构造函数 //输出函数 }; class Date { private: int year,month,//定义日期 public: //构造函数 //输出函数 }; class Oders:public Customer,public Date { private: int weight,n,diastance,//新增成员重量、数量、距离、运费 public: //构造函数 friend void calculate(Oders &s);//友元函数计算交易额 //输出函数 }; 运费与距离的关系:在(0,1000]之间免运费;(]之间首斤 4 元,然后 每增加 1 斤运费增加 2 元;(2500,∞)首斤 8 元,每增加 1 斤运费增加 4 元。Input输入数据有多组,每组数据有姓名,地址,日期,数量,距离。Output输出格式参考输出示例。(每组输出的前后都有*号,每组输出后有一空行)Sample InputJack Zhangzhou
10 800 Michael Beijing
25 2230Sample Output******************** Customer Name:Jack Address:Zhangzhou Date: Product Name:u.loveit Unit price:0.68 Distance:800 meters Fara:0 Total Price:6.8 Welcome to you next ******************** ******************** Customer Name:Michael Address:Beijing Date: Product Name:u.loveit Unit price:0.68 Distance:2230 meters Fara:4 Total Price:21 Welcome to you next ******************** #include&iostream& class Customer { private: char *name,*Amount:10 Price:6.8Amount:25 Price:17 public: Customer(char *na,char *add) { name=new char[strlen(na)+1]; address=new char[strlen(add)+1]; strcpy(name,na); strcpy(address,add); } ~Customer() { delete [] delete [] } void show() { cout&&&Customer Name:&&&name&&& Address:&&&address&& } }; class Date { private: int year,month, public: Date(int y,int m,int d) { year=y;month=m;day=d; } void show() { cout&&&Date:&&&year&&&/&&&month&&&/&&&day&& } }; class Oders:public Customer,public Date { private: int weight,n,fata, public: Oders(char *na,char *add,int y,int m,int d,int n1,int d1): Customer(na,add),Date(y,m,d) { n=n1;distance=d1;fata=0;weight=0; } friend void calculate(Oders &s); void show() { cout&&&********************&&& Customer::show(); Date::show(); cout&&&Product Name:u.loveit Unit price:0.68 Price:&&&n*0.68&& cout&&&Distance:&&&distance&&& meters Fara:&&&fata&& cout&&&Total Price:&&&0.68*n+fata&& cout&&&Welcome to you next&&& cout&&&********************&&&endl&& } }; void calculate(Oders &s) { s.weight=(s.n-25)*20/500; if(s.distance&0&&s.distance&=1000) s.fata=0; else if(s.distance&=2500) s.fata=4+s.weight*2; else s.fata=8+s.weight*4; } int main() { char na[30],add[100]; int y,m,d,n1,d1; while(cin&&na&&add&&y&&m&&d&&n1&&d1) { Oders one(na,add,y,m,d,n1,d1); calculate(one); one.show(); } return 0; }Amount:&&&n&&&35:时钟类Time/Memory Limit:1000 MS/32768 K Submitted: 66 Accepted: 32Problem Description定义一个时钟类(Clock),含三个整形元素(H,M,S)分别代表小时,分钟,秒,构 造初始化数据函数,另外,定义一个函数实现两个时钟相加,一个显示函数,输 出格式为 H:M:S Input输入数据有多组,每组两行,第一行输入三个整 数:h1(0&=h1&=23),m1(0&=m1&=59),s1(0&=s1&=59),分别代表是一个 24 小时制 的电子钟开始时显示的小时,分钟,秒,第二行仍输入三个整 数:h2(h2&=0),m2(0&=m2&=59),s1(0&=s2&=59),代表电子钟经历的时长Output每组输出占一行,输出现在电子钟上显示的时间,格式为“小时:分钟:秒”,注 意:此题不考虑电子钟显示 00~09 的形式,用 0~9 表示即可。Sample Input13 30 30 1 10 30 1 0 0 25 0 0Sample Output14:41:0 2:0:0 #include&iostream& #include&iomanip& class CLOCK { private: int H,M,S; public: CLOCK(int h=0,int m=0,int s=0) { H=h;M=m;S=s; } double add(int h1,int m1,int s1) { H=H+h1; M=M+m1; S=S+s1; if(S&=60) { M++;S=S-60; } if(M&=60) { H++;M=M-60; } while(H&=24) H=H-24; return H,M,S; } void show() { cout&&H&&&:&&&M&&&:&&&S&& } }; int main() { int h,m,s,h1,m1,s1; while(cin&&h&&m&&s) { cin&&h1&&m1&&s1; CLOCK one(h,m,s); one.add(h1,m1,s1); one.show(); } return 0; }36:平均数Time/Memory Limit:1000 MS/32768 K Submitted: 44 Accepted: 40Problem Description定义具有统计功能的学生类。 输入学生成绩,输出当前分数总和除以当前总人数 的结果。Input输入数据有多行,每行为一个浮点数。Output输出数据有多行 每行输出当前分数总和除以当前总人数的结果(保留两位小数) Sample Input1 2 3 4 5 6Sample Output1.00 1.50 2.00 2.50 3.00 3.50 #include&iostream& #include&iomanip& class student { private: public: student(double s=0.0) { score=s; count++; sum=sum+ ave=sum/ } static void show() { cout&&setiosflags(ios::fixed); cout&&setprecision(2)&&ave&& } }; double student::ave=0.0; double student::sum=0.0; int student::count=0; int main() { while(cin&&x) { student one(x); student::show(); } return 0; }37:社会保障卡Time/Memory Limit:1000 MS/32768 K Submitted: 2 Accepted: 1Problem Description漳州发行了新的社会保障卡, 这种卡集合了银行卡与医保卡的功能于一身,社会 保障卡的银行存款额与医保存款额是分开的,它可以像银行卡那样进行存、取、 查等业务,取款的时候,如果余额小于取款额,则无法取款并给出一定的提示; 它也可以像医保卡那样每月存入医保额,这部分钱是个人出一半,工作单位再出 一半,比如当月个人扣除的医保额是 50 元,则医保账户存入的是 100 元,保障 卡还可以进行买药与查询功能, 在买药的时候,社保机构规定一天最多只能用医 保卡买 100 元(包括 100)以内的药品,超过 100 元的部分可以用现金支付,同 时如果买药的钱大于医保余额时,医保余额将被扣空,剩下的也用现金支付。现 在要求你定义一个 Card 类,该类包含账号与用户名两个数据成员,及构造与显 示信息等成员函数;由 Card 类派生出 Bankcard 类和 Medicare 两个类,根据说 明给这两个类新增数据成员及成员函数,要求银行卡能查询余额,存钱及取钱, 医保卡能查询余额、存入医保额及买药;最后由这两个类派生出社会保障卡类, 使得该类可以集合上述两种类的功能。Input输入数据有多组,每组有多行,第一行是社会保障卡的基本信息,包括账号、用 户名、密码、银行卡余额、医保卡余额,接下来的若干行是对保障卡的操作,每 个操作都由命令及必要的数字组成,命令如下:show-显示社会保障卡的基本信 息,bsave-向银行卡存钱,bdraw-从银行卡部分取钱,bshow-显示银行卡余额, msave-存入医保额(只包括个人该出的部分),mdraw-买药(从医保额支出), mshow-显示医保余额,stop-该组测试数据测试结束。命令后面跟的数值部分是 操作的数额。Output 如输出样例所示,每组数据测试完成之后用十个*分隔。Sample Input Jacky 0 200 show bsave 200 bdraw 2000 bdraw 100 bshow mdraw 201 msave 40 mshow mdraw 90 mshow stop
Marry 0 1000 mdraw 200 mshow stopSample OutputSocial Security Card: Card ID: User Name:Jacky Account:1000 Medicare Card Account:200 Insufficient account balance! Account:1100 Medicare Card Account:180 Medicare Card Account:90 ********** Medicare Card Account:900 ********** #include&iostream& #include&string& class Card { private: string ID, public: Card(string id,string na) { ID=name= } void show() { cout&&&Card ID:&&&ID&&& User Name:&&&name&& } }; class Bankcard:virtual public Card { private: public: Bankcard(string id,string na,string pa,double s): Card(id,na) { sum=s; password= } void bsave(double x) { sum=sum+x; } void bdraw(double x) { if(x&sum) cout&&&Insufficient account balance!&&& else sum=sum-x; } void bshow() { cout&&&Account:&&&sum&& } }; class Medicare:virtual public Card { private: double count,y; public: Medicare(string id,string na,double c): Card(id,na) { count=c;y=100; } void msave(double x) { count=count+2*x; } void mdraw(double x) { if(x&=100) count-=x; else count-=100; if(count&0) count=0; } void mshow() { cout&&&Medicare Card Account:&&&count&& } }; class Social:public Bankcard,public Medicare { public: Social(string id,string na,string pa,double s,double c): Card(id,na),Bankcard(id,na,pa,s),Medicare(id,na,c) { } void show() { cout&&&Social Security Card:&&& Card::show(); bshow(); mshow(); } }; int main() { char na[25],id[30],pa[30],str[30]; double s,c,x; while(cin&&id&&na&&pa&&s&&c) { Social one(id,na,pa,s,c); while(cin&&str) { if(strcmp(str,&show&)==0) one.show(); else if(strcmp(str,&bshow&)==0) one.bshow(); else if(strcmp(str,&mshow&)==0) one.mshow(); else if(strcmp(str,&stop&)==0) { cout&&&**********&&& } else { cin&&x; if(strcmp(str,&bsave&)==0) one.bsave(x); else if(strcmp(str,&bdraw&)==0) one.bdraw(x); else if(strcmp(str,&msave&)==0) one.msave(x); else if(strcmp(str,&mdraw&)==0) one.mdraw(x); } } } return 0; }38:运算符重载――重载+和-对复数类对象进 行加减运算Time/Memory Limit:1000 MS/32768 K Submitted: 63 Accepted: 47Problem Description定义一个复数类,该类包含两个 double 型的数据成员代表复数的实部和虚部, 包含构造函数(默认值为 0,0),和显示函数,现重载运算符+和-,使其能对复 数类对象进行加和减运算。在主函数中进行测试Input输入数据有多行, 每行包括 4 个数,前两个代表参与运算的第一个复数对象的实 部和虚部,后两个代表第二个复数对象的实部和虚部。 Output输出多行,每行包括了加和减运算后的结果。Sample Input100 20 -10 20 10 23 20 34 2.5 6 3.5 1.3Sample Output(90,40) (110,0) (30,57) (-10,-11) (6,7.3) (-1,4.7) #include&iostream& class Complex { private: double real, public: Complex(double r=0,double i=0) { real=r;imag=i; } void show1() { cout&&&(&&&real&&&,&&&imag&&&) &; } void show2() { cout&&&(&&&real&&&,&&&imag&&&)&&& } Complex operator+(Complex c); Complex operator-(Complex c); }; Complex Complex::operator+(Complex c) { C temp.real=real+c. temp.imag=imag+c. } Complex Complex::operator-(Complex c) { C temp.real=real-c. temp.imag=imag-c. } int main() { double r1,i1,r2,i2; while(cin&&r1&&i1&&r2&&i2) { Complex A(r1,i1); Complex B(r2,i2); Complex add, add=A+B;sub=A-B; add.show1();sub.show2(); } return 0; }39:复数类的运算符重载Time/Memory Limit:1000 MS/32768 K Submitted: 49 Accepted: 45Problem Description定义描述复数 complex 的类 (前缀减)operator-- (成员) (前缀加)operator++ (友元) (后缀减)operator-- (成员) (后缀加)operator++ (友元)Input输入多个测试实例,每个测试实例占一行,表示一个复数Output每个输出实例占一行。如下输出所示,每行有四组复数,分别是对输入的复数对 象进行前缀减,前缀加,后缀减,后缀加。每个复数之间用空格隔开。Sample Input 1 1 2 2 3 3Sample Output(0,0) (1,1) (0,0) (1,1) (1,1) (2,2) (1,1) (2,2) (2,2) (3,3) (2,2) (3,3) #include&iostream& class Complex { private: double real, public: Complex(double r=0,double i=0) { real=r;imag=i; } Complex operator--(); Complex operator--(int); friend Complex operator++(Complex &); friend Complex operator++(Complex &,int); void show1() { cout&&&(&&&real&&&,&&&imag&&&) &; } void show2() { cout&&&(&&&real&&&,&&&imag&&&)&&& } }; Complex Complex::operator--() { ----return * } Complex Complex::operator--(int) { Complex temp(*this); real--;imag--; } Complex operator++(Complex &op) { ++op.++op. } Complex operator++(Complex &op,int) { op.real++;op.imag++; } int main() { double r,i; while(cin&&r&&i) { Complex op(r,i); --op.show1(); ++op.show1(); op--;op.show1(); op++;op.show2(); } return 0; }40:三角形面积之和Time/Memory Limit:1000 MS/32768 K Submitted: 44 Accepted: 43Problem Description设计一个三角形类 Triangle,包含三角形三条边长的私有数据成员,另有一个 重载运算符“+”,以实现求两个三角形对象的面积之和。Input输入数据有多组,每组占二行,每行包括 3 个整数,表示三角形的三条边(保证 能构成三角形)。Output对于每组数据,输出占一行。先是第 1 个三角形的面积,然后是第 2 个三角形的 面积,接下来是两个面积之和。中间用空格隔开。Sample Input 3 4 5 4 5 6Sample Output6.00 9.92 15.92 #include&iostream& #include&cmath& #include&iomanip& class Triangle { private: int a,b,c; public: Triangle(int a1=0,int b1=0,int c1=0) { a=a1;b=b1;c=c1; double p=0.0; p=double((a+b+c)/2.0); area=double(sqrt(p*(p-a)*(p-b)*(p-c))); } void show1() { cout&&setiosflags(ios::fixed); cout&&setprecision(2)&&area&&& &; } void show2() { cout&&area&& } friend Triangle operator+(Triangle &op,Triangle &ob); }; Triangle operator+(Triangle &op,Triangle &ob) { T temp.area=op.area+ob. } int main() { int a1,b1,c1,a2,b2,c2; while(cin&&a1&&b1&&c1) { cin&&a2&&b2&&c2; Triangle one(a1,b1,c1); Triangle two(a2,b2,c2); one.show1(); two.show1(); T three=one+ three.show2(); } return 0; }41:运算符重载――T 重载运算符“&”,用于对 给定的两个字符串进行比较Time/Memory Limit:1000 MS/32768 K Submitted: 54 Accepted: 48Problem Description重载运算符“&”,用于对给定的两个字符串进行比较,返回值为一个整数: s1&s2,返回 1;s1&s2,返回-1;s1==s2,返回 0。类的结构参考如下: class string { private: char * public: string(); string(const char *); ~string(){} int operator&(const string &); }; 将类定义完整,并在主函数中进行测试。Input输入数据有多行,每行包括要比较的两个字符串。Output输出有多行,对应每行输入输出一行。Sample Input abcd 09ab abcd helabCd ab09 efgh helSample Output1 -1 -1 0 #include&iostream& #include&string.h& class String { private: char * public: String() { } String(const char *s) { int len=0; len=strlen(s); if(len!=0) { str=new char[strlen(s)+1]; strcpy(str,s); } else str=NULL; } ~String() { if(str!=NULL) delete [] } int operator&(const String &); }; int String::operator&(const String &c) { if(strcmp(str,c.str)&0) x=1; else if(strcmp(str,c.str)==0) x=0; else x=-1; } int main() { char s1[100]=&&,s2[100]=&&; while(cin&&s1&&s2) { String A(s1); String B(s2); x=A&B; cout&&x&& } return 0; }42:复数Time/Memory Limit:1000 MS/32768 K Submitted: 63 Accepted: 44Problem Description设计一个复数类,实现复数的加减乘除计算功能:Input首先输入整数 t,表示有 t 组数据,每组数据奇数行为 m,n 分别为一个复数的实 部和虚部,偶数行为‘+’、‘-’、‘*’、‘/’、‘=’任意一个字符,根据 偶数行的输入决定其前后两行的运算。运算一律从前往后算。若偶数行输入为 ‘=’,则结束运算,并输出运算结果。Output输出最后计算的结果,以 m+ni 或 m-ni,其中 m,n 分别为复数的实部和虚部(如 果 m 为 0,则不用输出)。若除 0,则停止计算,并输出”divide by zero”的 信息。Sample Input 3 1 2 + 4 5 * 2 2 = 1 2 / 1 2 = 2.3 4.5 / 0 0 =Sample Output-4+24i 1 divide by zeroHints(a+bi)+(c+di)=(a+c)+(b+d)i (a+bi)-(c+di)=(a-c)+(b-d)i (a+bi)*(c+di)=(ac-bd)+(bc+ad)i (a+bi)/(c+di)=(ac+bd)/(c*c+d*d)+(bc-ad)/(c*c+d*d) i #include&iostream& class Complex { private: double real, public: Complex(double r,double i) { real=r;imag=i; } void Add(double m,double n) { real+=m; imag+=n; } void Subtract(double m,double n) { real-=m; imag-=n; } void Multiply(double m,double n) { double x,y; x=real*m-imag*n; y=real*n+imag*m; real=x;imag=y; } void Divide(double m,double n) { double t=0.0,x,y; t=1/(m*m+n*n); x=(real*m+imag*n)*t; y=(m*imag-real*n)*t; real=x;imag=y; } void show() { if(real==0&&imag==0) cout&&&0&; else { if(real!=0) cout&& if(imag&0&&real!=0) cout&&&+&; if(imag!=0) cout&&imag&&'i'; } cout&& } }; int main() { int t,flag=1; double r,i,m,n; cin&&t; while(t--) { flag=1; cin&&r&&i; Complex one(r,i); cin&&c; while(c!='=') { cin&&m&&n; if(c=='+') one.Add(m,n); else if(c=='-') one.Subtract(m,n); else if(c=='*') one.Multiply(m,n); else if(c=='/') { if(m==0&&n==0) { flag=0; cout&&&divide by zero&&& } else one.Divide(m,n); } cin&&c; } if(flag) one.show(); } return 0; }43:计数器Time/Memory Limit:1000 MS/32768 K Submitted: 55 Accepted: 50Problem Description设计一个计数器 Counter,用类成员重载自增运算符实现计数器的自增,用友元 重载实现计数器的自减。Input输入数据有多组,每组包含两个整数。第一个数 m 为计数器的基数,第二个数 n (n&0 为计数器增加,n&0 计数器减少)的数。Output 输出计数器自增或自减的结果。每组输出占一行。Sample Input1 10 5 -4Sample Output11 1 #include&iostream& class Counter { private: int x,y; public: Counter(int x1=0,int y1=0) { x=x1;y=y1; } void show() { cout&&x&& } Counter operator++() { int i=0; for(i=0;i&y;i++) ++x; return * } friend Counter operator--(Counter &op) { int i=0; for(i=0;i&-op.y;i++) --op.x; } }; int main() { int m,n; while(cin&&m&&n) { Counter one(m,n); if(n&0) ++ else if(n&0) -- one.show(); } return 0; }44:复整数相加Time/Memory Limit:1000 MS/32768 K Submitted: 55 Accepted: 45Problem Description定义一个复数类,含有复数的实部和虚部 class complex { int real, public: complex (int r=0,int i=0); ~complex() { } void print() { ... } friend complex operator + (complex a, int b) { ... } friend complex operator + (int a,complex b) { ... } friend complex operator + (complex a,complex b) { ... } }; 根据以上类的定义提示,及相应函数,将其补充完整,实现复整数的加法。 Input输入数据有多组,每组输入为三行,第一行是两个相加实数的类型 (总共有三种情况&zhengshu + fushu&,&fushu + fushu&,&fushu + zhengshu&),&zhengshu& ,代表大于 0 的整数,&fushu&,代表复数,并默认输入 的复数实部和虚部都大于 0,每组接下来依次输入实数或复数,若为复数,则输 入两个大于 0 的整数, 分别代表复数的实部和虚部, 若为整数, 则只输入一个数, 每组输入默认符合上述要求。Output每组输出结果为 a+bi(默认所有的结果 a&0,b&1)占一行。Sample Inputzhengshu + fushu 1 1 2 fushu + fushu 2 3 3 2 fushu + zhengshu 4 4 1Sample Output2+2i 5+5i 5+4i #include&iostream& class complex { private: int real, public: complex(int r=0,int i=0) { real=r;image=i; } ~complex() { } void print() { cout&&real&&&+&&&image&&'i'&& } friend complex operator+(complex a,int b) { return complex(a.real+b,a.image); } friend complex operator+(int a,complex b) { return complex(a+b.real,b.image); } friend complex operator+(complex a,complex b) { return complex(a.real+b.real,a.image+b.image); } }; int main() { char str[20]; int r,i,x,r1,i1; while(cin.getline(str,20)) { if(strcmp(str,&zhengshu + fushu&)==0) { cin&&x;cin&&r&&i; complex com1(r,i), com=x+com1; com.print(); } else if(strcmp(str,&fushu + fushu&)==0) { cin&&r&&i;cin&&r1&&i1; complex com1(r,i),com2(r1,i1), com=com1+com2; com.print(); } else if(strcmp(str,&fushu + zhengshu&)==0) { cin&&r&&i;cin&&x; complex com1(r,i), com=com1+x; com.print(); } } return 0; }45:运算符重载矩阵相加Time/Memory Limit:1000 MS/32768 K Submitted: 60 Accepted: 46Problem Description有两个矩阵 a 和 b,均为 2 行 3 列。求两个矩阵之和。重载运算符“+”,使之 能用于矩阵相加。如: c=a+b。 补充下面的类: class Matrix { public: Matrix(); friend Matrix operator+(Matrix & ,Matrix & ); void input(); void show(); private: int mat[2][3]; };Input输入数据有多组,每组数据第一行有一个整数 N,代表有 N 个 2 行 3 列的矩阵。Output输出 N 个数组相加的矩阵。按照矩阵格式输出,每个数据后面均有空格。Sample Input2 1 4 1 1 3 1 4 1 2 5 1 1 3 6 1 12 3 5 6 2 3 4 5 6 1 2 3 4 5 6Sample Output2 3 4 5 6 7 3 6 9 12 15 18 #include&iostream& class Matrix { private: int mat[2][3]; public: Matrix() {int i,j; for(i=0;i&2;i++) for(j=0;j&3;j++) mat[i][j]=0; } Matrix(int ma[2][3]) { int i=0,j=0; for(i=0;i&2;i++) for(j=0;j&3;j++) mat[i][j]=ma[i][j]; } friend Matrix operator+(Matrix &op,Matrix &ob); void input() { } void show() { int i=0,j=0; for(i=0;i&2;i++) { for(j=0;j&3;j++) cout&&mat[i][j]&&& &; cout&& } } }; Matrix operator+(Matrix &op,Matrix &ob) { M int i,j; for(i=0;i&2;i++) for(j=0;j&3;j++) temp.mat[i][j]=op.mat[i][j]+ob.mat[i][j]; } int main() { int n,a[2][3],i,j; while(cin&&n) {Matrix C; while(n--) { for(i=0;i&2;i++) for(j=0;j&3;j++) cin&&a[i][j]; Matrix A(a); C=C+A; } C.show(); } return 0; }46:数列中 n 个整数排序Time/Memory Limit:1000 MS/32768 K Submitted: 71 Accepted: 46Problem Description设计一个包含 size 个数的数列,要求能够把从指定位置 x 开始的 y 个数排列成 降序,并输出新的完整的数列。可将数列存放在一维数组中。 例如,原来列有 10 个数,值为{1,8,3,0,5,9,7,6,9,8},若要求把从 第 4 个数开始的 5 个数排成降序, 则得到的新数列为{1,8,3,9,7,6,5,0,9,8}。试建立一个类 LIST,来 完成上述功能。 class LIST { public: LIST(int a[],int len); //构造函数,用 len 初始化 size,根据 size 动态分配 数组存储空间,arr 指向该存储空间 void sortpart(int m,int n); //将数列从第 m 个元素开始的 n 个数排成降序 void output(); //输出整个数列 ~LIST(); //析构函数,释放 arr 指向的存储空间 private: //数列元素的个数 int * //数列数组的起始指针 };Input输入数据有多组,每组: 第 1 行输入整型数列的个数 第 2 行输入整型数列各个元素的值; 第 3 行输入 x 和 y, 表示将超长整型数列从指定位置 x 开始的 y 个数排列成降序;Output每组输出排序后的结果,占一行。每个元素后带一个空格。Sample Input10 1 4 2 7 3 8 2 8 23 6 3 5Sample Output1 4 8 7 3 2 2 8 23 6 #include&iostream& class LIST { private: int * public: LIST(int a[],int len) { size= arr=new int[size]; for(i=0;i&i++) arr[i]=a[i]; }; void sortpart(int m,int n) { int i=0,j=0,k=0,temp=0; for(i=m-1;i&m+n-2;i++) { k=i; for(j=k+1;j&n+m-1;j++) if(arr[k]&arr[j]) k=j; temp=arr[i];arr[i]=arr[k];arr[k]= } } void output() { int i=0; for(i=0;i&i++) cout&&arr[i]&&& &; cout&& } ~LIST() { delete [] } }; int main() { int n,a[100],x,y,i=0; while(cin&&n) { for(i=0;i&n;i++) cin&&a[i]; cin&&x&&y; LIST one(a,n); one.sortpart(x,y); one.output(); } return 0; }47:运算符重载――重载赋值运算符=用于字 符串赋值 Time/Memory Limit:1000 MS/32768 K Submitted: 73 Accepted: 52Problem Description定义一个字符串类,该类包括一个字符型指针数据成员,构造函数、析构函数、 显示字符串函数,以及重载=运算符函数(用于字符串赋值)。在主函数中对字 符串对象的赋值运算进行测试。Input输入数据有多行,每行包括一个字符串。Output输出有多行,对应每个输

我要回帖

更多关于 矩形的定义 的文章

 

随机推荐