题目描述
定义如下形式的point 类,其对象表示平面上的一个点(x,y),设计一个友元函数dis()求出两个对象(平面点)间的距离。并编制主函数,通过类对象验证相关函数的正确性。
class point {
double x,y;
public:
point (double x0=0, double y0=0) {x=x0; y=y0;}
void display();
};
运用两点间的距离公式,开根号函数为sqrt()。四个实数,前两个实数是一个点的坐标,后两个实数是另一个点的坐标。坐标中前一个数是横坐标,后一个数是纵坐标。
输入样例:
1.2 -3.5 -1.5 6输出三行数据,第一行是第一个点坐标,第二行是第二个点坐标,坐标输出形式为(x,y),第三行是一个实数,代表两个点之间的距离。
输出样例:
(1.2,-3.5)
(-1.5,6)
9.87623
#include
#include
#include
using namespace std;
class point{
double x,y;
public:
point(double x0=0, double y0=0) {x=x0; y=y0;}
void display();
friend double dis(point& a,point& b);
};
void point::display(){
cout<<'('<<x<<','<<y<<')'<<endl;
}
double dis(point& a,point& b){
cout<<sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int main(){
double x1,x2,x3,x4;
cin>>x1>>x2>>x3>>x4;
point a(x1,x2),b(x3,x4);
a.display();
b.display();
dis(a,b);
system("pause");
}
题目描述
根据下面部分代码,补充完成学生成绩类Score。在主函数中定义学生成绩对象数组。用Sum()计算每个学生的总成绩、用Show()显示每个学生的成绩。增加静态成员函数getAvg(),用于返回学生的总平均分。
include
include
include
include
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
当op==2,3,4时,输出所求答案,每个答案占一行。
输出样例:
注意输入之间会有一些输出,但测试只看cout结果。
#include
#include
#include
#include
using namespace std;
class Score {
private:
int Chinese, Math, English;
static double TotalScore;
static int TotalStudent;
public:
Score() {}
void setScore (int c, int m, int e) {
Chinese=c;
Math=m;
English=e;
TotalScore=TotalScore+c+m+e;
TotalStudent++;
}
int Sum() {
return Chinese+Math+English;
}
void Show() {
cout<<Chinese<<" "<<Math<<" "<<English<<endl;
}
double static getAve() {
return TotalScore/TotalStudent;
}
};
double 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()<<endl;
}
else if(op == 3) {
cin >> i;
sco[i].Show();
}
else {
cout<<fixed<<setprecision(2)<<Score::getAve()<<endl;
}
}
system("pause");
}
Original: https://www.cnblogs.com/haorical/p/16195992.html
Author: haorical
Title: MOOC高级语言程序设计第九章课后作业
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/620864/
转载文章受原作者版权保护。转载请注明原作者出处!