Created
March 6, 2018 18:25
-
-
Save ebba0194/86546ffdb1d14fdad7d794e0cabfa331 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cmath> | |
using namespace std; | |
class Complex{ | |
public: | |
Complex();//constructor | |
addComp(Complex&, Complex, Complex);//addition of complex members | |
subComp(Complex&, Complex, Complex);//subtraction of complex numbers | |
multComp(Complex&, Complex, Complex);//multiplication of complex numbers | |
divComp(Complex&, Complex, Complex);//division of complex numbers | |
getInput(Complex&);//get user input of complex number | |
dispComp(Complex);//display complex number | |
convertNum(Complex&, float, float, float);//convert a real number (r) to complex number (r+0i) | |
checkComp(Complex, Complex);//check for equality of 2 complex numbers | |
float a, b; //variables for numbers | |
}; | |
void complexArithmetic(Complex); | |
void quadraticSolution(Complex&, Complex&); | |
int main(){ | |
int input; | |
do{ | |
Complex solutionObject; | |
Complex quadraticObject; | |
cout << "Select an option- " | |
<<"(1) perform complex number arithmetic" << endl | |
<<"\t\t (2) check for quadratic equation solution" << endl | |
<<"\t\t (3) exit" << endl; | |
cin >> input; | |
if (input == 1){ | |
complexArithmetic(solutionObject); | |
} | |
else if (input == 2){ | |
quadraticSolution(solutionObject, quadraticObject); | |
} | |
} while (input != 3); | |
return 0; | |
} | |
void complexArithmetic(Complex solutionObject){ | |
char operation; | |
Complex firstIn; | |
Complex secondIn; | |
firstIn.getInput(firstIn); | |
cout << "Enter an operation (+, -, *, /): "; | |
cin >> operation; | |
secondIn.getInput(secondIn); | |
switch (operation){ | |
case '+': | |
solutionObject.addComp(solutionObject, firstIn, secondIn); | |
break; | |
case '-': | |
solutionObject.subComp(solutionObject, firstIn, secondIn); | |
break; | |
case '*': | |
solutionObject.multComp(solutionObject, firstIn, secondIn); | |
break; | |
case '/': | |
solutionObject.divComp(solutionObject, firstIn, secondIn); | |
break; | |
default: | |
cout << "ERROR: INVALID OPERATION \n"; | |
break; | |
} | |
firstIn.dispComp(firstIn); | |
cout << " " << operation << " "; | |
secondIn.dispComp(secondIn); | |
cout << " = "; | |
solutionObject.dispComp(solutionObject); | |
cout << endl << endl; | |
} | |
void quadraticSolution(Complex& solutionObject, Complex& quadraticObject){ | |
float a, b, c; | |
cout << "Enter the coefficients of a quadratic equation: "; | |
cin >> a >> b >> c; | |
quadraticObject.convertNum(quadraticObject, a, b, c); | |
solutionObject.getInput(solutionObject); | |
if (solutionObject.checkComp(solutionObject, quadraticObject) == true){ | |
cout << "\nThe complex number " << solutionObject.dispComp(solutionObject) << " is a solution to the quadratic equation." << endl; | |
} | |
else if (solutionObject.checkComp(solutionObject, quadraticObject) == false){ | |
cout << "\nThe complex number " << solutionObject.dispComp(solutionObject) << " is not a solution to the quadratic equation." << endl; | |
} | |
} | |
Complex::Complex(){//constructor to set new objects to zero | |
a = 0; | |
b = 0; | |
} | |
Complex::checkComp(Complex solutionObject, Complex quadraticObject){ | |
if ((solutionObject.a - quadraticObject.a) < abs(0.00001) && ((solutionObject.b - quadraticObject.b) < abs(0.0001))){ | |
return (true); | |
} | |
else { | |
return (false); | |
} | |
} | |
Complex::convertNum(Complex& quadraticObject, float a, float b, float c){ | |
float realX, compX, posVal; | |
posVal = (-1*((b*b) -4*a*c)); | |
quadraticObject.a = -b / (2*a); | |
quadraticObject.b = sqrt (posVal) / (2*a); | |
} | |
Complex::addComp(Complex& solutionObject, Complex X, Complex Y){ | |
solutionObject.a = (X.a + Y.a); | |
solutionObject.b = (X.b + Y.b); | |
} | |
Complex::subComp(Complex& solutionObject, Complex X, Complex Y){ | |
solutionObject.a = (X.a - Y.a); | |
solutionObject.b = (X.b - Y.b); | |
} | |
Complex::multComp(Complex& solutionObject, Complex X, Complex Y){ | |
solutionObject.a = (X.a*Y.a)-(X.b*Y.b); | |
solutionObject.b = (X.a*Y.b)+(X.b*Y.a); | |
} | |
Complex::divComp(Complex& solutionObject, Complex X, Complex Y){ | |
solutionObject.a = ((X.a*Y.a) + (X.b*Y.b))/((Y.a*Y.a)+(Y.b*Y.b)); | |
solutionObject.b = ((X.b*Y.a)-(X.a*Y.b))/((Y.a*Y.a)+(Y.b*Y.b)); | |
} | |
Complex::dispComp(Complex object){ | |
cout << object.a << "+" << object.b <<"i"; | |
} | |
Complex::getInput(Complex& object){ | |
char i; | |
cout << "Enter a complex number a+bi: "; | |
cin >> object.a >> object.b >> i; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment