Skip to content

Instantly share code, notes, and snippets.

@ebba0194
Created March 14, 2018 01:06
Show Gist options
  • Save ebba0194/3b9c4972ab558f7d9e0a4446ea902fff to your computer and use it in GitHub Desktop.
Save ebba0194/3b9c4972ab558f7d9e0a4446ea902fff to your computer and use it in GitHub Desktop.
Classwork; Overloaded Operations with Complex Numbers
#include <iostream>
#include <cmath>
using namespace std;
class Complex{
public:
Complex();//constructor
Complex(float, float);
void getInput();//get user input of complex number
void convertNum(float, float, float);//convert a real number (r) to complex number (r+0i)
friend ostream& operator << (ostream& outputStream, Complex& Object);
friend istream& operator >> (istream& inputStream, Complex& Object);
float getA(){ //accessor functions
return a;
}
float getB(){
return b;
}
float setA(float x){ //mutator functions
a = x;
}
float setB(float x){
b = x;
}
private:
float a, b; //variables for numbers
};
const Complex operator + (Complex X, Complex Y);
const Complex operator - (Complex X, Complex Y);
const Complex operator * (Complex X, Complex Y);
const Complex operator / (Complex X, Complex Y);
bool operator == (Complex X, Complex Y);
void complexArithmetic();
void quadraticSolution();
int main(){
int input;
do{
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();
}
else if (input == 2){
quadraticSolution();
}
else if (input == 3){
break;
}
else {
cout << "ERROR: INVALID OPERATION;" <<endl;
}
} while (input != 3);
return 0;
}
void complexArithmetic(){
char operation;
Complex solutionObject;
Complex firstIn;
Complex secondIn;
cin >> firstIn;
cout << "Enter an operation (+, -, *, /): ";
cin >> operation >> secondIn;
switch (operation){
case '+':
solutionObject = firstIn + secondIn;
break;
case '-':
solutionObject = firstIn - secondIn;
break;
case '*':
solutionObject = firstIn * secondIn;
break;
case '/':
solutionObject = firstIn / secondIn;
break;
default:
cout << "ERROR: INVALID OPERATION \n";
break;
}
cout << firstIn << " " << operation << " " << secondIn << " = " << solutionObject << endl << endl;
}
void quadraticSolution(){
float a, b, c;
Complex quadraticObject;
Complex solutionObject;
cout << "Enter the coefficients of a quadratic equation: ";
cin >> a >> b >> c;
quadraticObject.convertNum(a, b, c);
cin >> solutionObject;
if (solutionObject == quadraticObject){
cout << "\nThe complex number " << solutionObject << " is a solution to the quadratic equation." << endl;
}
else {
cout << "\nThe complex number " << solutionObject << " is not a solution to the quadratic equation." << endl;
}
}
Complex::Complex(){//constructor to set new objects to zero
a = 0;
b = 0;
}
Complex::Complex(float m, float n){
a = m;
b = n;
}
void Complex::convertNum(float x, float y, float z){
float realX, compX, posVal;
posVal = (-1*((y*y) -4*x*z));
a = -y / (2*x);
b = sqrt (posVal) / (2*x);
}
bool operator == (Complex X, Complex Y){
return ((X.getA() == Y.getA()) && (X.getB() == Y.getB()));
}
const Complex operator + (Complex X, Complex Y){
float m = (X.getA() + Y.getA());
float n = (X.getB() + Y.getB());
return Complex(m, n);
}
const Complex operator - (Complex X, Complex Y){
float m = (X.getA() - Y.getA());
float n = (X.getB() - Y.getB());
return Complex(m, n);
}
const Complex operator * (Complex X, Complex Y){
float m = (X.getA()*Y.getA())-(X.getB()*Y.getB());
float n = (X.getA()*Y.getB()+X.getB()*Y.getA());
return Complex(m, n);
}
const Complex operator / (Complex X, Complex Y){
float m = ((X.getA()*Y.getA()) + (X.getB()*Y.getB()))/((Y.getA()*Y.getA())+(Y.getB()*Y.getB()));
float n = ((X.getB()*Y.getA())-(X.getA()*Y.getB()))/((Y.getA()*Y.getA())+(Y.getB()*Y.getB()));
return Complex(m, n);
}
ostream& operator << (ostream& outputStream, Complex& object){
if (object.getB() >= 0){
cout << object.getA() << "+" << object.getB() <<"i";
}
else if (object.getB() < 0){
cout << object.getA() << object.getB() << "i";
}
return outputStream;
}
istream& operator >> (istream& inputStream, Complex& object){
char i;
float x, y;
cout << "Enter a complex number a+bi: ";
cin >> x >> y >> i;
object.setA(x);
object.setB(y);
return inputStream;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment