C++程序设计 求代码 设有一个类POINT的定义如下
1个回答
展开全部
代码有点繁琐了,自己优化去~~~
#include<iostream>
#include <sstream>
#include<string>
using namespace std;
enum Positions {
in = 1, on, out
};
class Point {
public:
Point() {
x = 0, y = 0;
}
Point(double x, double y) {
this->x = x;
this->y = y;
}
Point(Point &pt) {
x = pt.x;
y = pt.y;
}
double getx() {
return x;
}
double gety() {
return y;
}
double Area() {
return 0;
}
void show() {
cout << "x=" << x << " y=" << y << endl;
}
string get() {
stringstream ss;
string str;
string ret = "(";
ss << x;
ss >> str;
ret.append(str);
ret += ",";
ss << y;
ss >> str;
ret.append(str);
ret += ")";
return ret;
}
private:
double x, y;
};
class Rectangle: public Point {
public:
Rectangle() :
Point() {
a = 0;
b = 0;
}
Rectangle(double x, double y, double a, double b) :
Point(x, y) {
this->a = a;
this->b = b;
}
Rectangle(Point &pt, double a, double b) :
Point(pt) {
this->a = a;
this->b = b;
}
double Area() {
return a * b;
}
void show() {
cout << "x=" << getx() << " y=" << gety() << " a=" << a << " b=" << b
<< endl;
}
Positions position(Point &pt) {
if (pt.getx() < getx() || pt.gety() < gety()) {
return out;
}
if (((pt.getx() - getx()) > a) || ((pt.gety() - gety()) > b)) {
return out;
}
if ((pt.getx() - getx() == a) || (pt.gety() - gety() == b)
|| (pt.getx() - getx() == 0) || (pt.gety() - gety() == 0)) {
return on;
}
return in;
}
string get() {
stringstream ss;
string str;
string ret = "((";
ss << getx();
ss >> str;
ret.append(str);
ret += ",";
ss << gety();
ss >> str;
ret.append(str);
ret += "),";
ss << a;
ss >> str;
ret.append(str);
ret += ",";
ss << b;
ss >> str;
ret.append(str);
ret += ")";
return ret;
}
private:
double a, b;
}
;
class Circle: public Point {
public:
Circle() :
Point() {
r = 0;
}
Circle(double x, double y, double r) :
Point(x, y) {
this->r = r;
}
Circle(Point &pt, double r) :
Point(pt) {
this->r = r;
}
double Area() {
return 3.14 * r * r;
}
void show() {
cout << "x=" << getx() << " y=" << gety() << " r=" << r << endl;
}
Positions position(Point &pt) {
double tx = pt.getx() - getx();
double ty = pt.gety() - gety();
double tt = tx * tx + ty * ty;
if (tt > r * r) {
return out;
}
if (tt == r * r) {
return on;
}
return in;
}
string get() {
stringstream ss;
string str;
string ret = "((";
ss << getx();
ss >> str;
ret.append(str);
ret += ",";
ss << gety();
ss >> str;
ret.append(str);
ret += "),";
ss << r;
ss >> str;
ret.append(str);
ret += ")";
return ret;
}
private:
double r;
};
string getPosition(Positions p) {
switch (p) {
case in:
return "in";
case on:
return "on";
case out:
return "out";
}
}
int main() {
Point p(5, 5);
p.show();
Rectangle rt(p, 2, 8);
rt.show();
Circle c(p, 5);
c.show();
Point r(5, 10);
cout << r.get() << " is " << getPosition(rt.position(r)) << rt.get()
<< endl;
cout << r.get() << " is " << getPosition(c.position(r)) << c.get() << endl;
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询