以下为个人的分文件编写:
▪Code:
Circle.h
#pragma once #include <iostream> #include "Point.h" using namespace std; class Circle { public: Point get_center(); void set_center(Point center1); double get_r(); void set_r(double& r1); private: Point center; double r; };
Point.h
#pragma once #include <iostream> using namespace std; class Point { public: double getx(); void setx(double& x1); double gety(); void sety(double& y1); private: double x; double y; };
源.cpp
#include<iostream> #include"Circle.h" #include"Point.h" #include<math.h> using namespace std; int get_distance(Point& p1, Point& p2); void if_point_in_circle(double distance,double r); int main() { double x, y, x1, y1, r; Point p1,center; Circle c1; cout<<"Please enter the Point's x and y coordinates." << endl; cin >> x >> y; p1.setx(x); p1.sety(y); cout << "Please enter the Circle's center and radius." << endl; cin >> x1 >> y1 >> r; center.setx(x1); center.sety(y1); c1.set_center(center); double distance = get_distance(center, p1); cout << distance << endl; if_point_in_circle(distance,r); } int get_distance(Point& p1,Point& p2) { int x1 = p1.getx(); int y1 = p1.gety(); int x2 = p2.getx(); int y2 = p2.gety(); double distance = sqrt(pow(x1-x2,2)+pow(y1-y2,2)); //包含头文件math.h,不懂可以百度 return distance; } void if_point_in_circle(double distance,double r) { if (distance > r) { cout << "Point is out of the Circle." << endl; } else if (distance == r) { cout << "Point is on the Circle." << endl; } else { cout << "Point is in the Circle" << endl; } }
Circle.cpp
#include "Circle.h" Point Circle::get_center() { return center; }; void Circle::set_center(Point center1) { center = center1; }; double Circle::get_r() { return r; }; void Circle::set_r(double& r1) { r = r1; };
Point.cpp
#include "Point.h" double Point::getx() { return x; } void Point::setx(double& x1) { x = x1; } double Point::gety() { return y; } void Point::sety(double& y1) { y = y1; }