fork download
  1. #include<iostream>
  2. using namespace std;
  3. class Complex{
  4. private:
  5. float real;
  6. float imaginary;
  7. public:
  8. void set_values(float a,float b)
  9. {
  10. real=a;
  11. imaginary=b;
  12.  
  13. }
  14. float get_real(){
  15. return real;
  16. }
  17. float get_imaginary(){
  18. return imaginary;
  19. }
  20. void display(){
  21. if(imaginary<0){
  22. cout<<real<<"-"<<abs(imaginary)<<"i"<<endl;
  23. }
  24. else {
  25. cout<<real<<"+"<<imaginary<<"i"<<endl;
  26. }
  27. }
  28.  
  29. };
  30. class calculation{
  31. public:
  32. Complex sum(Complex a,Complex b){
  33. Complex S;
  34. float x,y;
  35. x=a.get_real()+b.get_real();
  36. y=a.get_imaginary()+b.get_imaginary();
  37. S.set_values(x,y);
  38. return S;
  39.  
  40.  
  41. }
  42. Complex difference(Complex a,Complex b){
  43. Complex d;
  44. float x,y;
  45. x=a.get_real()-b.get_real();
  46. y=a.get_imaginary()-b.get_imaginary();
  47. d.set_values(x,y);
  48. return d;
  49.  
  50.  
  51. }
  52. };
  53.  
  54. int main(){
  55. cout<<"Enter real and imaginary parts of first complex number :";
  56. float a,b;
  57. cin>>a>>b;
  58. Complex A,B,sum,diff;
  59. A.set_values(a,b);
  60. cout<<"Enter real and imaginary parts of second complex number :";
  61. cin>>a>>b;
  62. B.set_values(a,b);
  63. calculation Value;
  64. sum=Value.sum(A,B);
  65. diff=Value.difference(A,B);
  66. sum.display();
  67. diff.display();
  68.  
  69.  
  70. return 0;
  71. }
  72.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Enter real and imaginary parts of first complex number :Enter real and imaginary parts of second complex number :-9.65942e-17+1.50219e-41i
0+0i