Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.06 KB | None | 0 0
  1. //////////////MAIN.CPP//////////////
  2.  
  3. #include <iostream>
  4. #include <math.h>
  5. #include "header.h"
  6.  
  7. using namespace std;
  8.  
  9. ostream &operator <<(ostream &wyjscie, const Punkt &o_)
  10. {
  11. wyjscie << "x= " << o_.x << " y=" << o_.y << endl;
  12.         return wyjscie;
  13. }
  14.  
  15. int main() {  
  16.         cout<<"Sprawdzanie operatora +"<<endl;
  17. Punkt dod1(1,1);
  18. Punkt dod2(3,2);
  19. Punkt dodw;
  20. dodw=dod1+dod2;
  21.         cout<<"Wynik dodawania punktu ("<<dod1.x<<","<<dod1.y<<") oraz ("<<dod2.x<<","<<dod2.y<<") to ("<<dodw.x<<","<<dodw.y<<")";
  22.        
  23.         cout<<endl<<endl<<"Sprawdzenie operatora == "<<endl;
  24. Punkt spr1(1,3);
  25. Punkt spr2(1,7);
  26.         if (spr1==spr2){
  27.                 cout<<"Porownane punkty sa rowne!"<<endl<<endl;
  28.         }else{
  29.                 cout<<"Porownane punkty nie sa rowne!"<<endl<<endl;
  30.         }
  31.        
  32.         cout<<"Sprawdzenie operatora = "<<endl;
  33. Punkt rown1(3,12);
  34. Punkt rown2(11,2);
  35.         cout<<"Podstawiam w miejsce punktu pierwszego ("<<rown1.x<<","<<rown1.y<<") punkt ("<<rown2.x<<","<<rown2.y<<")"<<endl;
  36. rown1=rown2;
  37.         cout<<"Pierwszy punkt to teraz ("<<rown1.x<<","<<rown1.y<<")"<<endl<<endl;
  38.        
  39.         cout<<"Sprawdzenie operatora +="<<endl;
  40. Punkt pr1(3,2);
  41. Punkt pr2(1,1);
  42.         cout<<"Wynik operatora += z punktow ("<<pr1.x<<","<<pr1.y<<") oraz ("<<pr2.x<<","<<pr2.y;
  43. pr1+=pr2;
  44.         cout<<") to ("<<pr1.x<<","<<pr1.y<<")";
  45.        
  46. /*      
  47.         float *ta_x;
  48.         float *ta_y;
  49.         int indeks = 0;
  50.         float odleglosc = 0.0;
  51. ta_x = new float[5];
  52. ta_y = new float[5];
  53.  
  54.         for (int i = 0; i < 5; i++)
  55.         {
  56. ta_x[i] = i + 2;
  57. ta_y[i] = i + 3;
  58.         }
  59.  
  60. Vector punkcik(5, ta_x, ta_y);
  61. Vector nowywektor(punkcik);
  62. nowywektor.min(indeks, odleglosc);
  63.         cout << "Indeks: " << indeks << " Odleglosc od 0,0: " << odleglosc << endl;      
  64. */      
  65.        
  66.         return 0;                                      
  67. }
  68.  
  69.  
  70. //////////////header.h//////////////
  71.  
  72. #include <iostream>
  73. #include <stdio.h>
  74. #include <ctype.h>
  75.  
  76. using namespace std;
  77.  
  78. class Punkt{
  79.        
  80.       friend class Vector;
  81.       friend ostream &operator <<(ostream &wyjscie, const Punkt &o_);
  82.                
  83. public: int x, y;    
  84. Punkt(){
  85. x=0;
  86. y=0;
  87.                 }
  88. Punkt(int a, int b){
  89. x=a;
  90. y=b;
  91.                 }
  92. Punkt &operator +(Punkt &p){
  93. Punkt pp;
  94. pp.x= x+p.x;
  95. pp.y= y+p.y;
  96.                         return pp;
  97.                 }
  98.                 bool operator ==(Punkt &p){
  99.                         if ((x==p.x) && (y==p.y)){
  100.                                 return true;
  101.                         }else
  102.                                 return false;
  103.                 }
  104. Punkt &operator =(Punkt &p){
  105. x=p.x;
  106. y=p.y;                
  107.                 }
  108. Punkt &operator +=(Punkt &p){
  109. x=x+p.x;
  110. y=y+p.y;
  111.                 }
  112.                
  113. };
  114.  
  115.  
  116. class Vector
  117. {
  118.         int ile;
  119. Punkt *wp;
  120.  
  121. public:
  122. //konstruktor
  123. Vector(int il, float *t_x, float *t_y)
  124.         {
  125. ile = il;
  126. wp = new Punkt[ile];
  127.                 for (int i = 0; i < ile; i++)
  128.                 {
  129. wp[i].x = t_x[i];
  130.                 }
  131.                 for (int i = 0; i < ile; i++)
  132.                 {
  133. wp[i].y = t_y[i];
  134.                 }
  135.         }
  136. Vector(Vector &new_w)
  137.         {
  138. ile = new_w.ile;
  139. wp = new Punkt[ile];
  140.                 for (int i = 0; i < ile; i++)
  141.                 {
  142. wp[i].x = new_w.wp[i].x;
  143. wp[i].y = new_w.wp[i].y;
  144.                 }
  145.         }
  146. //operatory
  147. //metody
  148.         void min(int &indeks, float &mini)
  149.         {
  150.                 float odleglosc = 0.0;
  151.                 for (int i = 0; i < (this->ile); i++)
  152.                 {
  153. odleglosc = sqrt(((this->wp[i].x)*(this->wp[i].x)) + ((this->wp[i].y)*(this->wp[i].y)));
  154.                         if (i==0)
  155.                         {
  156. mini = odleglosc;
  157. indeks = i;
  158.                         }
  159.                         else
  160.                                 if (odleglosc < mini)
  161.                                 {
  162. mini = odleglosc;
  163. indeks = i;
  164.                                 }
  165.                 }
  166.         }
  167. //destruktor
  168. ~Vector()
  169.         {
  170.                 delete wp;
  171.         }
  172. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement