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

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

来源:网络收集 时间:2026-01-24
导读: using namespace std; void Max(int a,int b) { int max; if(a>=b) max=a; else max=b; cout void Max(double x,double y,double z) { double max; if(x>=y) max=x; else max=y; if(max void Max(char n1[50],char

using namespace std; void Max(int a,int b) { int max; if(a>=b) max=a; else max=b; cout<<\}

void Max(double x,double y,double z) { double max; if(x>=y) max=x; else max=y; if(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<<\}

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

Problem Description

定义一个类area,它有两个整形的私有数据代表长方形的长和宽,有三个成员函数init、print、areas,init用来给长方形初始化,print用来输出长方形的面积,areas用来计算长方形的面积,要求将类定义完整,并通过main函数定义相应的长方形对象,输出对象的面积。

Input

输入数据有多组,每组占一行,每行中有两个数,用空格分隔。

Output

对于每组输入数据,输出一行

Sample Input

5 2 6 4 7 5 8 2

Sample Output

10 24 35 16

#include using namespace std; class Area {

private: int l,w; public: void init(int l1,int w1) { l=l1; w=w1;

} int SS() { int s; s=l*w; return s; } void print(); };

void Area::print() { cout<

int main() { Area 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: 48

Problem Description

设计一个Circle类,可以求圆的面积。

Input

输入数据有多组,每组占一行,每行包括一个实数r,表示圆的半径。

Output

输出圆的面积。 其中PI=3.14。

Sample Input

1

Sample Output

3.14

#include using namespace std; class Circle {

private: double r; public: void init(double r1) { r=r1; } double SS() { double s; s=r*r*3.14; return s; } void print(); };

void Circle::print() { cout<

int main() { Circle x; double r2; while(cin>>r2) { x.init(r2); x.print(); } return 0; }

8:输出类对象

Time/Memory Limit:1000 MS/32768 K

Submitted: 59 Accepted: 47

Problem Description

定义一个类Student,用成员变量name、no、age描述一个学生的姓名、学号和年龄的信息。现从键盘获取三个不同学生的信息。按输入的顺序将它们存入相应的对象中,并显示它们(一行一条记录)。

Input

数据之间用空格分割,每行输入一个记录。共3条记录。

Output

数据之间用一个空格分割,每行输出一个记录。共3条记录。

Sample Input

zhangsan 1 21 lishi 2 19 wangwu 3 21

Sample Output

zhangsan 1 21 lishi 2 19 wangwu 3 21

#include #include using namespace std; class Student {

private: char name[20]; int no,age; public: void init(char n1[20],int no1,int age1) { strcpy(name,n1); no=no1; age=age1; } void print() { cout<

int main()

…… 此处隐藏:177字,全部文档内容请下载后查看。喜欢就下载吧 ……
c++数据结构上机题及实现代码(3).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)