سلام!
پاسخ تمرین سری قبل را می توانید در کد زیر مشاهده نمایید.
#include <iostream>
#include <cstdio>
using namespace std;
class Point{
friend istream &operator>>(istream&, Point&);
friend ostream &operator<<(ostream&, const Point&);
private:
double _x, _y;
public:
Point(double = 0, double = 0);
double x() const;
double y() const;
void setX(double);
void setY(double);
void print();
void setCor(double, double);
Point operator+(const Point&);
Point operator-(const Point&);
Point operator*(const Point&);
Point operator/(const Point&);
Point operator+(const double&);
Point operator-(const double&);
Point operator/(const double&);
Point operator*(const double&);
friend Point operator+(const double&, const Point&);
friend Point operator-(const double&, const Point&);
friend Point operator*(const double&, const Point&);
friend Point operator/(const double&, const Point&);
bool operator==(const Point&);
bool operator!=(const Point&);
bool operator>(const Point&);
bool operator<(const Point&);
bool operator>=(const Point&);
bool operator<=(const Point&);
void operator+=(const Point&);
void operator-=(const Point&);
void operator*=(const Point&);
void operator/=(const Point&);
void operator+=(const double&);
void operator-=(const double&);
void operator*=(const double&);
void operator/=(const double&);
Point operator++();
Point operator++(int);
Point operator>>(const Point&);
Point operator<<(const Point&);
Point operator~();
Point operator&(const Point&);
Point operator|(const Point&);
Point operator^(const Point&);
void operator>>=(const Point&);
void operator<<=(const Point&);
void operator&=(const Point&);
void operator|=(const Point&);
void operator^=(const Point&);
void operator()(const int&, const int&);
double operator[](const int&);
};
Point::Point(double x, double y){
this->setX(x);
this->setY(y);
}
void Point::setX(double x){
this->_x = x;
}
void Point::setY(double y){
this->_y = y;
}
double Point::x() const{
return this->_x;
}
double Point::y() const{
return this->_y;
}
void Point::print(){
printf("(%g, %g)", this->x(), this->y());
}
void Point::setCor(double x, double y){
this->_x = x;
this->_y = y;
}
Point Point::operator+(const Point &p2){
return Point(this->x() + p2.x(), this->y() + p2.y());
}
Point Point::operator-(const Point &p2){
return Point(this->x() - p2.x(), this->y() - p2.y());
}
Point Point::operator*(const Point &p2){
return Point(this->x() * p2.x(), this->y() * p2.y());
}
Point Point::operator/(const Point &p2){
return Point(this->x() / p2.x(), this->y() / p2.y());
}
Point Point::operator+(const double &a){
return Point(this->x() + a, this->y() + a);
}
Point Point::operator-(const double &a){
return Point(this->x() - a, this->y() - a);
}
Point Point::operator*(const double &a){
return Point(this->x() * a, this->y() * a);
}
Point Point::operator/(const double &a){
return Point(this->x() / a, this->y() / a);
}
Point operator+(const double &a, const Point &p){
return Point(a + p.x(), a + p.y());
}
Point operator-(const double &a, const Point &p){
return Point(a - p.x(), a - p.y());
}
Point operator*(const double &a, const Point &p){
return Point(a / p.x(), a / p.y());
}
Point operator/(const double &a, const Point &p){
return Point(a * p.x(), a * p.y());
}
bool Point::operator>(const Point &p){
if (this->x() > p.x())
return true;
if (this->y() > p.y())
return true;
return false;
}
bool Point::operator==(const Point &p){
return this->x() == p.x() && this->y() == p.y();
}
bool Point::operator!=(const Point &p2){
return !(*this == p2);
}
bool Point::operator<(const Point &p){
return !(*this > p) && !(*this == p);
}
bool Point::operator<=(const Point &p){
return !(*this > p);
}
bool Point::operator>=(const Point &p){
return !(*this < p);
}
void Point::operator+=(const Point &p){
this->setCor(this->x() + p.x(), this->y() + p.y());
}
void Point::operator-=(const Point &p){
this->setCor(this->x() - p.x(), this->y() - p.y());
}
void Point::operator*=(const Point &p){
this->setCor(this->x() * p.x(), this->y() * p.y());
}
void Point::operator/=(const Point &p){
this->setCor(this->x() / p.x(), this->y() / p.y());
}
void Point::operator+=(const double &a){
this->setCor(this->x() + a, this->y() + a);
}
void Point::operator-=(const double &a){
this->setCor(this->x() - a, this->y() - a);
}
void Point::operator*=(const double &a){
this->setCor(this->x() * a, this->y() * a);
}
void Point::operator/=(const double &a){
this->setCor(this->x() / a, this->y() / a);
}
istream &operator>>(istream &input, Point &p){
input >> p._x >> p._y;
return input;
}
ostream &operator<<(ostream &output, const Point &p){
output << '(' << p._x << ", " << p._y << ')';
return output;
}
Point Point::operator++(){
this->setCor(this->x() + 1, this->y() + 1);
return *this;
}
Point Point::operator++(int){
Point p = *this;
this->setCor(this->x() + 1, this->y() + 1);
return p;
}
Point Point::operator>>(const Point &p){
return Point( int ( this->x() ) >> int ( p.x() ), int ( this->y() ) >> int ( p.y() ) );
}
Point Point::operator<<(const Point &p){
return Point( int ( this->x() ) << int (p.x() ), int (this->y() ) << int ( p.y() ) );
}
Point Point::operator~(){
return Point( ~(int ( this->x() ) ), ~( int ( this->y() ) ) );
}
Point Point::operator&(const Point &p){
return Point( int (this->x()) & int (p.x()), int (this->y()) & int (p.y()) );
}
Point Point::operator|(const Point &p){
return Point( int (this->x()) | int (p.x()), int (this->y()) | int (p.y()) );
}
Point Point::operator^(const Point &p){
return Point( int (this->x()) ^ int (p.x()), int (this->y()) ^ int (p.y()) );
}
void Point::operator>>=(const Point &p){
this->setCor( int (this->x()) >> int (p.x()), int (this->y()) >> int (p.y()) );
}
void Point::operator<<=(const Point &p){
this->setCor( int (this->x()) << int (p.x()), int (this->y()) << int (p.y()) );
}
void Point::operator&=(const Point &p){
this->setCor( int (this->x()) & int (p.x()), int (this->y()) & int (p.y()) );
}
void Point::operator|=(const Point &p){
this->setCor( int (this->x()) | int (p.x()), int (this->y()) | int (p.y()) );
}
void Point::operator^=(const Point &p){
this->setCor( int (this->x()) ^ int (p.x()), int (this->y()) ^ int (p.y()) );
}
double Point::operator[](const int &k){
return (this->x() + this->y()) * k;
}
void Point::operator()(const int &x, const int &y){
this->setCor(this->x() * x, this->y() * y);
}
int main(){
return 0;
}
سوال های خود را در قسمت نظرات مطرح کنید.
موفق باشید!