Javkens

Untitled

Jan 18th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.17 KB | None | 0 0
  1. //MAIN.CPP
  2. //kompulowac z plikiem vect.cpp
  3. #include <iostream>
  4. #include "complex0.h"
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.  
  10. Complex0 a(3,4);
  11. Complex0 c(10,12);
  12. cin >> c;
  13.  
  14. cout << "c to " << c << '\n';
  15. cout << "a to " << a << '\n';
  16. cout << "a + c wynosi " << a + c << endl;
  17. cout << "a - c wynosi " << a - c << endl;
  18. cout << "a * c wynosi " << a * c << endl;
  19. cout << "c * 2 wynosi " << c * 2 << endl;
  20.  
  21. return 0;
  22. }
  23.  
  24.  
  25. //////////
  26. COMPLEX0.H
  27. /////////
  28. #ifndef COMPLEX0_H_
  29. #define COMPLEX0_H_
  30. #include <iostream>
  31.  
  32. class Complex0
  33. {
  34. private:
  35.     double real;
  36.     double imag;
  37.  
  38. public:
  39.     Complex0();
  40.     Complex0(double rl, double im);
  41.     ~Complex0();
  42.     void set_real() {std::cin >> real;}
  43.     void set_imag() {std::cin >> imag;}
  44.     friend std::ostream & operator<<(std::ostream & os, const Complex0 & c);
  45.     friend std::iostream & operator>>(std::iostream & os, Complex0 & c);
  46.     Complex0 operator+(Complex0 &b);
  47.     Complex0 operator-(Complex0 &b);
  48.     Complex0 operator*(Complex0 &b);
  49.     Complex0 operator*(double b);
  50. };
  51. #endif
  52.  
  53.  
  54.  
  55.  
  56. ///////////
  57. COMPLEX0.CPP
  58. ///////////
  59. //#include <cmath>
  60. #include <iostream>
  61. #include "complex0.h"
  62. using namespace std;
  63.  
  64.  
  65. Complex0::Complex0()
  66. {
  67.     cout << "Nie podano danych liczby. Zerowanie liczby";
  68.     real = 0;
  69.     imag = 0;
  70. }
  71.  
  72. Complex0::Complex0(double rl, double im=0)
  73. {
  74.     real = rl;
  75.     imag = im;
  76. }
  77.  
  78. Complex0::~Complex0() {}
  79.  
  80. std::ostream & operator<<(std::ostream & os, const Complex0 & c)
  81. {
  82.     os << "(" << c.real <<  ", " << c.imag << "i)";
  83.     return os;
  84. }
  85.  
  86. std::iostream & operator<<(std::iostream & os, Complex0 & c)
  87. {
  88.     os << "Podaj czesc rzeczywista: ";
  89.     c.set_real();
  90.     os << endl << "Podaj czesc urojona: ";
  91.     c.set_imag();
  92.     os << endl;
  93.     return os;
  94. }
  95.  
  96. Complex0 Complex0::operator+(Complex0 &b)
  97. {
  98.     return Complex0(real+b.real, imag + b.imag);
  99. }
  100.  
  101. Complex0 Complex0::operator-(Complex0 &b)
  102. {
  103.     return Complex0(real-b.real, imag-b.imag);
  104. }
  105.  
  106. Complex0 Complex0::operator*(Complex0 &b)
  107. {
  108.     return Complex0(real*b.real - imag*b.imag, real*b.imag+imag*b.real);
  109. }
  110.  
  111. Complex0 Complex0::operator*(double b)
  112. {
  113.     return Complex0(real*b, imag*b);
  114. }
Advertisement
Add Comment
Please, Sign In to add comment