教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 资格考试 >

c++数据结构上机题及实现代码(6)

来源:网络收集 时间:2026-01-24
导读: (3) Cylinder c3(c1);//用一个已有的圆柱体构造一个新的圆柱体 在主函数中进行测试。 Input 输入数据有多行,每行有两个数据,代表圆柱体的底面半径和高。 Output 输出有多行,每行有两个数据,代表圆柱体的体积

(3) Cylinder c3(c1);//用一个已有的圆柱体构造一个新的圆柱体 在主函数中进行测试。

Input

输入数据有多行,每行有两个数据,代表圆柱体的底面半径和高。

Output

输出有多行,每行有两个数据,代表圆柱体的体积和表面积,最后两行分别表示调用默认构造函数和拷贝构造函数的圆柱体对象的体积和表面积。

Sample Input

20 100 4 5 2 7

Sample Output

125600 15072 251.2 226.08 87.92 113.04 3140 1256 3140 1256

#include using namespace std; class Cylinder {

private: float h,r; public: Cylinder() { r=10;h=10; } Cylinder(float r1,float h1) { r=r1;h=h1; } float VV() { float v; v=3.14*r*r*h;

return v; } float SS() { float s; s=2*3.14*r*h+2*3.14*r*r; return s; } void show() { cout<

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: 40

Problem Description

定义学生类,该类包含的数据成员有:学生编号,姓名,年龄,成绩;成员函数有:构造函数,拷贝构造函数,析构函数,输出学生信息函数。其中构造函数要输出\提示;拷贝构造函数要输出\提示;析构函数要输出\提示。在主函数中进行测试:主函数中先定义一个学生对象s1,数据从键盘中输入,再定义一个对象s2,用对象s1对s2进行初始化。

Input

输入数据只有一行,分别代表学生的编号,姓名,年龄,成绩。

Output

输出两个学生对象的信息,具体如下所示。

Sample Input

10 wang 19 88

Sample Output

Constructing...

Copy Constructing...

Id:10 Name:wang Age:19 Score:88 Id:10 Name:wang Age:19 Score:88 Destructing... Destructing...

#include #include using namespace std; class student {

private: int num,age,score; char *name; public: student(int n1=0,char *na=\ { num=n1; name=new char[strlen(na)+1]; strcpy(name,na); age=a1;score=s1; cout<<\ } student(student &p) { num=p.num; name=new char[strlen(p.name)+1]; strcpy(name,p.name);age=p.age;score=p.score; cout<<\ } ~student() {

delete []name; cout<<\ } void show() { cout<<\ } };

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: 47

Problem Description

定义一个Student类记录学生的学号和C++课程的成绩。要求使用静态成员变量和静态成员函数计算全班学生C++课程的总成绩和平均成绩。

Input

输入每个学生的学号和成绩,每个学生的信息占一行,直到文件结束。

Output

输出包括两行,第一行全班人数和总成绩,用空格隔开; 第二行平均成绩。

Sample Input

101 30 102 50 103 90

104 60 105 70

Sample Output

5 300 60

#include #include using namespace std; class student {

private: double score; string num; static int count; static double sum; static double ave; public: student(string num,double score) { this->num=num;this->score=score; count++; sum=sum+score; ave=double(sum/count); } static void show() { cout<int student::count=0; double student::sum=0.0; double student::ave=0.0; int main() { double s; string num; while(cin>>num>>s) { student stu(num,s); } student::show();

…… 此处隐藏:402字,全部文档内容请下载后查看。喜欢就下载吧 ……
c++数据结构上机题及实现代码(6).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/411995.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)