【C++】第九章 作业
三角形类
【问题描述】
先定义一个能描述平面上一条线段的类Beeline,包含私有数据成员为线段两个端点的坐标(X1,Y1,X2,Y2),在类中定义形参默认值为0的构造函数,计算线段长度的公有成员函数Length(),显示线段两个端点坐标的公有成员函数show()。然后再定义一个能描述平面上三角形的类Triangle,其数据成员为用Beeline定义的对象line1,line2,line3。在类中定义的构造函数要能对对象成员进行初始化。再定义计算三角形面积的函数Area()及显示三条边端点坐标及面积的函数Print(),Print()函数中可调用show()函数显示三条边两端点坐标。
【输入形式】
输入三角形三个顶点的坐标(x1,y1)、(x2,y2)、(x3,y3)。
其中 -100 <= x1,x2,x3,y1,y2,y3 <= 100,且为整数。
在主函数中创建类对象tri(x1,y1,x2,y2,x3,y3),对应line1(x1, y1, x2, y2),line2(x2,y2,x3,y3),line3(x3,y3,x1,y1)。
【输出形式】
调用Print()函数,将三角形三条边的端点坐标及面积。面积保留两位小数。
具体格式见样例。
【样例输入】
0 0
0 4
3 0
【样例输出】
Three edges’ points are listed as follows:
(0, 0),(0, 4)
(0, 4),(3, 0)
(3, 0),(0, 0)
The area of this triangle is: 6.00.
【提示】
1.严格按照输出样例输出,建议复制。
2.计算面积建议用海伦公式。
3.严格控制保留2位小数。
4.如果没有严格使用类,得分为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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using namespace std;
class beeline
{
private:
int x1, y1, x2, y2;
public:
beeline()
{
x1 = 0; x2 = 0; y1 = 0; y2 = 0;
}
beeline(int, int, int, int);
float length();
void show();
};
beeline::beeline(int a, int b, int c, int d)
{
x1 = a;
y1 = b;
x2 = c;
y2 = d;
}
float beeline::length()
{
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
void beeline::show()
{
cout << "(" << x1 << ", " << y1 << ")" << "," << "(" << x2 << ", " << y2 << ")" << endl;
}
class Triangle
{
private:
beeline line1, line2, line3;
public:
Triangle(int, int, int, int, int, int);
float Area();
void print();
};
Triangle::Triangle(int a, int b, int c, int d, int e, int f) :line1(a, b, c, d), line2(c, d, e, f), line3(e, f, a, b)
{
}
float Triangle::Area()
{
float x, y, z;
x = line1.length();
y = line2.length();
z = line3.length();
return ((1 / 4.00) * sqrt((x + y + z) * (x + y - z) * (x + z - y) * (y + z - x)));
}
void Triangle::print()
{
cout << "Three edges' points are listed as follows:";
cout << endl;
line1.show();
line2.show();
line3.show();
cout << "The area of this triangle is: ";
cout << std::fixed << std::setprecision(2) << (*this).Area() << ".";
}
int main()
{
int a, b, c, d, e, f;
cin >> a >> b >> c >> d >> e >> f;
Triangle x(a, b, c, d, e, f);
x.print();
}
学生成绩类
【问题描述】
设计学生成绩类Score。在主函数中定义学生成绩对象数组s[]。用Sum()计算每个学生的总成绩、用Show()显示每个学生的成绩。增加静态成员函数getAvg(),用于返回学生的总平均分。通过增加合适的成员、修改成员函数等完成这一功能。
【输入形式】
包含一组测试数据。第一行输入一个整数n(1<=n<=100)。
接下来n行。每行先输入一个整数op:
当op==1时,输入x, y, z。代表输入一位新同学i(i从1开始编号)的语文、数学、英语成绩,无需输出。
当op==2时,输入i,输出第i同学的总成绩。数据保证这位同学的成绩已经录入。
当op==3时,输入i,依次输出第i同学的语文数学英语成绩,成绩之间用空格隔开。
当op==4时,输出当前已经录入学生的总平均分,结果保留两位小数。
(1<=n<=100, 1<=id<=10, 1<=op<=3, 0<=x,y,z<=100,全部输入都为整型数)
【输出形式】
注意输入之间会有一些输出,但测试只看cout结果。
【题解】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
69
70
71
using namespace std;
class Score
{
private:
int Chinese, Math, English;
static int TotalScore;
static int TotalStudent;
public:
Score() {}
void setScore(int c, int m, int e)
{
Chinese = c;
Math = m;
English = e;
TotalScore += c + m + e;
TotalStudent++;
}
int Sum()
{
return(Chinese + Math + English);
}
void Show()
{
cout << Chinese << " " << Math << " " << English;
}
double static getAve()
{
return (double)TotalScore / TotalStudent;
}
};
int Score::TotalScore = 0;
int Score::TotalStudent = 0;
int main()
{
int n, op, i, c, m, e;
cin >> n;
int id = 1;
Score sco[11];
while (n--)
{
cin >> op;
if (op == 1)
{
cin >> c >> m >> e;
sco[id].setScore(c, m, e);
id++;
}
else if (op == 2)
{
cin >> i;
cout << sco[i].Sum();
cout << endl;
}
else if (op == 3)
{
cin >> i;
sco[i].Show();
cout << endl;
}
else
{
cout << std::fixed << std::setprecision(2) << Score::getAve();
cout << endl;
}
}
return 0;
}
电视类
【问题描述】
补全设计一个TV类和一个Remote类。Remote类的成员函数是TV类的友元, 电视类有状态、频道和音量基本属性,默认初始频道为5,默认初始音量为20。状态有开和关(-1表示关机状态,其他为开机状态)。
在主函数根据输入的op值进行不同操作。补全代码使程序满足如下要求。
【输入形式】
当op==1时,
输入电视操作命令如下:
OFF_ON(切换电视开关机状态)
VOL_UP(电视音量+1)
VOL_DOWN(电视音量-1)
CHA_NEXT(电视频道+1)
CHA_PRE(电视频道-1)
CHA_TO x(0<=x<=100,将电视频道切到x)
VOL_TO x(0<=x<=100,将电视音量切到x)
其中CHA_TO与VOL_TO通过调用友元类实现。
当op==2时,输出当前电视状态。
当op==3时,结束程序。
【输出形式】
当op==2时,输出当前电视状态,具体格式见样例。
【题解】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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using namespace std;
class TV;
class Remote
{
public:
Remote() {};
void volume_to(TV &tv, int x);
void channel_to(TV &tv, int x);
};
class TV
{
private:
int state;
int channel;
int volume;
public:
friend void Remote::volume_to(TV &tv, int x);
friend void Remote::channel_to(TV &tv, int x);
TV() {};
TV(int st) :state(st),volume(20),channel(5){}
void onoff() {
state = -state;
}
void cha_next() {
channel++;
}
void cha_pre() {
channel--;
}
void vol_up() {
volume++;
}
void vol_down() {
volume--;
}
void print() {
if(state == -1)
{
cout << "The TV is OFF" << endl;
}
else {
cout << "The TV is ON" << endl;
cout << "The channel is " << channel << endl;
cout << "The volume is " << volume << endl;
}
}
};
void Remote::volume_to(TV &tv, int x) {
tv.volume = x;
}
void Remote::channel_to(TV &tv, int x) {
tv.channel = x;
}
int main()
{
int x, op;
string s;
TV tv(-1);
Remote rem;
while(1) {
cin >> op;
if(op == 1) {
cin >> s;
if(s == "OFF_ON") tv.onoff();
else if(s == "VOL_UP") tv.vol_up();
else if(s == "VOL_DOWN") tv.vol_down();
else if(s == "CHA_NEXT") tv.cha_next();
else if(s == "CHA_PRE") tv.cha_pre();
else if(s == "CHA_TO") {
cin >> x;
rem.channel_to(tv, x);
} else if(s == "VOL_TO") {
cin >> x;
rem.volume_to(tv, x);
}
} else if(op == 2){
tv.print();
} else {
break;
}
}
return 0;
}