| | |
Complex Number Calculator in C++
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2006
Posts: 6
Reputation:
Solved Threads: 0
Hi,everyone
I have to write a calculator which can + , - , * , / , + = ,- = , * = , /= ,++,-- do this operations with operator overloading
i wrote that code according to some topics in this forum but it gives me a error
How can i fix this?
Also i have to write a display function to print complex numbers according to my code i can not do it please also help me in this problem...
Have a good day...
P.S. i have just registered yet sorry if i did sth wrong...:rolleyes:
I have to write a calculator which can + , - , * , / , + = ,- = , * = , /= ,++,-- do this operations with operator overloading
C++ Syntax (Toggle Plain Text)
#include<iostream> using namespace std; class complexNumber { private: double realPart; double imaginaryPart; public: complexNumber(){realPart=0.0; imaginaryPart=0.0;} ~complexNumber(){cout<<"Destructor do it's work..!"<<endl;} double get_real(){return realPart;} double get_imaginary(){return imaginaryPart;} void set_real(double r){realPart=r;} void set_imaginary(double i){imaginaryPart=i;} complexNumber operator+(const complexNumber& c); complexNumber operator-(const complexNumber& c); }; void main() { complexNumber c1,c2,c3; c1.set_real(3.4); c1.set_imaginary(4.5); c2.set_real(5.6); c2.set_imaginary(6.2); c3= c1 + c2; cout<< c1 <<endl<< c2 <<endl<< c3 <<endl; }
•
•
•
•
error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class complexNumber' (or there is no acceptable conversion)
Also i have to write a display function to print complex numbers according to my code i can not do it please also help me in this problem...
Have a good day...
P.S. i have just registered yet sorry if i did sth wrong...:rolleyes:
•
•
•
•
Originally Posted by Roc
Hi,everyone
I have to write a calculator which can + , - , * , / , + = ,- = , * = , /= ,++,-- do this operations with operator overloading
i wrote that code according to some topics in this forum but it gives me a errorC++ Syntax (Toggle Plain Text)
#include<iostream> using namespace std; class complexNumber { private: double realPart; double imaginaryPart; public: complexNumber(){realPart=0.0; imaginaryPart=0.0;} ~complexNumber(){cout<<"Destructor do it's work..!"<<endl;} double get_real(){return realPart;} double get_imaginary(){return imaginaryPart;} void set_real(double r){realPart=r;} void set_imaginary(double i){imaginaryPart=i;} complexNumber operator+(const complexNumber& c); complexNumber operator-(const complexNumber& c); }; void main() { complexNumber c1,c2,c3; c1.set_real(3.4); c1.set_imaginary(4.5); c2.set_real(5.6); c2.set_imaginary(6.2); c3= c1 + c2; cout<< c1 <<endl<< c2 <<endl<< c3 <<endl; }
How can i fix this?
Also i have to write a display function to print complex numbers according to my code i can not do it please also help me in this problem...
Have a good day...
P.S. i have just registered yet sorry if i did sth wrong...:rolleyes:
The method for overloading the << operator is slightly different. You have to be careful when doing that.
Also this:
c1.set_real(3.4);
should be:
c1.set_real(3,4); <-- separated with a comma.
And exchange void main with int main. Carry on...
Well without actually handing out the code for a complex number class the following thread should make things clearer.
It's a fraction class but you should get an idea of what you need to do. It can probably simplified as well.
http://www.daniweb.com/techtalkforums/thread44082.html
It's a fraction class but you should get an idea of what you need to do. It can probably simplified as well.
http://www.daniweb.com/techtalkforums/thread44082.html
•
•
Join Date: Apr 2006
Posts: 6
Reputation:
Solved Threads: 0
i have looked that topic but i couldn't get the main idea of overloading "<<" operator...
Can you give me a little code for cout complexNumbers from my code???
This code is working but can not print my complex numbers...
Can you edit this code's "<<" operator for me???or give me an idea please...
Can you give me a little code for cout complexNumbers from my code???
This code is working but can not print my complex numbers...
Can you edit this code's "<<" operator for me???or give me an idea please...
C++ Syntax (Toggle Plain Text)
#include<iostream> using namespace std; class complexNumber { private: double realPart; double imaginaryPart; public: complexNumber(){realPart=0.0; imaginaryPart=0.0;} ~complexNumber(){cout<<"Destructor do it's work..!"<<endl;} double get_real(){return realPart;} double get_imaginary(){return imaginaryPart;} void set_real(double r){realPart=r;} void set_imaginary(double i){imaginaryPart=i;} friend ostream &operator<<( ostream &output, complexNumber &c ) { return output << c.realPart << " / " << c.imaginaryPart; } complexNumber operator+(complexNumber c) { complexNumber temp; int temp_realPart,temp_imaginaryPart; temp_realPart= realPart * c.get_imaginary() + c.get_real()* imaginaryPart; temp_imaginaryPart = imaginaryPart * c.get_imaginary(); temp.set_real(temp_realPart); temp.set_imaginary(temp_imaginaryPart); return temp; } // complexNumber operator-(const complexNumber& c); }; int main() { complexNumber c1,c2,c3; c1.set_real(3.4); c1.set_imaginary(4.5); c2.set_real(5.6); c2.set_imaginary(6.2); c3= c1 + c2; cout<<c3<<endl; return 0; }
You've more or less got it. I've just highlighted the stuff I've changed. Now you have to finish the methods for overloading the other operators.
Carry on...
Carry on...
#include<iostream>
using namespace std;
class complexNumber
{
private:
double realPart;
double imaginaryPart;
public:
complexNumber(){realPart=0.0; imaginaryPart=0.0;}
~complexNumber(){cout<<"Destructor do it's work..!"<<endl;}
double get_real(){return realPart;}
double get_imaginary(){return imaginaryPart;}
void set_real(double r){realPart=r;}
void set_imaginary(double i){imaginaryPart=i;}
friend ostream &operator<<( ostream &output, complexNumber &c )
{
return output << c.realPart << " + " << c.imaginaryPart <<"i";
}
complexNumber operator+(complexNumber c)
{
complexNumber temp;
double temp_realPart,temp_imaginaryPart;
temp_realPart= realPart + c.get_real();
temp_imaginaryPart = imaginaryPart + c.get_imaginary();
temp.set_real(temp_realPart);
temp.set_imaginary(temp_imaginaryPart);
return temp;
}
// complexNumber operator-(const complexNumber& c);
};
int main()
{
complexNumber c1,c2,c3;
c1.set_real(3.4);
c1.set_imaginary(4.5);
c2.set_real(5.6);
c2.set_imaginary(6.2);
c3= c1 + c2;
cout<< c3<<endl;
cin.get();
return 0;
} Just one last thing. Multiplication and division of complex numbers is non-intuitive. This is how I was told it should be done:-
http://img142.imageshack.us/img142/5735/c4um.png
http://img142.imageshack.us/img142/5735/c4um.png
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Please Help!
- Next Thread: help w/ dictionary look-up program by using Associative_Array
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






