Advertisement
rowers

sgsgs

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