首页 > 代码库 > 13周 项目2 圆的比较
13周 项目2 圆的比较
#include <iostream> #include <cmath> using namespace std; class Point { public: Point(double a,double b):x(a),y(b) {} double getx() { return x; } double gety() { return y; } friend ostream&operator << (ostream&,Point&); protected: double x,y; }; ostream&operator << (ostream &output,Point &p) { output<<"("<<p.getx()<<","<<p.gety()<<")"<<endl; return output; } class Circle:public Point { public: Circle(double a,double b,double c):Point(a,b),r(c) {} friend ostream&operator << (ostream&,Circle&); bool operator > (Circle &c); bool operator < (Circle &c); bool operator >= (Circle &c); bool operator <= (Circle &c); bool operator == (Circle &c); bool operator != (Circle &c); protected: double r; }; ostream&operator << (ostream &output,Circle &c) { output<<"("<<c.getx()<<","<<c.gety()<<")"<<" "; output<<"半径:"<<c.r<<endl; return output; } bool Circle::operator < (Circle &c) { if(r<c.r) return true; else return false; } bool Circle::operator > (Circle &c) { if(r>c.r) return true; else return false; } bool Circle::operator >= (Circle &c) { if(*this<c)return false; return true; } bool Circle::operator <= (Circle &c) { if(*this>c)return false; return true; } bool Circle::operator == (Circle &c) { if(*this>=c&&*this<=c)return true; return false; } bool Circle::operator != (Circle &c) { if(*this==c)return false; return true; } int main( ) { Circle c1(3,2,4),c2(4,5,5); cout<<"c1:"<<c1<<endl; cout<<"c2:"<<c2<<endl; if (c1 > c2) cout << "c1>c2" << endl; if (c1 < c2) cout << "c1<c2" << endl; if (c1 == c2) cout << "c1=c2" << endl; if (c1 != c2) cout << "c1≠c2" << endl; if (c1>= c2) cout << "c1≥c2" << endl; if (c1 <= c2) cout << "c1≤c2" << endl; return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。