1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
#include<iostream>
using namespace std;
int Menu();
class Complex
{
private:
int a,b,c,d;
public:
Complex();
Complex(int, int);
Complex operator+ (Complex);
Complex operator- (Complex);
Complex operator* (int);
friend ostream & operator<< (ostream &, Complex &);
friend istream & operator>> (istream &, Complex &);
};
Complex::Complex()
{
a=b=0;
}
Complex::Complex(int a1,int b1)
{
a = a1;
b = b1;
}
Complex Complex::operator+(Complex X)
{
Complex temp;
temp.a = X.a + a;
temp.b = X.b + b;
return temp;
}
Complex Complex::operator-(Complex X)
{
Complex temp;
temp.a = X.a - a;
temp.b = X.b - b;
return temp;
}
Complex Complex::operator*(int scalar)
{
Complex temp;
temp.a = scalar*a;
temp.b = scalar*b;
return temp;
}
istream& operator >> (istream &Ip, Complex &P)
{
cout << "Please enter a Complex number" << endl;
Ip >> P.a; // input a
Ip >> P.b; // input b
Ip.ignore(); // skip (i)
return Ip;
}
ostream& operator << (ostream &Ip,Complex &P)
{
if (P.b < 0)
Ip << "(" << P.a<< " - " << -1*P.b << "i)";
else
Ip << "(" << P.a<< "+" << P.b << "i)";
return Ip;
}
int main()
{
char REDO = 'y';
int a,b,scalar;
while(REDO == 'y')
{
Complex c1,c2,c3;
cin >>c1;
switch(Menu())
{
case 1 : cout << "Add\n";
cout << "Enter a second Complex number\n";
cin >>c2;
c3=c1+c2;
cout << c1 << "+" << c2 << " = " << c3 << endl;
break;
case 2 : cout << "Subtract\n";
cout << "Enter a second Complex number\n";
cin >>c2;
c3=c1-c2;
cout << c1 << " - " << c2 << " = " << c3 <<endl;
break;
case 3 : cout << "Scalar Multiplication\n";
cout << "Scalar = ";
cin >> scalar;
c3=c1*scalar;
cout <<c1 << "(" << scalar << ") = " << c3 << endl;
break;
}
cout << "Would you like to do another? (Y/N)\n";
cin >> REDO;
if (REDO == 'Y')
REDO = 'y';
system("cls");
}
return 0;
}
int Menu()
{
int choice = 0;
cout << "What operation would you like performed." << endl;
cout << "1.Add\n2.Subtract\n3.Scalar Multiplication\n";
cin >> choice;
if (choice < 0 or choice > 3)
cout << "Enter a valid Entry!\n";
else
cout << "Choice = ";
return choice;
}
|