Guest
Public paste!

Untitled

By: a guest | Mar 19th, 2010 | Syntax: C++ | Size: 1.98 KB | Hits: 54 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using std::cout;
  5. using std::cin;
  6. using std::endl;
  7. using std::fixed;
  8.  
  9. class Complex
  10. {
  11. protected:
  12.   float re, im;
  13.  
  14. public:
  15.   Complex(){
  16.         re = 0;
  17.         im = 0;
  18.   }
  19.  
  20.   Complex(float re){
  21.         this->re = re;
  22.         this->im = 0;
  23.   }
  24.  
  25.   Complex(float re, float im){
  26.         this->re = re;
  27.         this->im = im;
  28.   }
  29.  
  30.   void print(){
  31.         cout << fixed << re;
  32.         if(im != 0)
  33.           if (im > 0)
  34.                 cout << " + " << fixed << im << "i";
  35.           else
  36.                 cout << " - " << fixed << -im << "i";
  37.         cout << endl;
  38.   }
  39.  
  40.   void read(){
  41.         float a, b;
  42.         cin >> a >> b;
  43.         re = a;
  44.         im = b;
  45.   }
  46.  
  47.   float mod(){
  48.         return re * re + im * im;
  49.   }
  50.  
  51.   Complex operator+ (Complex &x){
  52.         return Complex(re + x.re, im + x.im);
  53.   }
  54.   Complex operator- (Complex &x){
  55.         return Complex(re - x.re, im - x.im);
  56.   }
  57.   Complex operator* (Complex &x){
  58.         return Complex(re * x.re - im * x.im, re * x.im + im * x.re);
  59.   }
  60.   Complex operator/ (Complex &x){
  61.         if(x.re == 0 && x.im ==0){
  62.           cout << "Divide by zero!";
  63.           return Complex(0, 0);
  64.         }
  65.         else
  66.           return Complex((re * x.re + im * x.im) / x.mod(),
  67.                                          (re * x.im - im * x.re) / x.mod());
  68.   }
  69. };
  70.  
  71. int main(){
  72.   Complex x, y, z;
  73.   char action;
  74.   cout << "Enter first number: ";
  75.   x.read();
  76.   cout << "Enter second number: ";
  77.   y.read();
  78.   cout << "Choose action (+, -, *, /): ";
  79.   cin >> action;
  80.   switch(action){
  81.   case '+':
  82.         z = x + y;
  83.         break;
  84.   case '-':
  85.         z = x - y;
  86.         break;
  87.   case '*':
  88.         z = x * y;
  89.         break;
  90.   case '/':
  91.         z = x / y;
  92.         break;
  93.   }
  94.   cout << "Answer: ";
  95.   z.print();
  96.   float a, b, c;
  97.   Complex *x1, *x2;
  98.   cout << "Enter coefficients: ";
  99.   cin >> a >> b >> c;
  100.   float D = b * b - 4 * a * c;
  101.   if(D >= 0){
  102.         x1 = new Complex((- b + sqrt(D)) / (2 * a));
  103.         x2 = new Complex((- b - sqrt(D)) / (2 * a));
  104.   } else {
  105.         x1 = new Complex(-b / (2 * a), sqrt(-D) / (2 * a));
  106.         x2 = new Complex(-b / (2 * a), -(sqrt(-D) / (2 * a)));
  107.   }
  108.   cout << "x1 = ";
  109.   x1->print();
  110.   cout << "x2 = ";
  111.   x2->print();
  112.   return 0;
  113. }