6-1 2017final友元函数之全班同学的平均绩点
一个学生类,有三个私有成员:名字name、课程学分指针score、课程成绩指针grade。定义一个友元函数,求全班同学的平均绩点。单门课程的学分绩点=学分*绩点=学分*(成绩/10-5) ; 全班同学的平均绩点是 所有同学的全部课程的学分绩点之和/所有同学学分数之和。单个同学的课程数不超过100门。全班同学人数不超过100名。
输入说明:
输入若干行。
每行一个学生的信息:第一个输入是学生的名字,第二个输入是第一门课程的学分,第三个输入是第一门课程的成绩,第四个输入是第二门课程的学分,第五个输入是第二门课程的成绩,以此类推,最后以-1表示该行输入结束。每个学生的课程数不超过100门。
最后以 no 表示输入结束。
输出一行,即该全班同学的平均绩点。
函数接口定义:
这是求全部同学平均绩点的友元函数的声明: friend double averagegrade(student *stu, int count)
其中 *stu
和 count
都是用户传入的参数。 *stu
是传入的学生对象数组的首指针,count
是全班学生数量。
裁判测试程序样例:
#include<iostream> #include<string> using namespace std; class student{ private: double *grade; double *score; string name; public: student( ) { grade=NULL; score=NULL; } student(string n, double *g, double *s) { name=n; grade=g; score=s; } friend double averagegrade(student *stu, int count); }; /* 请在这里填写答案 */ int main() { student stu[100]; double s[100][100], g[100][100]; int count=0; string n; for(int i=0;i<100;i++) { cin>>n; if(n=="no") break; count++; for(int j=0;j<100;j++) { cin>>s[i][j]; if(s[i][j]==-1) break; cin>>g[i][j]; } stu[i]=student(n, g[i], s[i]); } cout<<averagegrade(stu, count); return 0; }
输入样例:
bob 3 90 2 68.5 2.5 50 -1
andy 3 80 2 77 -1
no
输出样例:
2.408
double averagegrade(student *stu, int count)
{
double sum1=0,sum2=0;
for(int i=0;i<count;i++)
{
for(int j=0;stu[i].score[j]!=-1;j++)
{
sum1+=stu[i].score[j]*(stu[i].grade[j]/10-5);
sum2+=stu[i].score[j];
}
}
if(sum1==0||sum2==0)
{
return 0;
}
else{
return sum1/sum2;
}
}
6-2 车与船的重量
定义一boat与car两个类,二者都有weight属性,定义二者的一个友元函数totalweight(),计算二者的重量和。
测试程序样例:
在这里给出函数被调用进行测试的例子。例如: #include <iostream> using namespace std; /* 请在这里填写答案 */ int main() { int c,b; cin>>c>>b; car c1(c); boat b1(b); cout<<totalweight(b1,c1)<<endl; }
输入样例:
1000 2000
输出样例:
3000
class car;
class boat
{
private:
int weight;
public:
friend int totalweight(boat &b,car &c);//用不用引用都行
boat (int c=0):weight(c){};
};
class car
{
private:
int weight;
public:
friend int totalweight (boat &b,car &c);
car (int b=0):weight(b){};
};
int totalweight(boat &b,car &c)
{
return b.weight+c.weight;
}
6-3 计算两点之间距离(友元函数)
现有一个类Point表示二维空间中的点,包含私有数据成员double x和double y,x表示该点的x坐标,y表示该点的y坐标。要求编写一个友元函数,输出两点之间的距离。
输入说明:
每一行四个数值,前两个数是第一个坐标点的x坐标和y坐标,后两个数是第二个坐标点的x坐标和y坐标。当输入四个0时表示输入结束。
输出说明:
输出两点距离必须保留两位小数。
函数接口定义:
输出两个点p1和p2之间的距离,要求输出结果必须保留两位小数。 void pointDis(Point& p1, Point& p2);
其中p1
和 p2
都是用户传入的参数。函数须计算p1和p2的距离并输出,要求输出结果必须保留两位小数。
裁判测试程序样例:
#include<iostream> #include<cmath> #include<iomanip> using namespace std; class Point { public: Point() { } Point(double px,double py) { x=px; y=py; } friend void pointDis(Point &p1,Point &p2); private: double x; double y; }; /* 请在这里填写答案 */ int main() { double x1,y1,x2,y2; cin>>x1>>y1>>x2>>y2; Point p1,p2; while(!(x1==0&&y1==0&&x2==0&&y2==0)) { p1=Point(x1,y1); p2=Point(x2,y2); pointDis(p1,p2); cin>>x1>>y1>>x2>>y2; } return 0; }
输入样例:
1.5 3.8 4.2 8.5
1 3 4 7
0 0 0 0
输出样例:
5.42
5.00
void pointDis(Point &p1,Point &p2)
{
cout<<fixed<<setprecision(2)<<sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y))<<endl;
}
6-4 复数的加减运算
###复数加减(友元的应用)
- 声明一个复数类CComplex(类私有数据成员为double型的real和image)
- 定义构造函数,用于指定复数的实部与虚部。
- 定义成员函数,调用该函数时,以格式real+image i的格式输出当前对象(当虚部为正数时,实部虚部中间用+号连接,当虚部为复数时,实部虚部用-号连接)。
- 编写加法友元函数,以复数对象c1, c2为参数,求两个复数对象之和(实部虚部分别相加)。
- 编写减法友元函数,以复数对象c1, c2为参数,求两个复数对象的差(实部虚部分别相减)
输入格式:
输入有两行:
第一行是复数c1的实部与虚部,以空格分隔;
第二行是复数c2的实部与虚部,以空格分隔。
输出格式:
输出共四行:
第一行是c1;
第二行是c2;
第三行是c1与c2之和;
第四行是c1与c2之差;
函数接口定义:
在这里描述类定义 class CComplex{...}
裁判测试程序样例:
int main() { double r1,r2,i1,i2; cin>>r1>>i1; cin>>r2>>i2; CComplex c1(r1,i1),c2(r2,i2),c3,c4; c1.display(); c2.display(); c3=add(c1,c2); c3.display(); c4= sub(c1,c2); c4.display(); } /* 请在这里填写答案 */
####输入样例:
10 20
30 40
输出样例:
10+20i
30+40i
40+60i
-20-20i
/*#include <iostream>
using namespace std;
class CComplex
{
private:
double real,image;
public:
CComplex (){};
CComplex (double a,double b):real(a),image(b){};
friend CComplex add(CComplex c1,CComplex c2);
friend CComplex sub(CComplex c1,CComplex c2);
void display()
{
if(image>0)
{
cout<<real<<"+"<<image<<"i"<<endl;
}
else if(image<0)
{
cout<<real<<image<<"i"<<endl;
}
else
{
cout<<real<<endl;
}
}
};*/
#include <iostream>
using namespace std;
class CComplex {
private:
double real; // 复数的实部
double imag; // 复数的虚部
public:
// 构造函数
CComplex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}
// 显示复数
void display() {
cout << real;
if (imag >= 0) cout << "+";
cout << imag << "i" << endl;
}
// 获取实部
double getReal() const {
return real;
}
// 获取虚部
double getImag() const {
return imag;
}
};
// 复数加法
CComplex add(const CComplex& c1, const CComplex& c2) {
return CComplex(c1.getReal() + c2.getReal(), c1.getImag() + c2.getImag());
}
// 复数减法
CComplex sub(const CComplex& c1, const CComplex& c2) {
return CComplex(c1.getReal() - c2.getReal(), c1.getImag() - c2.getImag());
}
6-5 猫和老鼠有多重
定义猫和老鼠:Cat与Mouse两个类,二者都有weight属性,定义二者的一个友元函数totalweight(),计算二者的重量和。
裁判测试程序样例:
#include <iostream>
using namespace std;
/* 请在这里填写答案 */
int main()
{
int w1,w2;
cin>>w1>>w2;
Cat tom(w1);
Mouse jerry(w2);
cout<<totalWeight(tom,jerry)<<endl;
}
输入样例:
100 200
输出样例:
300
class Mouse;
class Cat
{
public:
Cat(int x){weight=x;}
friend int totalWeight(Cat &a,Mouse &b);
private:
int weight;
};
class Mouse
{
friend class Cat;
public:
Mouse(int y){weight=y;}
friend int totalWeight(Cat &a,Mouse &b);
private:
int weight;
};
int totalWeight(Cat &a,Mouse &b)
{
return a.weight+b.weight;
}
6-6 计算捐款总量
这里需要设计一个捐款人类Donator及一个相关函数getMaxName( ),Donator类中包含捐款人的姓名及其捐款额,其部分代码如下:
class Donator{
private:
string name; //捐款人姓名
float money; //捐款金额,单位:元
public:
void setName(string _name);
void setMoney(float _money);
string getName(){return name;}
float getMoney(){return money;}
请根据题意将代码补充完整,以输出一批捐款人来到前后的捐款总金额,以及本批次捐款人中捐款最高者的姓名,题目保证捐款人数不少于1人。
裁判测试程序样例:
#include <iostream> using namespace std; class Donator{ private: string name; float money; //单位:元 public: void setName(string _name); void setMoney(float _money); string getName(){return name;} float getMoney(){return money;} /* 你编写的代码将被嵌入这里*/ //读取n个捐款人的姓名和捐款额 void read(Donator dt[],int n){ string name; float money; for(int i=0;i<n;i++){ cin>>name>>money; dt[i].setName(name); dt[i].setMoney(money); } } int main(){ int n; cin>>n; 输入本批次将参与的捐款人数 cin>>Donator::totalMoney; //输入目前已有的捐款总额 Donator::printTotal(); Donator d[n]; read(d,n); Donator::printTotal(); cout<<getMaxName(d,n)<<endl;//输出本批次中捐款最高者姓名 return 0; }
输入样例:
第一行为捐款人数及当前的捐款总额,第二行开始每行为一个捐款人的姓名和个人捐款金额。
3 28.5
Xiaoyu 12
Mike 81.5
Joey 50
输出样例:
输出本批次捐款人到达前后的捐款总额,及本批次中捐款最高者的姓名。
total:28.5
total:172
Mike
static float totalMoney;
static void printTotal()
{
cout<<"total"<<":"<<Donator::totalMoney<<endl;
}
};
float Donator:: totalMoney = 0.0;
void Donator :: setName(string _name)
{
name = _name;
}
void Donator :: setMoney(float _money)
{
totalMoney += _money;
money = _money;
}
string getMaxName(Donator d[], int n)
{
float maxmoney = 0;
int a=n;
string maxname;
for(int i = 0; i < a; i++)
{
if(d[i].getMoney() > maxmoney)
{
maxmoney = d[i].getMoney();
maxname = d[i].getName();
}
}
return maxname;
}
7-1 求两点之间距离
定义一个Point类,有两个数据成员:x和y, 分别代表x坐标和y坐标,并有若干成员函数。
定义一个函数Distance(), 用于求两点之间的距离。
输入格式:
输入有两行:
第一行是第一个点的x坐标和y坐标;
第二行是第二个点的x坐标和y坐标。
输出格式:
输出两个点之间的距离,保留两位小数。
输入样例:
0 9
3 -4
输出样例:
13.34
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
class Point{
double x,y;
public:
void setx(int xx){
x=xx;
}
void sety(int yy){
y=yy;
}
double getx(){
return x;
}
double gety(){
return y;
}
};
double Distance(double x1,double y1,double x2,double y2){
return sqrt(pow((x1-x2),2)+pow((y1-y2),2));
}
int main(){
Point p1,p2;
double x1,x2,y1,y2;
cin>>x1>>y1>>x2>>y2;
p1.setx(x1);
p1.setx(x2);
p2.sety(y1);
p2.sety(y2);
printf("%.2lf",Distance(x1,y1,x2,y2));
return 0;
}
7-2 友元函数的练习
定义Boat与Car两个类,两者都有私有的整型weight属性,定义两者的一个友元函数getTotalWeight(),计算二者的重量和。
参考主函数:
int main()
{
int n,m;
cin>>n>>m;
Boat boat(n);
Car car(m);
cout<<"船和汽车共重"<<getTotalWeight(boat,car)<<"吨"<<endl;
}
输入格式:
请在这里写输入格式。例如:输入在一行中给出2个整数m和n。
输出格式:
请在这里描述输出格式。例如:对每一组输入,在一行中输出:船和汽车共重M+n吨值。
输入样例:
在这里给出一组输入。例如:
40 30
输出样例:
在这里给出相应的输出。例如:
船和汽车共重70吨
#include<iostream>
using namespace std;
class Car;
class Boat
{
friend double getTotalWeight(Boat& b, Car& c);
private:
double weight;
public:
Boat(double weight)
{
this->weight = weight;
}
};
class Car
{
friend double getTotalWeight(Boat& b, Car& c);
private:
double weight;
public:
Car(double weight)
{
this->weight = weight;
}
};
double getTotalWeight(Boat& b, Car& c)
{
return b.weight + c.weight;
}
int main()
{
int n, m;
cin >> n >> m;
Boat boat(n);
Car car(m);
cout << "船和汽车共重" << getTotalWeight(boat, car) << "吨" << endl;
}
7-3 复数类的操作
1、声明一个复数类Complex(类私有数据成员为double型的real和image)
2、定义构造函数,用于指定复数的实部与虚部。
3、定义取反成员函数,调用时能返回该复数的相反数(实部、虚部分别是原数的相反数)。
4、定义成员函数Print(),调用该函数时,以格式(real, image)输出当前对象。
5、编写加法友元函数,以复数对象c1,c2为参数,求两个复数对象相加之和。
6、主程序实现:
(1)读入两个实数,用于初始化对象c1。
(2)读入两个实数,用于初始化对象c2。
(3)计算c1与c2相加结果,并输出。
(4)计算c2的相反数与c1相加结果,并输出。
输入格式:
输入有两行:
第一行是复数c1的实部与虚部,以空格分隔;
第二行是复数c2的实部与虚部,以空格分隔。
输出格式:
输出共三行:
第一行是c1与c2之和;
第二行是c2的相反数与c1之和;
第三行是c2 。
输入样例:
在这里给出一组输入。例如:
2.5 3.7
4.2 6.5
输出样例:
在这里给出相应的输出。例如:
(6.7, 10.2)
(-1.7, -2.8)
(4.2, 6.5)
#include<iostream>
using namespace std;
class Complex{
public:
double real,imag;
Complex(){
}
void Print(){
cout<<"("<<real<<", "<<imag<<")"<<endl;
}
void qufan(){
real = -real;
imag = -imag;
}
friend Complex operator+(Complex &,Complex &);
};
Complex operator+(Complex &op1,Complex &op2){
Complex x;
x.real = op1.real + op2.real;
x.imag = op1.imag + op2.imag;
return x;
}
int main(){
Complex c1,c2,c3;
double x,y;
cin>>c1.real>>c1.imag;
cin>>c2.real>>c2.imag;
c3 = c1 + c2;
c3.Print();
c2.qufan();
c3 = c1 + c2;
c3.Print();
c2.qufan();
c2.Print();
return 0;
}
7-4 实验5-3 计算点到直线的距离一一友元函数的应用
计算点到直线的距离。首先设计一个点类Point,它有2 个私有数据成员x和y,表示点的坐标。另一个类为直线类Line,它有3 个私有数据成员a、b和c,表示直线方程ax+by+c= 0。这两个类中都说明了一个友元函数dist,用于计算点到直线的距离。点(x,y)到直线ax+by+c=0的距离d的计算公式如下:
输入格式:
输入两行,第一行输入两个整数,表示点坐标x、y的值
在第二行中输入直线三个参数,表示直线方程的三个系数a、b和c。
输出格式:
计算点到直线的距离,保留两位小数。
输入样例:
在这里给出一组输入。例如:
5 5
2 4 3
输出样例:
在这里给出相应的输出。例如:
The distance is: 7.38
#include<bits/stdc++.h>
using namespace std;
class Line;
class Point{
double x,y;
public:
Point(double a=0,double b=0):x(a),y(b){}
friend double dist(Point&,Line&);
};
class Line{
double a,b,c;
public:
Line(double x=0,double y=0,double z=0):a(x),b(y),c(z){}
friend double dist(Point&,Line&);
};
double dist(Point &p,Line &l){
return abs((l.a*p.x+l.b*p.y+l.c)/sqrt(l.a*l.a+l.b*l.b));
}
int main(){
double x,y,a,b,c;
cin>>x>>y;
Point p(x,y);
cin>>a>>b>>c;
Line L(a,b,c);
cout<<"The distance is: "<<fixed<<setprecision(2)<<dist(p,L)<<endl;
return 0;
}