Advertisement
Guest User

assignment1 last ver

a guest
Nov 27th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.40 KB | None | 0 0
  1. //-----------------------------  myPoint.h  -----------------------------//
  2.  
  3. #include <iostream>
  4.  
  5. #ifndef myPoint_h
  6. #define myPoint_h
  7.  
  8. class Point
  9. {
  10.  
  11. friend void showPoint(Point);                   // friend function that prints one point
  12.  
  13. public:                                                 // class methods
  14.         //Point();                                      // default constructor
  15.         Point(int =0,int =0);                           // class constructor
  16.         Point(const Point &);                           // copy constructor
  17.         void setX(int);                                 // set the x value of the point
  18.         void setY(int);                                 // set the y value of the point
  19.         int getX() const;                               // get the x value of the point
  20.         int getY() const;                               // get the y value of the point
  21.         bool checkEqualy(Point) const;     //-- do i need it?    // return answer if 2 points are equal
  22.         const Point &operator=(const Point &);          // assign points
  23.         bool operator==(const Point &) const;           // compare equal
  24.         bool operator!=(const Point &) const;           // compare not equal
  25.  
  26. private:                // class members
  27.         int x;          // x coordinate
  28.         int y;          // y coordinate
  29. };
  30.  
  31. #endif
  32.  
  33. //-----------------------------  myPoint.h  -----------------------------//
  34.  
  35.  
  36.  
  37.  
  38. //-----------------------------  myLine.h  -----------------------------//
  39.  
  40. #include <iostream>
  41. #include <math.h>
  42. #include "myPoint.h"
  43.  
  44. #ifndef myLine_h
  45. #define myLine_h
  46.  
  47. class Line
  48. {
  49.  
  50. friend void ShowLine(Line) ;                             // presents line details
  51.  
  52. public:                                                 // class methods:
  53.         Line ();                                        // default constructor
  54.         Line(Point, Point);                             // class constructor
  55.         Line(const Line &);                             // copy constructor
  56.         void SetStart(Point);                           // set the start point of the line
  57.         void SetEnd(Point);                             // set the end point of the line
  58.         Point GetStart() const;                         // get the start point of the line
  59.         Point GetEnd() const;                           // get the end point of the line
  60.         double GetLength() const;                       // get the length of the line
  61.         double GetSlope() const;                        // get the slope of the line
  62.         double CalcLength() const;                      // calculate the length of the line
  63.         double CalcSlope() const;                       // calculate the slope of the line
  64.         double LineAddition(Line,Line) const;           // calculate the length of line1+line2
  65.         double LineSubtraction(Line,Line) const;        // calculate the length of line1-line2
  66.         void LineAdd(Line);                             // add the other line to current line (this->line +=new line)
  67.         void LineSub(Line);                             // subtract the other line to current line (this->line -=new line)
  68.         const Line &operator=(const Line &);            // assign lines    
  69.  
  70. private:
  71.                                 // class members
  72.         Point Start;            // start point of the line
  73.         Point End;              // end point of the line
  74.         double length;          // the length of the line
  75.         double Slope;           // the slope of the line
  76. };
  77.  
  78. #endif
  79.  
  80. //-----------------------------  myLine.h  -----------------------------//
  81.  
  82.  
  83.  
  84.  
  85. /*
  86.  *      Author      : Yossi Yadgar
  87.  *      ID number   : 200973683
  88.  */
  89.  
  90. #include "myPoint.h"
  91. #include "myLine.h"
  92. #include <iostream>
  93.  
  94. int main ()
  95. {
  96.         return 0;
  97. }
  98.  
  99.  
  100.  
  101.  
  102. //-----------------------------  myLine.cpp  -----------------------------//
  103.  
  104. #include "myLine.h"
  105. using namespace std;
  106.  
  107. void ShowLine(Line l1)
  108. {
  109.         cout << "The line details:" << endl;
  110.         cout << "start point :" << endl;
  111.         showPoint(l1.GetStart());
  112.         cout << "end point :" << endl;
  113.         showPoint(l1.GetEnd());
  114.         cout << "The point slope: " << l1.GetSlope() << endl;
  115.         cout << "The point length " << l1.GetLength() << endl;
  116. }
  117.  
  118. Line :: Line (){
  119.     this->Start.setX(0);
  120.     this->Start.setY(0);
  121.     this->End.setX(0);
  122.     this->End.setY(0);
  123.     this->length = 0;
  124.     this->Slope = 0;
  125. }
  126.  
  127. Line :: Line (const Line &l1)
  128. {
  129.     this->Start = l1.Start;
  130.     this->End = l1.End;
  131.     this->length = l1.length;
  132.     this->Slope = l1.Slope;
  133. }
  134.  
  135. Line :: Line(Point p1, Point p2)
  136. {
  137.         this->Start=p1;
  138.         this->End=p2;
  139.         this->Slope = CalcSlope();
  140.         this->length= CalcLength();
  141. }
  142.  
  143.  
  144. void Line :: SetStart(Point p)
  145. {
  146.         this->Start.setX(p.getX());
  147.         this->Start.setY(p.getY());
  148. }
  149.  
  150.  
  151. void Line :: SetEnd(Point p)
  152. {
  153.         this->End.setX(p.getX());
  154.         this->End.setY(p.getY());
  155. }
  156.  
  157.  
  158. Point Line :: GetStart() const
  159. {
  160.         return this->Start;
  161. }
  162.  
  163.  
  164. Point Line :: GetEnd() const
  165. {
  166.         return this->End;
  167. }
  168.  
  169.  
  170. double Line :: GetLength() const
  171. {
  172.         return this->length;
  173. }
  174.  
  175.  
  176. double Line :: GetSlope() const
  177. {
  178.         return this->Slope;
  179. }
  180.  
  181.  
  182. double Line :: CalcLength() const
  183. {
  184. int x1=this->Start.getX();
  185. int x2=this->End.getX();
  186. int y1=this->Start.getY();
  187. int y2=this->End.getY();
  188.  
  189.         return  sqrt(((x1-x2)*(x1-x2))+ ((y1-y2)*(y1-y2)));
  190. }
  191.  
  192.  
  193. double Line :: CalcSlope() const
  194. {
  195. int x1=this->Start.getX();
  196. int x2=this->End.getX();
  197. int y1=this->Start.getY();
  198. int y2=this->End.getY();
  199.  
  200.         return ((y2-y1)/(x2-x1));
  201.  
  202. }
  203.  
  204.  
  205. double Line :: LineAddition(Line l1,Line l2) const
  206. {
  207.         return ( l1.GetLength() + l2.GetLength() );
  208. }
  209.  
  210.  
  211. double Line :: LineSubtraction(Line l1,Line l2) const
  212. {
  213.         return ( l1.GetLength() - l2.GetLength() );
  214. }
  215.  
  216.  
  217. void Line ::LineAdd(Line l1)
  218. {
  219.         if  (this->Slope == l1.GetSlope() )
  220.                 {
  221.                 // if current before l1
  222.                 if (this->GetEnd().checkEqualy(l1.GetStart()))
  223.                         this->SetEnd(l1.GetEnd());
  224.                 // if l1 before current
  225.                 if (this->GetStart().checkEqualy(l1.GetEnd()))
  226.                         this->SetStart(l1.GetStart());
  227.                 }
  228.         // else the other line will make from the current line
  229.         // 2 lines, so we don't handle with this case
  230.  
  231. }
  232.  
  233.  
  234. void Line :: LineSub(Line l2)
  235. {
  236.         if ( (this->GetLength() >= l2.GetLength()) && (this->GetSlope() == l2.GetSlope()) )
  237.         {
  238.                 // if they have the same start
  239.                 if (this->GetStart().checkEqualy(l2.GetStart()))
  240.                 {
  241.                         this->SetStart(l2.GetEnd());
  242.                         return;
  243.                 }
  244.                 // if they have the same end
  245.                 if (this->GetEnd().checkEqualy(l2.GetEnd()))
  246. {
  247.                         this->SetEnd(l2.GetStart());
  248.                         return;
  249.                 }
  250.                 // else the other line will make from the current line
  251.                 // 2 lines, so we don't handle with this case
  252.         }
  253. }
  254.  
  255. const Line & Line :: operator=(const Line &l1){
  256.     this->Start = l1.Start;
  257.     this->End = l1.End;
  258.     this->length = l1.length;
  259.     this->Slope = l1.Slope;
  260.     return *this;
  261. }
  262.  
  263.  
  264.  
  265.  
  266.  
  267. //-----------------------------  myPoint.cpp  -----------------------------//
  268.  
  269. #include "myPoint.h"
  270. using namespace std;
  271.  /*
  272. Point::Point(void)
  273. {
  274.         x=y=0;
  275. }
  276.  */
  277. Point::Point(int x, int y)
  278. {
  279.        setX(x);
  280.        setY(y);
  281. }
  282.  
  283. Point :: Point(const Point &p)
  284. {
  285.     this->setX(p.getX());
  286.     this->setY(p.getY());
  287. }
  288.  
  289. void Point::setX(int x)
  290. {
  291.         this->x=x;
  292. }
  293.  
  294. void Point::setY(int y)
  295. {
  296.         this->y=y;
  297. }
  298.  
  299. int Point::getX() const
  300. {
  301.         return this->x;
  302. }
  303.  
  304. int Point::getY() const
  305. {
  306.         return this->y;
  307. }
  308.  
  309. bool Point ::checkEqualy(Point p1) const
  310. {
  311.         if ( (this->getX() == p1.getX()) && (this->getY() == p1.getY()) )
  312.                 return true;
  313.         return false;
  314. }
  315.  
  316. void ShowPoint (Point p1)
  317. {
  318.         cout << "The point coordinates are: " << endl;
  319.         cout << "x= " << p1.getX() <<" y= "<< p1.getY() << endl;
  320. }
  321.  
  322. const Point & Point ::operator=(const Point &p){
  323.     if (&p !=this)
  324.     {
  325.         this->setX(p.getX());          
  326.         this->setY(p.getY());
  327.     }
  328.     return *this;
  329. }
  330.  
  331. bool Point :: operator==(const Point &p) const {
  332.         if ( (this->getX() == p.getX()) && (this->getY() == p.getY()) )
  333.                 return true;
  334.         return false;
  335. }
  336.  
  337. bool Point :: operator!=(const Point &p) const {
  338.     return !(*this==p);
  339. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement