编译器:C++ (g++)
在一个平面游戏里,当一个大泡泡与一个小泡泡相遇时,会合并成一个更大的泡泡。新泡泡的圆心为两个泡泡的圆心的中点,新泡泡的面积为两个泡泡的面积之和。
请在下述代码中补充Bubble类的设计,使得下述代码可以正常运行。显然,你需要一个自定义oeprator+操作符函数。
裁判测试程序样例:
输入样例:
100.0 200.0 10.1
70.0 120.0 20.2
输出样例:
New Bubble: [(85.00, 160.00), 22.58]
请注意:函数题只需要提交相关代码片段,不要提交完整程序。
//Project - EatBubble
#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;
//在此处定义Bubble类
int main(){
double x=0, y=0, r=0;
cin >> x >> y >> r;
auto a = Bubble(x,y,r); //参数依次是圆心的x坐标,y坐标,圆半径
cin >> x >> y >> r;
auto b = Bubble(x,y,r);
auto c = a + b;
printf("New Bubble: [(%.2f, %.2f), %.2f]",c.x,c.y,c.r);
return 0;
}
Ans:
class Bubble {
public:
double x;
double y;
double r;
Bubble(double x, double y, double r) :x(x),y(y),r(r){}
friend const Bubble operator+(const Bubble& a, const Bubble& b);
};
const Bubble operator+(const Bubble& a, const Bubble& b) {
Bubble temp(0, 0, 0);
temp.x = (a.x + b.x) / 2;
temp.y = (a.y + b.y) / 2;
temp.r = sqrt((a.r * a.r) + (b.r * b.r));
return temp;
}