February 09, 2014

OOMP Assignment#4

/*
Aim: Design a C++ Class "Complex" with data members for real and imaginary part. Provide
default and parametrized constructors. Write a program to perform arithmetic operations
of two complex numbers using operator overloading (using either member functions or
friend functions).
*/

#include <iostream>
#include <math.h>
using namespace std;

class Complex{
 public:
   int a;
   int b;
   Complex(){
     Complex(0, 0);
   }
   Complex(int x, int y){
     a = x;
     b = y;
   }
   void get(){
     cout << endl << "Enter real part: ";
     cin >> a;
     cout << endl << "Enter imaginary part: ";
     cin >> b;
     print();
   }
   void print(){
     cout << endl << a << " " << (b >= 0 ? "+" : "-") << " " << abs(b) << "i";
   }
   Complex operator + (Complex c){
     return Complex(a + c.a, b + c.b);
   }
   Complex operator - (Complex c){
     return Complex(a - c.a, b - c.b);
   }
   Complex operator *(Complex c){
     return Complex(a * c.a - b * c.b, a*c.b + c.a * b);
   }
   Complex operator /(Complex c){
     return Complex((a * c.a + b * c.b)/(c.a * c.a + c.b * c.b), (b * c.a - a * c.b)/(c.a * c.a + c.b * c.b));
   }
};

int main(){
 int choice;
 Complex c1, c2, c3;
 do{
   cout << endl << "-- Complex Numbers --";
   cout << endl << "1. Input numbers";
   cout << endl << "2. Display numbers";
   cout << endl << "3. Add";
   cout << endl << "4. Subtract";
   cout << endl << "5. Multiply";
   cout << endl << "6. Divide";
   cout << endl << "7. Exit";
   cout << endl << "Enter your choice: ";
   cin >> choice;
   switch(choice){
     case 1 : c1.get(); c2.get(); break;
     case 2 : c1.print(); c2.print(); break;
     case 3 :
       c3 = c1 + c2;
       cout << endl << "Result of addition: ";
       c3.print();
       break;
     case 4 :
       c3 = c1 - c2;
       cout << endl << "Result of subtraction: ";
       c3.print();
       break;
     case 5 :
       c3 = c1 * c2;
       cout << endl << "Result of multiplication: ";
       c3.print();
       break;
     case 6 :
       c3 = c1 / c2;
       cout << endl << "Result of division: ";
       c3.print();
       break;
     default : cout << endl << "Invalid choice!"; break;
   }
 }while(choice != 7);
 return 0;
}

/*
---------------------------------------
             OUTPUT
---------------------------------------

-- Complex Numbers --
1. Input numbers
2. Display numbers
3. Add
4. Subtract
5. Multiply
6. Divide
7. Exit
Enter your choice: 1

Enter real part: 10

Enter imaginary part: 20

10 + 20i
Enter real part: -15

Enter imaginary part: -25

-15 - 25i
-- Complex Numbers --
1. Input numbers
2. Display numbers
3. Add
4. Subtract
5. Multiply
6. Divide
7. Exit
Enter your choice: 2

10 + 20i
-15 - 25i
-- Complex Numbers --
1. Input numbers
2. Display numbers
3. Add
4. Subtract
5. Multiply
6. Divide
7. Exit
Enter your choice: 3

Result of addition:
-5 - 5i
-- Complex Numbers --
1. Input numbers
2. Display numbers
3. Add
4. Subtract
5. Multiply
6. Divide
7. Exit
Enter your choice: 4

Result of subtraction:
25 + 45i
-- Complex Numbers --
1. Input numbers
2. Display numbers
3. Add
4. Subtract
5. Multiply
6. Divide
7. Exit
Enter your choice: 5

Result of multiplication:
350 - 550i
-- Complex Numbers --
1. Input numbers
2. Display numbers
3. Add
4. Subtract
5. Multiply
6. Divide
7. Exit
Enter your choice: 6

Result of division:
0 + 0i
-- Complex Numbers --
1. Input numbers
2. Display numbers
3. Add
4. Subtract
5. Multiply
6. Divide
7. Exit
Enter your choice: 7
*/

No comments:

Post a Comment