i need some help with my assignment the link is to the assignment

Click Here

Complex.h

#include <iostream>
using namespace std;

class Complex

{
private: 
double realPart;
double imaginaryPart;

public:
//Default Constructor
Complex();

//Parameterized Constructor
Complex(double r, double i);

double GetRealPart() const;
double GetImaginaryPart() const;

void printComplex();
};

test.cpp

#include <iostream>
#include <complex>
using namespace std;

typedef complex<double> dcomplex;

int main(){

    dcomplex a,b;


    cout << "Enter real number: ";
    cin >> a;
    cout << "Enter imaginary number: ";
    cin >> b;

    cout << "a = " << a << "\n";
    cout << "b = " << b << "\n";

    cout << "a + b = " << a + b << "\n";
    cout << "a * b = " << a * b << "\n";
    cout << "a / b = " << a / b << "\n";
    cout << "|a| = "   << abs(a) << "\n";
    cout << "complex conjugate of a = " << conj(a) << "\n";
    cout << "norm of a = " << norm(a) << "\n";
    cout << "abs of a = " << abs(a) << "\n";
    cout << "exp(a) = " << exp(a) << "\n";

    }

complex.cpp

    #include <iostream>
    #include "complex.h"
    using namespace std;
    Complex::Complex(double, double)
    {
    }
    double Complex::GetRealPart( ) const
    {
    return realPart;
    }
    double Complex::GetImaginaryPart( ) const
    {
    return imaginaryPart;
    }
    void printComplexNumber( const Complex& n)
    {

    if( n.GetImaginaryPart ( ) < 0)
    cout << n.GetRealPart( ) <<" - "<< n.GetImaginaryPart( ) << "i";

    if( n.GetImaginaryPart( ) > 0)
    cout << n.GetRealPart( ) <<" + "<< n.GetImaginaryPart( ) << "i";

    if( n.GetImaginaryPart( ) > 0)
    cout << n.GetRealPart( ) <<" * "<< n.GetImaginaryPart( ) << "i";

    if( n.GetImaginaryPart( ) > 0)
    cout << n.GetRealPart( ) <<" / "<< n.GetImaginaryPart( ) << "i";

    if( n.GetRealPart( ) == 0)
    cout << n.GetImaginaryPart( ) << "i";

    if( n.GetImaginaryPart( ) == 0 )
    cout << n.GetRealPart( );

    if( n.GetImaginaryPart( ) == 1 )
    cout << n.GetRealPart( ) << "i";

    }

Could you please tell us what you're having trouble with? I can see several things that need to be done, but it would be helpful if we knew what you were looking for specifically.

i just dont know if i am on the right track or what..im am so confused

Ah, OK... well, I will tell you that the printComplex() function is overly complex (if you pardon the pun). You should have only three tests, in more or less this order:

  • If the real part is zero;
  • if the imaginary part is negative; and
  • if the imaginary part is one.

At each step, you print just the one part of the number. This should be fairly straightforward to code.

ok so i changed it to this...i think that is what u meant

    void printComplexNumber( const Complex& n)
        {

        if( n.GetImaginaryPart ( ) < 0) //if the imaginary part is negative
        cout << n.GetRealPart( ) <<" - "<< n.GetImaginaryPart( ) << "i";

        if( n.GetRealPart( ) == 0) //if the real part is zero
        cout << n.GetImaginaryPart( ) << "i";

        if( n.GetImaginaryPart( ) == 1 ) //if the imaginary part is one
        cout << n.GetRealPart( ) << "i";

        }

Edited 4 Years Ago by hartk1213

ok so my partner on the project came to help me and he had something that looks alot better

here is his

complex.cpp

#include "stdafx.h"
#include <iostream>
#include "complex.h"
using namespace std;

//no arg constructor
complex::complex()
{
    realPart = 0;
    imagPart = 0;
}

//one arg instructor
complex::complex(double real)
{
    realPart = real;
    imagPart = 0;
}

//two arg constructor
complex::complex(double real, double imaginary)
{
    realPart = real;
    imagPart = imaginary;
}

//addition
complex complex::operator+(const complex &number2) const
{
    return realPart + number2.realPart, imagPart + number2.imagPart;
}

//subtraction
complex complex::operator-(const complex &number2) const
{
    return realPart - number2.realPart, imagPart - number2.imagPart;
}

//multiplication
complex complex::operator*(const complex &number2) const
{
    return realPart * number2.realPart, imagPart * number2.imagPart;
}

//division
complex complex::operator/(const complex &number2) const
{
    return realPart / number2.realPart, imagPart / number2.imagPart;
}

//output display for complex number
void complex::print() const
{
    cout << '(' << realPart << ", " << imagPart << ')';
}

test.cpp

#include "stdafx.h"
#include <iostream>
#include "complex.h"

using namespace std;

int main()
{
    complex b(1.0, 0.0);
    complex c(3.0, -1.0);

   cout << "a: ";
    a.print();

    system ("PAUSE");
};

complex.h

//complex class definition
#ifndef COMPLEX_H 
#define COMPLEX_H


//class complex
class complex
{
public:

    complex(); //default no arg constructor
    complex(double a); //one arg constructor
    complex(double a, double b); //two arg constructor
    complex operator+(const complex &) const; // + (addition) method
    complex operator-(const complex &) const; // - (subtraction) method
    complex operator*(const complex &) const; // * (multi) method
    complex operator/(const complex &) const; // / (division) method 
    void print() const;

private:

    double realPart; // represents the real number
    double imagPart; // represent the imaginary number
}; //end class Complex

#endif

now i am getting the errors

Error 1 error C2065: 'a' : undeclared identifier
Error 2 error C2228: left of '.print' must have class/struct/union
Error 3 IntelliSense: identifier "a" is undefined

This article has been dead for over six months. Start a new discussion instead.