问题一
题目内容:
定义如下形式的point 类,其对象表示平面上的一个点(x,y),设计一个友元函数dis()求出两个对象(平面点)间的距离。并编制主函数,通过类对象验证相关函数的正确性。
class point {
double x,y;
public:
point (double x0=0, double y0=0) {x=x0; y=y0;}
void display();
};
运用两点间的距离公式,开根号函数为sqrt()。
输入格式:
四个实数,前两个实数是一个点的坐标,后两个实数是另一个点的坐标。坐标中前一个数是横坐标,后一个数是纵坐标。
输出格式:
输出三行数据,第一行是第一个点坐标,第二行是第二个点坐标,坐标输出形式为(x,y),第三行是一个实数,代表两个点之间的距离。
输入样例:
1.2 -3.5 -1.5 6
输出样例:
(1.2,-3.5)
(-1.5,6)
9.87623
#include<iostream> #include<cmath> using namespace std; class point { public: double x, y; point(double x0 = 0, double y0 = 0) { x = x0; y = y0; } void display() { cout << '(' << x << ',' << y << ')' << endl; } friend double dis(); }; double dis(point& p1, point& p2) { return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)); } int main() { double x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; point p1(x1, y1); point p2(x2, y2); p1.display(); p2.display(); cout << dis(p1, p2); return 0; }
按照题目意思,x,y应该是私有成员,但是vs2019友元函数访问不了私有成员,就很离谱。所以只能把x,y改成公有成员了
问题二
题目内容:
根据下面部分代码,补充完成学生成绩类Score。在主函数中定义学生成绩对象数组。用Sum()计算每个学生的总成绩、用Show()显示每个学生的成绩。增加静态成员函数getAvg(),用于返回学生的总平均分。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
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) {
/*补充代码*/
}
int Sum() {
/*补充代码*/
}
void Show() {
/*补充代码*/
}
double static getAve() {
/*补充代码*/
}
};
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;
/*补充代码*/ }
else if(op == 2) {
cin >> i;
/*补充代码*/ }
else if(op == 3) {
cin >> i;
/*补充代码*/ }
else {
/*补充代码*/ }
}
return 0;
}
输入格式:
包含一组测试数据。第一行输入一个整数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,全部输入都为整型数)
输出格式:
当op==2,3,4时,输出所求答案,每个答案占一行。
输入样例:
【样例输入】 【对应样例输出】
10
1 90 85 90
1 80 90 75
2 1 265
3 2 80 90 75
4 255.00
1 80 80 85
1 50 60 65
1 30 90 75
3 5 30 90 75
4 225.00
输出样例:
注意输入之间会有一些输出,但测试只看cout结果。
#include <iostream> #include <cstdio> #include <cstdlib> #include <iomanip> 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; TotalStudent++; TotalScore += Chinese + Math + English; } int Sum() { return Chinese + Math + English; } void Show() { cout << Chinese << " " << Math << " " << English << endl; } double static getAve() { return (double(TotalScore) /double( TotalStudent)); } }; int Score::TotalScore = 0; int Score::TotalStudent = 0; int main() { int n, op, i, c, m, e; cin >> n; int id = 1; int p = 1; Score sco[11]; while (n--) { cin >> op; if (op == 1) { cin >> c >> m >> e; sco[p++].setScore(c, m, e); } else if (op == 2) { cin >> i; cout << sco[i].Sum() << endl; } else if (op == 3) { cin >> i; sco[i].Show(); } else { cout << setiosflags(ios::fixed) << setprecision(2) << Score::getAve() << endl; } } return 0; }
要初始化静态成员,实型强制转换整型