Click to See Complete Forum and Search --> : complex numbers c++
shaggy112
10-26-2001, 08:03 AM
Hello,
I am trying to do arithmetic operations on complex numbers. I am not exactly sure how to handle the problem. For example:
3 + 4i = 3 + 4i
4i + 6i = 10i
would appreciate any help that you can give.
thanks
AdaHacker
10-26-2001, 06:42 PM
Well, I'd probably start by introducing a class or struct for complex numbers. You set it up with a real part and an imaginary part for each number. Then you should be able to just start overloading the operators you need. Addition/subtraction will just add the real parts and imaginary parts and put them into the real and imagainary parts of the return value. For multiplication, you'd do your multiplication via good old FOIL and then check each of the power of i to see if you need to move an imaginary part into the real part. And so on for the other operators. I/O should be along the same lines: print/read a real part, a '+' sign (or '-' if you prefer), an imaginary part, and then an 'i'.
That's the general idea, anyway. I haven't written C++ in a while, so I can't give you any (reliable) code.
[ 26 October 2001: Message edited by: AdaHacker ]
UprightMan
10-26-2001, 10:50 PM
class complex
{
complex();
complex(int real, int imag);
complex(const complex&);
void output(ostream&) const;
complex& conjugate();
complex& plusequal(const complex&);
bool equals(const complex &) const;
private:
int R; int I;
friend ostream& operator<<(ostream&, const complex&);
friend complex& complex: :operator+=(const complex&);
friend complex operator+(complex, const complex&);
};
complex::complex()
{
R = 0; I = 0;
}
complex::complex(int real, int imag)
{
R = real; I = imag;
}
void complex: :output(ostream& o) const
{
o << R << " " << I << "i" ;
}
bool complex::equals(const complex& C) const
{
return ( (R == C.R) && (I == C.I));
}
complex& complex::conjugate()
{
I = -I; return *this;
}
complex& complex: :plusequal(const complex& c)
{
R += c.R; I += c.I;
}
ostream& operator<<(ostream& LHS, const complex& RHS)
{
LHS << RHS.R << " " << RHS.I << "i";
return LHS;
}
complex operator+(complex a, const complex& b)
{
return a.plusequal(b);
complex
and on and on and on....
Hope this helps you get started.
[ 26 October 2001: Message edited by: UprightMan ]
[ 26 October 2001: Message edited by: UprightMan ]
shaggy112
10-27-2001, 10:59 AM
thank you.
that is a big help to get me started!
shaggy112
10-27-2001, 03:58 PM
I am having a problem with this.
When I try to use my class in main....Visual C++ 6.0 tells me "Ambiguous call to overloaded function". I do not see my error.
Excerpt from main
complex r;
complex s;
Excerpt from complex.cpp
complex::complex(int realNum, int complexNum) {
setReal(realNum);
setComplex(complexNum);
}
// grab the real number
int complex::getReal() const {
return realNumVal;
}
// grab the complex number
int complex::getComplex() const {
return complexNumVal;
}
//set the real number
void complex::setReal(int realNum) {
realNumVal = realNum;
}
//set the complex number
void complex::setComplex(int complexNum) {
complexNumVal = complexNum;
}
header file for class
class complex {
public:
complex();
complex(int realNum = 0, int complexNum = 0);
complex addReal(const complex &r) const;
complex multiply(const complex &r) const;
void insert(ostream &sout) const;
void extract(istream &sin);
int getReal() const;
int getComplex() const;
void setReal(int realNum);
void setComplex(int complexNum);
private:
int realNumVal;
int complexNumVal;
};
complex operator+(const complex &r, const complex &s);
complex operator-(const complex &r, const complex &s);
ostream& operator<<(ostream &sout, const complex &s);
istream& operator>>(istream &sin, complex &r);
#endif
Thanks for any help!
bwkaz
10-27-2001, 05:16 PM
Excerpt from main
complex r;
complex s;
header file for class
class complex {
public:
complex();
complex(int realNum = 0, int complexNum = 0);
[/qb]<HR></BLOCKQUOTE>
The problem is the two lines right at the end here. The compiler, when you overload a function, can only tell the difference between two functions by their signature -- the number and type of the arguments. Well, unfortunately, the "default value" extension to C++ (at least, I don't think it's standard) is not part of the argument type. The compiler treats the declaration as having no arguments, or two. So it clashes with the no-argument constructor.
The reason the compiler does this is that it can't tell at compile time (without reading your mind ;) ) WHICH no-argument constructor you want, the one with the default arguments, or the one with no arguments. Keep in mind, these could do ENTIRELY different things. So it gives you that error. Either get rid of the empty constructor or get rid of the defaults in the other one, so that two arguments are required.
[ 27 October 2001: Message edited by: bwkaz ]
pinoy
10-27-2001, 06:06 PM
The problem is the two lines right at the end here. The compiler, when you overload a function, can only tell the difference between two functions by their signature -- the number and type of the arguments. Well, unfortunately, the "default value" extension to C++ (at least, I don't think it's standard) is not part of the argument type. The compiler treats the declaration as having no arguments, or two. So it clashes with the no-argument constructor.
The reason the compiler does this is that it can't tell at compile time (without reading your mind ) WHICH no-argument constructor you want, the one with the default arguments, or the one with no arguments. Keep in mind, these could do ENTIRELY different things. So it gives you that error. Either get rid of the empty constructor or get rid of the defaults in the other one, so that two arguments are required.
Default arguments are part of the standard.
To solve this, remove the default constructor (one with no arguments). As the one with the defaulted arguments already handle that case.
bwkaz
10-28-2001, 11:41 AM
OK, it is standard, never mind. But yeah.