Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. class complexNum
  4. {
  5.  
  6. private:
  7.  
  8.     double w, i;
  9.  
  10. public:
  11.  
  12.     complexNum ();
  13.     complexNum (double, double);
  14.  
  15.     void add      (complexNum o)
  16.     {
  17.         w += o.getWholePart();
  18.         i += o.getImagiPart();
  19.     }
  20.     void subtract (complexNum o)
  21.     {
  22.         w -= o.getWholePart();
  23.         i -= o.getImagiPart();
  24.     }
  25.     void multiply (complexNum o)
  26.     {
  27.         double tmp = w;
  28.         w = w*o.getWholePart() - i*o.getImagiPart();
  29.         i = tmp*i + i*o.getWholePart();
  30.     }
  31.  
  32.     double getWholePart ()
  33.     {
  34.         return w;
  35.     }
  36.     double getImagiPart ()
  37.     {
  38.         return i;
  39.     }
  40.     void print()
  41.     {
  42.         std :: cout << "(" << w << " + " << i<<"i)";
  43.     }
  44.  
  45.  
  46. };
  47.  
  48. complexNum::complexNum ()
  49. {
  50.     w = 16;
  51.     i = 12;
  52. }
  53.  
  54. complexNum::complexNum (double wPart, double iPart)
  55. {
  56.     w = wPart;
  57.     i = iPart;
  58. }
  59.  
  60. int main()
  61. {
  62.     double x1, y1, x2, y2;
  63.     std::cout << "Enter an x1 and y1 value in the form \"x1 y1\"\n";
  64.     std::cin >> x1 >> y1;
  65.     std::cout << "Enter an x2 and y2 value in the form \"x2 y2\"\n";
  66.     std::cin >> x2 >> y2;
  67.  
  68.     complexNum a = complexNum(x1, y1), b = complexNum(x2, y2);
  69.     a.add(b);
  70.     a.print();
  71.     a = complexNum(x1, y1), b = complexNum(x2, y2);
  72.     a.subtract(b);
  73.     a.print();
  74.     a = complexNum(x1, y1), b = complexNum(x2, y2);
  75.     a.multiply(b);
  76.     a.print();
  77.  
  78.  
  79.     system("PAUSE");
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement