Wednesday, November 14, 2012

Complex Number Operations

Language: C++

Purpose:

Performing mathematical operations on complex numbers by overloading operators (+= , -=, etc.) to work with two "Complex number objects" (or just one depending on the operation).
Explanation:

In C++ it is possible to overload mathematical operators. The syntax in the code below will get you started. Also check out learncpp.com chapter 9 for a more in depth look.

I have posted below three files required by my professor.


Complex Class (.cpp)

#include <stdlib.h>
#include <iostream>
#include <cmath>
#include "Complex.h"
using namespace std;

//Global variables
double a = 0;
double b = 0;
string t = "True";
string f = "False";


//Default (NoArgs) Constructor
Complex::Complex()
    :a(0), b(0)
{}

//One arg Constructor
Complex::Complex(double &a )
    :a(0), b(0)
{
    (*this).a = a;
}

//Two arg Constructor
Complex::Complex(double &a, double &b )
    :a(0), b(0)
{
    (*this).a = a;
    (*this).b = b;
}

//Accessor Functions
double Complex::getA()    
{return a;}
double Complex::getB()
{return b;}

//Add 2 Complex numbers together
Complex & ::addCom(Complex & com1, Complex & com2, Complex & temp)
{
    temp.a = com1.a;
    temp.b = com1.b;
    temp += com2;
    return temp;
}

//Subtract 1 Complex number from  another
Complex & ::subCom(Complex & com1, Complex & com2, Complex & temp)
{
    temp.a = com1.a;
    temp.b = com1.b;
    temp -= com2;
    return temp;
}

//Multiply 2 Complex numbers together
Complex & ::mulCom(Complex & com1, Complex & com2, Complex & temp)
{
    temp.a = com1.a;
    temp.b = com1.b;
    temp *= com2;
    return temp;
}

//divide 1 Complex number by another
Complex & ::divCom(Complex & com1, Complex & com2, Complex & temp){
        
    temp.a = com1.a;
    temp.b = com1.b;
    temp /= com2;
    return temp;
}

//Assign the value of one Complex number to another
Complex & ::assign(Complex & com1, Complex & com2, Complex & temp){
        
    com2 = com1;
    temp = com2;
    return temp;
}

//Check for equality
 string & ::isEqual(Complex & com1, Complex & com2){

    if(    com1 == com2){
        return t;
    }
    else{
        return f;
    }
}

 //Check for inequality
 string & ::isNotEqual(Complex & com1, Complex & com2){

    if(    com1 != com2){
        return t;
    }
    else{
        return f;
    }
}

//Absolute value of a Complex number
 Complex & ::absolute(Complex & com1, Complex & temp){

      temp.a = abs(com1.a);
      temp.b = abs(com1.b);
      return temp;
  }

//Operator overload = for Complex Numbers
void Complex::operator = (Complex & c)
{
    (*this).a = c.a;
    (*this).b = c.b;
}

//Operator overload += for Complex Numbers
void Complex::operator += (Complex & c)
{
    (*this).a += c.a;
    (*this).b += c.b;
}

//Operator overload -= for Complex Numbers
void Complex::operator -= (Complex & c)
{
   (*this).a -= c.a;
   (*this).b -= c.b;
}

//Operator overload *= for Complex Numbers
void Complex ::operator *= (Complex & c){
    
    double temp = (*this).a;
    (*this).a = ((*this).a*c.a) - ((*this).b*c.b);
    (*this).b = (((*this).b)*(c.a) + (temp)*c.b);
}

//Operator overload /= for Complex Numbers
void Complex ::operator /= (Complex & c){
    
    double temp = (*this).a;
    (*this).a = (((*this).a*c.a) + ((*this).b*c.b)) / ((c.a * c.a) + (c.b* c.b));
    (*this).b = (((*this).b*c.a) - ((temp)*c.b)) / ((c.a * c.a) + (c.b* c.b));
}

//Operator overload << for Complex Numbers
 ostream & operator << (ostream & out, Complex & c)
{
   out << "(" << c.getA()
       << ", " << c.getB()
       << ")" ;
   return out;
}

//Operator overload == for Complex Numbers
 bool operator == (Complex & l, Complex & r){

     if( l.a == r.a && l.b == r.b){
         return true;
     }
     else{
         return false;
     }

 }

 //Operator overload != for Complex Numbers
  bool operator != (Complex & l, Complex & r){

     if( l.a != r.a && l.b != r.b){
         return true;
     }
     else{
         return false;
     }

 }


Class test (.cpp)
#include "Complex.h"
#include <iostream>
#include <string>

int main(){

    //locals
    double a = 1.00;
    double b = -2.00;
    double c = 3.00;
    double d = 4.00;
    double e = -4.00;
    double f = 2.00;
    double zed = 0;
    
    //Instantiation of Complex number objects
    Complex complexNoArgs;
    Complex complexOneArg(a);
    Complex complexTwoArgs(a, b);
    Complex c1(a, b);
    Complex c2(c, b);
    Complex c3(a,f);
    Complex c4(c,b);
    Complex temp(zed,zed);

    //output stream
    //precision of 2 decimal places and using fixed 
    cout.precision(2);
    cout << "The constructor with no arguments produces " << fixed << complexNoArgs <<endl;
    cout << "The constructor with one arguments produces " << fixed << complexOneArg <<endl;
    cout << "The constructor with two arguments produces " << fixed << complexTwoArgs <<endl;
    cout << c1 << " + " << c2 << " = " << fixed << addCom(c1, c2, temp) <<endl;
    cout << c1 << " - " << c3 << " = " << fixed << subCom(c1, c3, temp) <<endl;
    cout << c1 << " * " << c4 << " = " << fixed << mulCom(c1, c4, temp) <<endl;
    cout << c1 << " / " << c4 << " = " << fixed << divCom(c1, c4, temp) <<endl;
    cout << "If c1 = " << c1 <<  fixed << ", the result of c2 = c1 is c2 = " <<  fixed << assign(c1, c2, temp) <<endl;
    cout << "If we now test c1 == c2 the result is " << isEqual(c1, c2) <<endl;
    cout << "If we now test c1 != c2 the result is " << isNotEqual(c1, c2) <<endl;
    cout << "If c1 = " << c1 <<  fixed <<" and c4 = " << c4 << fixed << " and we calculate c1 += c4, then c1 = " << addCom(c1, c4, temp) <<endl;
    cout << "If c1 = " << c1 <<  fixed <<" and c3 = " << c3 << fixed << " and we calculate c1 -= c3, then c1 = " << subCom(c1, c3, temp) <<endl;
    cout << "If c1 = " << c1 <<  fixed <<" and c4 = " << c4 << fixed << " and we calculate c1 *= c4, then c1 = " << mulCom(c1, c4, temp) <<endl;
    cout << "If c1 = " << c1 <<  fixed <<" and c4 = " << c4 << fixed << " and we calculate c1 /= c4, then c1 = " << divCom(c1, c4, temp) <<endl;
    cout << "If c1 = " << c1 <<  fixed <<" then abs(c1) is " << absolute(c1, temp) <<endl;
}


Header file Complex.h
#ifndef COMPLEX_H
#define COMPLEX_H

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

class Complex
{
    
    // output operator
    friend ostream & operator << (ostream & out, Complex & c);

    

public:

    //Non-member
    friend Complex & addCom(Complex & com1, Complex & com2, Complex & temp);
    friend Complex & subCom(Complex & com1, Complex & com2, Complex & temp);
    friend Complex & mulCom(Complex & com1, Complex & com2, Complex & temp);
    friend Complex & divCom(Complex & com1, Complex & com2, Complex & temp);
    friend Complex & assign(Complex & com1, Complex & com2, Complex & temp);
    friend string & isEqual(Complex & com1, Complex & com2);
    friend string & isNotEqual(Complex & com1, Complex & com2);
    friend Complex & absolute(Complex & com1, Complex & temp);

    Complex(); //Default Constructor
    Complex(double & a); //One argument
    Complex(double & a , double & b); //Two argument

    //accessor functions
    double getA();
    double getB();

     // comparison operators
    friend bool operator == (Complex & l, Complex & r);  // equality
    friend bool operator != (Complex & l, Complex & r);  // inequality


    //Assignment Operators
    void operator = (Complex & c);
    void operator += (Complex & c);
    void operator -= (Complex & c);
    void operator *= (Complex & c);
    void operator /= (Complex & c);
    
    

private:
   // data fields
   double a;  // x value
   double b;  // y value
   
};

#endif


No comments:

Post a Comment