complex number

Reply

Join Date: Nov 2009
Posts: 5
Reputation: nick30266 is an unknown quantity at this point 
Solved Threads: 0
nick30266 nick30266 is offline Offline
Newbie Poster

complex number

 
0
  #1
Nov 18th, 2009
the program is ok(i think)but the data is fixed.i want it able to input your own data.i tried cin>>c1.real>>c1.image;
cin>>c2.real>>c2.image;
but it cannot use them because they are private...

  1. #include<iostream>
  2. using namespace std;
  3. class complex
  4. {
  5. public:
  6.  
  7. complex(){real=0;image=0;}
  8.  
  9. complex(double r,double i){real=r;image=i;}
  10.  
  11. complex complex_add(complex &c2);
  12.  
  13. complex complex_sub(complex &c2);
  14.  
  15. complex complex_mult(complex &c2);
  16.  
  17. complex complex_div(complex &c2);
  18. void display();
  19.  
  20. private:
  21. double real;
  22. double image;
  23. };
  24.  
  25.  
  26. complex complex::complex_add(complex &c2)
  27. {
  28. complex c;
  29.  
  30. c.real=real+c2.real;
  31.  
  32. c.image=image+c2.image;
  33.  
  34. return c;
  35. }
  36.  
  37. complex complex::complex_sub(complex &c2)
  38.  
  39. {
  40. complex d;
  41.  
  42. d.real=real-c2.real;
  43.  
  44. d.image=image-c2.image;
  45.  
  46. return d;
  47. }
  48.  
  49. complex complex::complex_mult(complex &c2)
  50. {
  51. complex f;
  52.  
  53. f.real=real*c2.real;
  54.  
  55. f.image=image*c2.image;
  56.  
  57. return f;
  58. }
  59.  
  60. complex complex::complex_div(complex &c2)
  61. {
  62. complex g;
  63. g.real=real/c2.real;
  64. g.image=image/c2.image;
  65. return g;
  66. }
  67.  
  68.  
  69. void complex::display()
  70. {
  71. cout<<"("<<real<<","<<image<<"i)"<<endl;
  72. }
  73.  
  74.  
  75. int main()
  76. {
  77. complex c1(2,5),c2(4,9),c3; //i want input my own complex number.
  78. c3=c1.complex_add(c2);
  79. cout<<"c1=";c1.display();
  80. cout<<"c2=";c2.display();
  81. cout<<"c1+c2=";c3.display();
  82.  
  83. c3=c1.complex_sub(c2);
  84. cout<<"c1=";c1.display();
  85. cout<<"c2=";c2.display();
  86. cout<<"c1-c2=";c3.display();
  87.  
  88. c3=c1.complex_mult(c2);
  89. cout<<"c1=";c1.display();
  90. cout<<"c2=";c2.display();
  91. cout<<"c1*c2=";c3.display();
  92.  
  93. c3=c1.complex_div(c2);
  94. cout<<"c1=";c1.display();
  95. cout<<"c2=";c2.display();
  96. cout<<"c1/c2=";c3.display();
  97.  
  98. return 0;
  99. }
Last edited by Nick Evan; Nov 19th, 2009 at 3:05 pm. Reason: Removed code-tags, added code-tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 1,823
Reputation: jonsca is a glorious beacon of light jonsca is a glorious beacon of light jonsca is a glorious beacon of light jonsca is a glorious beacon of light jonsca is a glorious beacon of light 
Solved Threads: 227
Sponsor
jonsca jonsca is offline Offline
Posting Virtuoso
 
0
  #2
Nov 19th, 2009
Take in 2 doubles from the user and then use your constructor to create a new object.
  1. double r,im;
  2. cin >> r >> im;
  3. complex num(r,im);
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 409
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 77
StuXYZ StuXYZ is offline Offline
Posting Pro in Training
 
0
  #3
Nov 19th, 2009
I am sorry I don't like jonsca's solution, it has ugly temporary variables, that aren't necessary.

The better way to do this (IMHO) is to add two operators. One is operator<< and the other is operator>>

  1.  
  2. std::istream& operator<<(std::istream& FX,complex& A)
  3. {
  4. A.read(FX);
  5. return FX;
  6. }
  7. std::ostream& operator>>(std::ostream& FX,const complex& A)
  8. {
  9. A.write(FX);
  10. return FX;
  11. }

Now those two functions allow your to write
  1.  
  2. complex c;
  3. std::cin>>c;
  4. std::cout<<"You entered "<<c<<std::endl;
  5. }

The only thing you have to write is read/write
  1. void complex::write(std::ostream& FX) const
  2. {
  3. FX<<r<<" "<<i;
  4. }
  5. // AND
  6. void complex::read(std::ostream& FX)
  7. {
  8. FX>>r>>i;
  9. }

I would add checking etc, to read and allow stuff like 4+i5 etc. I normally don't put the endl within the write function, because it is normal to allow a chained set of functions e.g.
std::cout<<"Complex to add are "<<cA<<" + "<<cB<<std::endl;
Last edited by StuXYZ; Nov 19th, 2009 at 2:18 am.
experience is the most expensive way to learn anything
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,925
Reputation: firstPerson is a name known to all firstPerson is a name known to all firstPerson is a name known to all firstPerson is a name known to all firstPerson is a name known to all firstPerson is a name known to all 
Solved Threads: 254
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Posting Virtuoso
 
0
  #4
Nov 19th, 2009
Another way is to make a get/set functions.
Go ahead and try to solve  this  euler problem, if u can. Check with me for hints and 
answers. Good Luck
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 434 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC