Advertisement
rowers

programikc

Nov 27th, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 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);
  47.  
  48.  
  49.  
  50.  
  51. ~Zbiór();
  52.  
  53. }
  54.  
  55.  
  56. punkcik.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.  
  95.     }
  96.  
  97. float Zbiór::Max(int &ktora)
  98.     {
  99.         float odl;
  100.         float max;
  101.  
  102.  
  103.  
  104.     for(int i; i<ile; i++)
  105.     {
  106.         odl = sqrt((this -> p[i].x) * (this -> p[i].x) + (this -> p[i].y) * (this -> p[i].y));
  107.         if(i==0)
  108.         {
  109.             max = odl;
  110.             ktora = i;
  111.         }
  112.         else
  113.             if(odl > max)
  114.             {
  115.                 max = odl;
  116.                 ktora = i;
  117.             }
  118.     }
  119. }
  120.  
  121.  
  122.            
  123. main.cpp
  124.  
  125. #include "punkt.h"
  126.  
  127. using namespace std;
  128.  
  129. int main(){
  130.     //Punkt punkt1(5,5);
  131.     //Punkt punkt2(8,8);
  132.     //cout << punkt1.odl(punkt2) << endl;
  133.     //Punkt *x1, *x2;
  134.     //x1 = new Punkt(4, 4);
  135.     //x2 = new Punkt(6, 6);
  136.     //x1->dodaj(*x2);
  137.     //delete x1;
  138.     //delete x2;
  139.  
  140.     float *ta_x;
  141.     float *ta_y;
  142.     int indeks = 0;
  143.     float odleglosc = 0.0;
  144.     ta_x = new float[3];
  145.     ta_y = new float[3];
  146.  
  147.     for (int i = 0; i < 3; i++)
  148.     {
  149.         ta_x[i] = i + 1;
  150.         ta_y[i] = i + 2;
  151.     }
  152.  
  153.     Zbiór punkcik(5, ta_x, ta_y);
  154.     Zbiór nowywektor(punkcik);
  155.  
  156.  
  157.     nowywektor.Max(indeks, odl);
  158.     cout << "Indeks: " << indeks << " Odleglosc od 0,0: " << odl << endl;
  159.     delete ta_x;
  160.     delete ta_y;
  161.    
  162.     system("pause");
  163.     return 0;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement