1.9
题目内容:
先定义一个能描述平面上一条线段的类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 #include<iostream> 2 #include<cmath> 3 #include<iomanip> 4 using namespace std; 5 class Beeline 6 { 7 double x1,y1,x2,y2; 8 public: 9 Beeline(double m1=0,double n1=0,double m2=0,double n2=0) 10 { 11 x1=m1; 12 y1=n1; 13 x2=m2; 14 y2=n2; 15 } 16 double Length(); 17 double show(); 18 }; 19 double Beeline::Length() 20 { 21 return (double)sqrt(pow(x1-x2,2)+pow(y1-y2,2)); 22 } 23 double Beeline::show() 24 { 25 cout<<"("<<x1<<", "<<y1<<"),("<<x2<<", "<<y2<<")"<<endl; 26 } 27 class Triangle 28 { 29 Beeline l1,l2,l3; 30 public: 31 Triangle(double m1,double n1,double m2,double n2,double m3,double n3 ): l1(m1,n1,m2,n2),l2(m2,n2,m3,n3),l3(m3,n3,m1,n1) 32 { 33 } 34 double area() 35 { 36 double p; 37 p=(l1.Length()+l2.Length()+l3.Length())*0.5; 38 return sqrt(p*(p-l1.Length())*(p-l2.Length())*(p-l3.Length())); 39 } 40 void print() 41 { 42 l1.show(); 43 l2.show(); 44 l3.show(); 45 } 46 }; 47 int main() 48 { 49 double a,b,c,d,e,f; 50 cin>>a>>b>>c>>d>>e>>f; 51 Triangle spark(a,b,c,d,e,f); 52 cout<<"Three edges' points are listed as follows:"<<endl; 53 spark.print(); 54 cout << fixed << setprecision(2) << "The area of this triangle is: " << spark.area() << "."<<endl; 55 56 }
要点:int ,double类的选用和转换,输出结果保留两位数,派生类的构造函数的书写
题目内容:
定义如下形式的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 #include<iostream> 2 #include<cmath> 3 using namespace std; 4 class point { 5 6 double x,y; 7 8 public: 9 10 point (double x0=0, double y0=0) {x=x0; y=y0;} 11 12 void display(); 13 friend double dis(point &k,point &b) 14 { 15 return sqrt(pow(k.x-b.x,2)+pow(k.y-b.y,2)); 16 } 17 }; 18 void point::display() 19 { 20 cout<<"("<<x<<","<<y<<")"<<endl; 21 } 22 int main() 23 { 24 double a,b,c,d; 25 cin>>a>>b>>c>>d; 26 point m(a,b),n(c,d); 27 m.display(); 28 n.display(); 29 cout<<dis(m,n); 30 }
要点:友元函数的使用
题目内容:
根据下面部分代码,补充完成学生成绩类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
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <iomanip> 5 using namespace std; 6 class Score { 7 private: 8 int Chinese, Math, English; 9 static double TotalScore; 10 static int TotalStudent; 11 public: 12 Score() {} 13 void setScore (int c, int m, int e) { 14 Chinese=c; 15 Math=m; 16 English=e; 17 TotalStudent++; 18 TotalScore=TotalScore+Chinese+Math+English; 19 } 20 int Sum() { 21 return Chinese+Math+English; 22 } 23 void Show() { 24 cout<<Chinese<<" "<<Math<<" "<<English<<endl; 25 } 26 double static getAve() { 27 return TotalScore/TotalStudent; 28 } 29 }; 30 double Score::TotalScore=0; 31 int Score::TotalStudent=0; 32 int main() { 33 int n, op, i, c, m, e; 34 cin >> n; 35 int id = 1; 36 Score sco[11]; 37 while(n--) 38 { 39 cin >> op; 40 if(op == 1) { 41 cin >> c >> m >> e; 42 sco[id++].setScore(c,m,e); } 43 else if(op == 2) { 44 cin >> i; 45 cout<<sco[i].Sum()<<endl; } 46 else if(op == 3) { 47 cin >> i; 48 sco[i].Show(); } 49 else { 50 cout<< fixed << setprecision(2)<<Score::getAve()<<endl; } 51 } 52 return 0; 53 }

浙公网安备 33010602011771号