Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,549 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,567 people online right now. Registration is fast and FREE... Join Now!




Need Help adding complex numbers

 
Reply to this topicStart new topic

Need Help adding complex numbers

James Bond C++ Spy
30 Nov, 2007 - 10:03 AM
Post #1

D.I.C Head
**

Joined: 3 Oct, 2007
Posts: 81


My Contributions
I need to write a program that adds complex numbers. I have the code so far to enter the numbers and display them but I have commented out the addition part. I need some help on this.

complex.cpp file:
CODE

#include "complex_h.h"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

void complex::setr1(double value)
{
    cout << "Please Enter Your First Real Number: ";
    cin >> value;
    r1 = value;
}

void complex::seti1(double value)
{
    cout << "Please Enter Your First Imaginary Number: ";
    cin >> value;
    i1 = value;
}

void complex::setr2(double value)
{
    cout << "Please Enter Your Second Real Number: ";
    cin >> value;
    r2 = value;
}

void complex::seti2(double value)
{
    cout << "Please Enter Your Second Imaginary Number: ";
    cin >> value;
    i2 = value;
}

double complex::getr1(void)
{
    return r1;
}

double complex::geti1(void)
{
    return i1;
}

double complex::getr2(void)
{
    return r2;
}

double complex::geti2(void)
{
    return i2;
}

complex::complex(double r1, double i1, double r2, double i2)
{
    setr1(r1);
    seti1(i1);
    setr2(r2);
    seti2(i2);
}

//double complex::mycomplex(double r1, double i1, double r1, double i1)
//{
   // double addrealproduct=(r1 + r2);
   // double addimagproduct=(i1 + i2);
   // return sqrt(addrealproduct);
   // return sqrt(addimagproduct);

//}

void complex::print(void)
{
    cout<<"("<<r1<<"+"<<i1<<"i"<<")"<<" + "<<"("<<r2<<"+"<<i2<<"i"<<")"<<endl;
    cout<<"Adding the Real and Imaginary numbers has a product of: "<<endl;
}


The complex driver.cpp file:
CODE

#include "complex_h.h"
#include <iostream>
using std::cout;

void main(void)
{
    //addreal = add.myadd(r1, r2);
    //addimag = add.myadd(i1, i2);

    complex a1(1.,2.,1.,2.);
    a1.print();

    //cout<<"("<<addrealproduct<<" + "<<addimagproduct<<")"<<endl;
}


And the complex_h.h file:

CODE

#ifndef complex_h
#define complex_h
class complex
{

public:
void setr1(double);
void seti1(double);
void setr2(double);
void seti2(double);

double getr1(void);
double geti1(void);
double getr2(void);
double geti2(void);

complex(double=1,double=1,double=1,double=1);

void print(void);

private:
    double r1;
    double i1;
    double r2;
    double i2;
};
#endif

//class add
//{
//public:
//double myadd(double, double);
//};


Thanks for the help

This post has been edited by James Bond C++ Spy: 1 Dec, 2007 - 09:00 AM
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Need Help Adding Complex Numbers
30 Nov, 2007 - 11:22 AM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,868



Thanked: 53 times
Dream Kudos: 550
My Contributions
adding complex numbers is really easy... add the real parts, add the imaginary parts. There are a few questions that you have to ask yourself.

Are you creating a class that will overload the addition operator? or are you just creating an add() function? The second method may be more intuitive, but in truth there is little difference between them, you just learn more if you overload the operator. There is a tutorial on this. There are also a number of snippets: here is one, that should get you started.
here is an example in C
Here is my complex number parser
User is offlineProfile CardPM
+Quote Post

James Bond C++ Spy
RE: Need Help Adding Complex Numbers
1 Dec, 2007 - 09:03 AM
Post #3

D.I.C Head
**

Joined: 3 Oct, 2007
Posts: 81


My Contributions
I looked at the examples but still can't figure it out. I am use to one file as opposed to the 3 files. Can someone please help me with my code. Thanks
User is offlineProfile CardPM
+Quote Post

James Bond C++ Spy
RE: Need Help Adding Complex Numbers
4 Dec, 2007 - 10:03 AM
Post #4

D.I.C Head
**

Joined: 3 Oct, 2007
Posts: 81


My Contributions
OK I have redone the code and I know I'm close but getting errors at compile. Can anyone help?

First Error
error C4430; missing type specifier - int assumed.Note C++ does not support default-int

ComplexNum.h file:
CODE

// complex class definition

#ifndef complex_h
#define complex_h

class Complex
{
public:
    complex( double = 0.0, double = 0.0 ); //constructor
    complex operator+( const complex & ) const; //addition
    complex operator-( const complex & ) const; //subtraction
    void print() const;
private:
    double real; // real part
    double imaginary; //imaginary part
}; // end class complex
#endif


complex.cpp file:
CODE

// complex class member function definitions

#include <iostream>
using std::cout;

#include "ComplexNum.h"

// constructor
complex::complex( double realpart, double imaginarypart )
: real( realpart ),
imaginary( imaginarypart )
{
  //empty body
} // end complex constructor

// addition operator
complex complex::operator+( const complex &operand2 ) const
{
    return complex( real + operand2.real,
        imaginary + operand2.imaginary );
} // end function operator+

// subtraction operator
complex complex::operator-( const complex &operand2 ) const
{
    return complex( real - operand2.real,
        imaginary - operand2.imaginary );
} // end function operator-

// diplays in for ( a , b )
void complex::print() const
{
    cout << '( ' << real << " , " << imaginary << ' )';
} // end print function


complex_driver.cpp file:
CODE

// complex class test program

#include <iostream>
using std::cout;
using std::endl;

#include "ComplexNum.h"

int main()
{
    complex a;
    complex b( 4.3, 8.2 );
    complex c( 3.3, 1.1 );

    cout << "a: ";
    a.print();
    cout << "nb: ";
    b.print();
    cout << ""\nc: ";
    c.print()

    a = b + c;
    cout << "\na = b + c: " << endl;
    a.print();
    cout << " = ";
    b.print();
    cout << " + ";
    c.print();

    a = b - c;
    cout << "\na = b - c: " << endl;
    a.print();
    cout << " = ";
    b.print();
    cout << " + ";
    c.print();
    cout << endl;
    return 0;
} // end main


Thanks for any Help

This post has been edited by James Bond C++ Spy: 4 Dec, 2007 - 10:18 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 09:39PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month