SHARE
TWEET

Siemano

a guest Jan 21st, 2020 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ///////////
  2. Wektory.pro
  3. ///////////
  4. TEMPLATE = app
  5. CONFIG += console c++11
  6. CONFIG -= app_bundle
  7. CONFIG -= qt
  8.  
  9. SOURCES += \
  10.         main.cpp \
  11.         wektor.cpp
  12.  
  13. HEADERS += \
  14.     wektor.h
  15.  
  16.  
  17. ///////////////
  18. wektor.h
  19. ///////////////
  20. #ifndef WEKTOR_H
  21. #define WEKTOR_H
  22. #include <iostream>
  23. using namespace std;
  24.  
  25. class Wektor
  26. {
  27.     double *W=nullptr;
  28.     int numof_elem;
  29. public:
  30.     int Size() { return numof_elem; }
  31.     Wektor(int num_elem);
  32.     ~Wektor();
  33.     double& elem(int elem_num);
  34.     void operator =(double a);
  35.     Wektor &operator =(const Wektor &N);
  36.     Wektor operator +(Wektor &N);
  37.     Wektor operator -(Wektor &N);
  38.     Wektor operator *(Wektor &N);
  39.     friend ostream& operator<< (ostream &wyjscie,Wektor &m);
  40. };
  41.  
  42. #endif // WEKTOR_H
  43.  
  44.  
  45. /////////////////
  46. main.cpp
  47. ////////////////
  48. #include <iostream>
  49. #include <wektor.h>
  50.  
  51. using namespace std;
  52.  
  53. int main()
  54. {
  55.     int rozmiar;
  56.     double element;
  57.     cout << "Podaj rozmiar 1. wektora: ";
  58.     cin >> rozmiar;
  59.     Wektor *w1 = new Wektor(rozmiar);
  60.     for (int i=0;i<rozmiar;i++){
  61.         cout << "Podaj " << i << ". element 1. wektora: ";
  62.         cin >> element;
  63.         cout << endl;
  64.         w1->elem(i)=element;
  65.     }
  66.     cout << "1. wektor:" << endl;
  67.     cout << *w1 << endl;
  68.  
  69.     Wektor *w2 = new Wektor(rozmiar);
  70.     *w2 = *w1;
  71.     cout << "2. wektor:" << endl;
  72.     cout << *w2 << endl;
  73.  
  74.     Wektor *w3 = new Wektor(rozmiar);
  75.     *w3 = *w1 + *w1;
  76.     cout << "Wynik dodawania:" << endl;
  77.     cout << *w3 << endl;
  78.     *w3 = *w1 - *w1;
  79.     cout << "Wynik odejmowania:" << endl;
  80.     cout << *w3 << endl;
  81.     *w3 = *w1 * *w1;
  82.     cout << "Wynik mnozenia skalarnego:" << endl;
  83.     cout << *w3 << endl;
  84.  
  85.     delete w1;
  86.     delete w2;
  87.     delete w3;
  88.  
  89.     return 0;
  90. }
  91.  
  92.  
  93. ///////////////////
  94. wektor.cpp
  95. //////////////////
  96. #include "wektor.h"
  97. #include <iostream>
  98. using namespace std;
  99.  
  100.  
  101. Wektor::Wektor(int num_elem)
  102. {
  103.     numof_elem=num_elem;
  104.     W = new double[numof_elem];
  105.     for(int i=0;i<numof_elem;i++)
  106.         W[i]=0;
  107. }
  108.  
  109. Wektor::~Wektor()
  110. {
  111.   for (int i=0; i<numof_elem; i++)
  112.    delete[] &W[i];
  113.    delete[] W;
  114. }
  115.  
  116. double& Wektor::elem(int  elem_num)
  117. {
  118.   return W[elem_num];
  119. }
  120.  
  121. void Wektor::operator= (double  a)
  122. {
  123.   for (int i=0; i<numof_elem; i++)
  124.       W[i]=a;
  125. }
  126.  
  127. Wektor& Wektor::operator= ( const Wektor&  N)
  128. {
  129.   if(&N==this) return *this;
  130.  
  131.   for(int i=0;i<numof_elem;i++)
  132.   delete[] &W[i];
  133.   delete[] W;
  134.   int  num_elem=this->Size(); //this wskazuje na N
  135.   W=new double[num_elem];
  136.    for(int i=0;i<num_elem;i++)
  137.      W[i]=N.W[i];
  138.      return *this;
  139.    }
  140.  
  141. Wektor  Wektor::operator+ (Wektor&  N)
  142. {
  143.   int       num_elem=N.numof_elem;
  144.  
  145.   Wektor   S(num_elem);
  146.  
  147.   for (int i=0; i<num_elem; i++)
  148.       S.W[i]=W[i]+N.W[i];
  149.   return S;
  150. }
  151.  
  152. Wektor  Wektor::operator- (Wektor&  N)
  153. {
  154.   int       num_elem=N.numof_elem;
  155.  
  156.   Wektor    S(num_elem);
  157.  
  158.   for (int i=0; i<num_elem; i++)
  159.       S.W[i]=W[i]-N.W[i];
  160.   return S;
  161. }
  162.  
  163. Wektor Wektor::operator* (Wektor&  N)
  164. {
  165.     int     num_elem=N.numof_elem;
  166.  
  167.     Wektor  S(num_elem);
  168.  
  169.     for (int i=0; i<num_elem; i++)
  170.         S.W[i]=W[i]*N.W[i];
  171.     return S;
  172. }
  173.  
  174. ostream& operator<< (ostream &wyjscie,Wektor &m)
  175. {
  176.     int num_elem=m.Size();
  177.  
  178.     for (int i=0; i<num_elem; i++){
  179.     wyjscie << m.elem(i) << " ";
  180.     }
  181. wyjscie << endl;
  182. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top