Advertisement
rowers

pomoc1

Nov 27th, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.56 KB | None | 0 0
  1. //punkt.h
  2.  
  3. #include <iostream>
  4. #include <math.h>
  5.  
  6. using namespace std;
  7.  
  8.  class Punkt
  9. {
  10.         friend class Zbiór;
  11.         friend ostream &operator <<(ostream &wyjscie, const Punkt &o_);
  12.         float x, y;
  13.  
  14. public:
  15.         Punkt(float x_ = 0,  float y_ = 0)
  16.         {
  17.                 x = x_;
  18.                 y = y_;
  19.         }
  20.         double odl(Punkt &p);
  21.  
  22.         void dodaj(Punkt &p){
  23.                 x += p.x;
  24.                 y += p.y;
  25.        
  26.         }
  27.  
  28.  
  29.         Punkt operator +(const Punkt &o_)
  30. {
  31.     Punkt p;
  32.     p.x = this->x + o_.x;
  33.     p.y = this->y + o_.y;
  34.     return p;
  35. }
  36.  
  37. Punkt operator =(const Punkt &o_)
  38. {
  39.     this->x = o_.x;
  40.     this->y = o_.y;
  41. }
  42.  
  43.         ~Punkt()
  44.         {
  45.                 cout<<"Destruktor"<<endl;
  46.         }
  47.  
  48. };
  49.  
  50. class Zbiór
  51. {
  52.         Punkt *p;
  53.         int ile;
  54.  
  55. public:
  56.  
  57. Zbiór(float *t_x, float *t_y, int roz);
  58. Zbiór(Zbiór &k);
  59. Zbiór(int roz);
  60. void Max(int &ktora, float &max);
  61.  
  62. Zbiór &operator [](int i);
  63.  
  64.  
  65.        
  66.  
  67. ~Zbiór();
  68.  
  69. };
  70.  
  71. //punkt.cpp
  72.  
  73. #include "punkt.h"
  74. #include <math.h>
  75.  
  76. using namespace std;
  77.  
  78. double Punkt::odl(Punkt &p){
  79.         double odl;
  80.         odl = sqrt((p.x - x) * (p.x - x) + (p.y - y) * (p.y - y));
  81.         return odl;
  82. }
  83.  
  84.         Zbiór::Zbiór(float *t_x, float *t_y, int roz)
  85.         {
  86.                 ile = roz;
  87.                 p = new Punkt[ile];
  88.                 for(int i=0; i<ile; i++)
  89.                 {
  90.                         p[i].x = t_x[i];
  91.                         p[i].y = t_y[i];
  92.                 }
  93.  
  94.         }
  95.  
  96.         Zbiór::Zbiór(Zbiór &nowy)
  97.         {
  98.                 ile = nowy.ile;
  99.                 p = new Punkt[ile];
  100.                 for (int i=0; i<ile; i++)
  101.                 {
  102.                         p[i].x = nowy.p[i].x;
  103.                         p[i].y = nowy.p[i].y;
  104.                 }
  105.         }
  106.  
  107.         Zbiór::Zbiór(int roz):ile(roz)
  108.         {
  109.                         p = new Punkt[ile];
  110.  
  111.         }
  112.  
  113.         Zbiór::~Zbiór()
  114. {
  115.  
  116. delete []p;
  117.  
  118. }
  119.  
  120. Zbiór &Zbiór::operator [](int i)
  121. {
  122.     {
  123.         Zbiór *temp = this;
  124.         temp->p[i].x = (-1)*temp->p[i].x;
  125.         temp->p[i].y = (-1)*temp->p[i].y;
  126.  
  127.         return *temp;
  128.     }
  129. }
  130.  
  131.  
  132.  
  133. void Zbiór::Max(int &ktora, float &max)
  134.         {
  135.                 float odl;
  136.                
  137.  
  138.  
  139.  
  140.         for(int i=0; i<ile; i++)
  141.         {
  142.                 odl = sqrt((this -> p[i].x) * (this -> p[i].x) + (this -> p[i].y) * (this -> p[i].y));
  143.                 if(i==0)
  144.                 {
  145.                         max = odl;
  146.                         ktora = i;
  147.                 }
  148.                 else
  149.                         if(odl > max)
  150.                         {
  151.                                 max = odl;
  152.                                 ktora = i;
  153.                         }
  154.  
  155.                        
  156.         }
  157.         cout << "Najwieksza odleglosc to" << max << endl;
  158. }
  159.  
  160. //main.cpp
  161.  
  162. #include "punkt.h"
  163.  
  164. using namespace std;
  165.  
  166.  
  167. int main(){
  168.  
  169.     // p1(8, 8);
  170.     //Punkt p2(9, 9);
  171.     //cout << p1.odleglosc(p2) << endl;
  172.     //Punkt *x1, *x2;
  173.     //x1 = new Punkt(2, 2);
  174.     //x2 = new Punkt(5, 5);
  175.     //x1->dodawanie(*x2);
  176.     //delete x1;
  177.     //delete x2;
  178.    
  179.    
  180.     float *t_x;
  181.     float *t_y;
  182.     int index = 0;
  183.     float odlg = 0.0;
  184.     t_x = new float[3];
  185.     t_y = new float[3];
  186.  
  187.     for (int i = 0; i < 3; i++)
  188.     {
  189.         t_x[i] = i + 1;
  190.         t_y[i] = i + 2;
  191.     }
  192.  
  193.     Zbiór p1(t_x, t_y, 6);
  194.     Zbiór w1(p1);
  195.     Zbiór p2(t_x, t_y, 6);
  196.     p2[2];
  197.    
  198.     Punkt z1(5,19);
  199.     Punkt z2(5,19);
  200.     Punkt z3 = z1 + z2;
  201.  
  202.  
  203.  
  204.     w1.Max(index, odlg);
  205.     cout << "Indeks: " << index << " Odleglosc od 0,0: " << odlg << endl;
  206.     delete t_x;
  207.     delete t_y;
  208.     system("pause");
  209.     return 0;
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement