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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
#include<iostream> //Input/Output stream
#include<conio.h> //Allows getch() and clrscr()
#include<math.h> //Allows sqrt and atan
#include<stdlib.h> //Allows exit function
#include<iomanip> //Allows setprecision()
using namespace std;
/*********************************************************************************/
//Extension of Complex class designed in assignment 4, fixed according to feedback
/*********************************************************************************/
class complex
{
private:
int real; //real part
int imag; //imaginary part
public:
complex() //constructor
{real=1; imag=0;} //initialize parts to 1 and 0 respectively
void input(); //input function left empty
void display(complex c1,complex c2);
complex operator +(complex c2); //Operators to be overloaded
complex operator *(complex c2);
float operator !();
float operator ~();
friend void operator == (complex c1,complex c2); //Comparison operators
friend void operator < (complex c1,complex c2); //Used friend function to give comparison operators access to the class members
friend void operator > (complex c1,complex c2);
void show(float f);
}; //end class Complex
/*******************************************************/
void complex::input() //Gets numbers from the user
{
cout<<"\nEnter real part of complex number:";
cin>>real; //Stores real part
cout<<"\nEnter imaginary part of complex number:";
cin>>imag; //Stores imaginary part
}
void complex::display(complex c1,complex c2) //Displays result
{
cout<<"\n Complex numbers:";
cout<<"\n";
cout<<"("<<c1.real; //display first number
if(c1.imag<0) //if statement that allows for negative numbers to be displayed
cout<<"-";
else //otherwise display a plus sign
cout<<"+";
cout<<abs(c1.imag)<<"i)"; //end formatting for first number
cout<<"\n";
cout<<"("<<c2.real; //display first number
if(c2.imag<0) //if statement that allows for negative numbers to be displayed
cout<<"-";
else
cout<<"+"; //otherwise display a plus sign
cout<<abs(c2.imag)<<"i)"; //end formatting for second number
cout<<"\n"<<"Result\n";
cout<<"("<<real;
if(imag<0)
cout<<"-";
else
cout<<"+";
cout<<abs(imag)<<"i)";
}
/*************************************************/
//Begin overloading operators +, *, !, ~, ==, >, <
/*************************************************/
complex complex::operator +(complex c2) //Overloads + operator to add two numbers
{
complex temp;
temp.real=real+c2.real;
temp.imag=imag+c2.imag;
return(temp);
}
complex complex::operator *(complex c2) //Overloads * operator to multiply two numbers
{
complex temp;
temp.real=((real*c2.real)-(imag*c2.imag));
temp.imag=((real*c2.imag)+(imag*c2.real));
return temp;
}
float complex::operator !() //Overloads ! operator for magnitude of one number
{
return sqrt((real*real)+(imag*imag));
}
float complex::operator ~() //Overloads ~ operator for angle of one number
{
return atan2(imag,real); //Use of atan2 to give inverse of imag/real expressed in radians
}
void complex::show(float f) //Displays result for magnitude or angle functions
{
cout<<"\n Complex number:";
cout<<"\n";
cout<<"("<<real;
if(imag<0)
cout<<"-";
else
cout<<"+";
cout<<abs(imag)<<"i)";
cout<<setprecision(3);
cout<<"\n The result is"<<f;
}
void operator ==(complex c1,complex c2) //Checks the equality of the complex numbers
{
if(!c1==!c2) //formula given in problem spec
cout<<"\n\nBoth the complex numbers are equal";
}
void operator <(complex c1,complex c2) //Checks whether first complex number is lesser than the second
{
if(!c1<!c2) //formula given in problem spec
cout<<"\n\nFirst complex number is lesser than the second";
}
void operator >(complex c1,complex c2) //Checks whether first complex number is greater than the second
{
if(!c1>!c2) //formula given in problem spec
cout<<"\n\nFirst complex number is greater than the second";
}
/******************************************************/
//Main driver
/******************************************************/
void main()
{
complex c1,c2,c3; //class instances
int ch; //variable for menu choice
float f; //stores the magnitude or angle
cout<<"\nwelcome \n This program will implement operations on complex numbers.\n"; //Opening statement
do
{
cout<<"\n\nPress any key to continue";
getch();
//Main menu:
cout<<"\n\n1. Input two complex numbers\n2. Add and display result \n3. Multiply and display result \n4. Display magnitude of first complex number \n5. Display angle of first complex number \n6. Quit ";
cout<<"\nEnter your choice:\t";
cin>>ch; //Stores menu choice
switch(ch) //Switch statement that implements menu choice
{
case 1:
c1.input(); //call input function twice for two numbers
c2.input();
//Comparison test immediately following input of two Complex objects
c1==c2; //determines if they are equal
c1<c2; //if first is lesser than second
c1>c2; //if first is greater than second
break;
case 2:
c3=c1+c2; //use overloaded operator + on two numbers
c3.display(c1,c2); //call to display function
break;
case 3:
c3=c1*c2; //use overloaded operator * on two numbers
c3.display(c1,c2); //call to display function result
break;
case 4:
f=!c1; //use overloaded operator ! to get magnitude of first number
c1.show(f); //call to show() function
break;
case 5:
f=~c1; //use overloaded operator ~ to get angle of first number
c1.show(f); //call to show() function
break;
case 6:
exit(1); //exits program
break;
default:
cout<<"\nYou have entered an invalid number.";
break;
}
}while(ch!=6);
getch(); //Holds until the user presses a key
}
|