题目1

【问题描述】

请设计球类Sphere ,完成main函数对其进行的测试。以下是main函数,请依据其进行Sphere的设计,并拷贝该代码进行测试、不得修改:

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
26
27
int main()

{

Sphere s;

cout<<fixed<<setprecision(2);

s.SetRadiu(1);//缺省设置球半径为1

cout<<"Radius:"<<s.GetRadius()//获取半径

<<",Superficial area:"<<s.GetArea()//求表面积

<<",Volume:"<<s.GetVolume()<<endl;//求体积

double r;

cin>>r;

s.SetRadiu(r);

cout<<"Radius:"<<s.GetRadius()<<",Superficial area:"<<s.GetArea()

<<",Volume:"<<s.GetVolume()<<endl;

}

【代码】

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68


#include <iostream>
#include <iomanip>
#define pi 3.1415926535//需要定义pi
using namespace std;

class Sphere {

private:

double R,V,A;

public:

double SetRadiu(double r){

R = r;
return R;
}

double GetRadius(){

return R;

}

double GetArea(){
A=pi*R*R*4;
return A;
}

double GetVolume(){

V=pi*4/3*R*R*R;
return V;
}

};//结尾不能忘记加;

int main()

{

Sphere s;

cout<<fixed<<setprecision(2);

s.SetRadiu(1);//缺省设置球半径为1

cout<<"Radius:"<<s.GetRadius()//获取半径

<<",Superficial area:"<<s.GetArea()//求表面积

<<",Volume:"<<s.GetVolume()<<endl;//求体积

double r;

cin>>r;

s.SetRadiu(r);

cout<<"Radius:"<<s.GetRadius()<<",Superficial area:"<<s.GetArea()

<<",Volume:"<<s.GetVolume()<<endl;

}

题目2

【问题描述】

根据以下部分代码,请设计矩形类Rectangle,实现对矩形对象的长和宽进行读、写,并求矩形面积,完成所给代码(原样拷贝、不应修改)对其进行的测试。

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
26
27
28
29
30
31
32
33
void PrintRectangle(Rectangle& r)

{

cout<<"Length:"<<r.GetLength();//获取长度

cout<<",Width:"<<r.GetWidth()<<endl;//获取宽度

cout<<"Area:"<<r.Area()<<endl;//求面积

}

int main()

{

Rectangle r;

r.SetData(10,10);//修改长和宽

PrintRectangle(r);

r.SetLength(20);//修改长度

PrintRectangle(r);

r.SetWidth(20);//修改宽度

PrintRectangle(r);

return 0;

}

【代码】

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<iostream>
using namespace std;
class Rectangle
{
private:
double length;
double width;
public:
void SetData(double a, double b)
{
this->length = a;
this->width = b;
}
void SetLength(double a)
{
this->length = a;
}
void SetWidth(double b)
{
this->width = b;
}
double GetLength()
{
return this->length;
}
double GetWidth()
{
return this->width;
}
double Area()
{
return this->length * this->width;
}
};


void PrintRectangle(Rectangle& r)
{
cout << "Length:" << r.GetLength();//获取长度
cout << ",Width:" << r.GetWidth() << endl;//获取宽度
cout << "Area:" << r.Area() << endl;//求面积
}

int main()
{
Rectangle r;
r.SetData(10, 10);//修改长和宽
PrintRectangle(r);
r.SetLength(20);//修改长度
PrintRectangle(r);
r.SetWidth(20);//修改宽度
PrintRectangle(r);
return 0;
}

题目3

【问题描述】

编写时间类,实现时间的显示和增加(输入超过60进行取余处理)

【样例输入/输出】

请输入当前时间:12 15 13
当前时间为:12:15:13
请输入要添加的时间:1 2 3
增加后的时间:13:17:16

请输入当前时间:13 65 13
当前时间为:13:5:13
请输入要添加的时间:1 63 20
增加后的时间:14:8:33

【代码】

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55


#include <iostream>
using namespace std;

class cloc {

private:

double H,M,S;

public:

void SetTime(int h,int m,int s){

H = (h>=0&&h<24)?h:(h-60);
M = (m>=0&&m<60)?m:(m-60);
S = (s>=0&&s<60)?s:(s-60);

}

void ShowTime(){

cout<<H<<":"<<M<<":"<<S<<endl;

}

void AddTime(int h,int m,int s){

H = ((H+h)>=0&&(H+h)<24)?(H+h):(H+h-60);
M = ((M+m)>=0&&(M+m)<60)?(M+m):(M+m-60);
S = ((S+s)>=0&&(S+s)<60)?(S+s):(S+s-60);

}
};

int main()

{
int h,m,s;
cloc t;
cout<<"请输入当前时间:";
cin>>h>>m>>s;
t.SetTime(h,m,s);
cout<<"当前时间为:";
t.ShowTime();
cout<<"请输入要添加的时间:";
cin>>h>>m>>s;
t.AddTime(h,m,s);
cout<<"增加后的时间:";
t.ShowTime();

}
//注意转换成UTF-8格式

代码可能略有不足,请大佬多多指教