Advertisement
rowers

ccc

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