第一题
1.1题目描述
【问题描述】定义如下形式的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
1.2源代码
#include<iostream>
#include<cmath>
using namespace std;
class point
{
private:
double x,y;
public:
point (double x0=0, double y0=0) {x=x0; y=y0;}
void display();
friend void length(point a1,point a2)
{
double n;
n=sqrt((a1.x-a2.x)*(a1.x-a2.x)+(a1.y-a2.y)*(a1.y-a2.y));
cout<<n;
}
};
void point::display()
{
cout<<"("<<x<<","<<y<<")"<<endl;
}
int main()
{
double x1,x2,y1,y2;
cin>>x1>>y1>>x2>>y2;
point a1(x1,y1),a2(x2,y2);
a1.display();
a2.display();
length(a1,a2);
return 0;
}
1.3样例输出
第二题
2.1题目描述
学生成绩类
【问题描述】根据下面部分代码,补充完成学生成绩类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结果。
2.2源代码
#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()
{
int sum=Chinese+Math+English;
return sum;
}
void Show()
{
cout<<Chinese<<" "<<Math<<" "<<English<<endl;
}
double static getAve()
{
return (TotalScore*1.0/TotalStudent);
}
};
int Score::TotalStudent=0;
int Score::TotalScore=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);
}
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;
}
2.3输出样例
把忍受变成享受,是精神对于物质的最大胜利,灵魂可以自主,也可以自欺。
------钱钟书