7_61.cpp
上传用户:zipjojo
上传日期:2009-07-20
资源大小:70k
文件大小:2k
源码类别:

文章/文档

开发平台:

C/C++

  1. #include<iostream.h>
  2. class complex
  3. {
  4. float a,b;
  5. public:
  6. complex(){a=0.0;b=0.0;}
  7. complex(float x,float y) {a=x;b=y;}
  8. void Add(complex& comp1,complex& comp2);
  9. void Sub(complex& comp1,complex& comp2);
  10. void Mul(complex& comp1,complex& comp2);
  11. void Div(complex& comp1,complex& comp2);
  12. void disp()
  13. {
  14. if(b>0)
  15. cout<<a<<"+"<<b<<"i"<<endl;
  16. else
  17. cout<<a<<"-"<<-b<<"i"<<endl;
  18. }
  19. };
  20. void complex::Add(complex& comp1,complex& comp2)
  21. {
  22. float x=comp1.a+comp2.a;
  23. float y=comp1.b+comp2.b;
  24. a=x;
  25. b=y;
  26. cout<<"复数s1、s2之和为:";
  27. }
  28. void complex::Sub(complex& comp1,complex& comp2)
  29. {
  30. float x=comp1.a-comp2.a;
  31. float y=comp1.b-comp2.b;
  32. a=x;
  33. b=y;
  34. cout<<"复数s1、s2之差为:";
  35. }
  36. void complex::Mul(complex& comp1,complex& comp2)
  37. {
  38. float x=comp1.a*comp2.a-comp1.b*comp2.b;
  39. float y=comp1.a*comp2.b+comp1.b*comp2.a;
  40. a=x;
  41. b=y;
  42. cout<<"复数s1、s2之积为:";
  43. }
  44. void complex::Div(complex& comp1,complex& comp2)
  45. {
  46. if((comp2.a*comp2.a+comp2.b*comp2.b)!=0)
  47. {
  48.     float x=(comp1.a*comp2.a+comp1.b*comp2.b)/(comp2.a*comp2.a+comp2.b*comp2.b);
  49.     float y=(comp1.b*comp2.a-comp1.a*comp2.b)/(comp2.a*comp2.a+comp2.b*comp2.b);
  50.     a=x;
  51.     b=y;
  52. cout<<"复数s1、s2之商为:";
  53.         if(b>0)
  54.   cout<<a<<"+"<<b<<"i"<<endl;
  55.     else
  56.   cout<<a<<"-"<<-b<<"i"<<endl;
  57. }
  58. else
  59. cout<<"除数为零,不能相除!";
  60. }
  61. void main()
  62. {
  63. complex s1(1,2),s2(3,4),s3,s4,s5,s6;
  64. cout<<"复数s1:";
  65. s1.disp();
  66. cout<<"复数s2:";
  67. s2.disp();
  68. s3.Add(s1,s2);
  69. s3.disp();
  70. s4.Sub(s1,s2);
  71. s4.disp();
  72. s5.Mul(s1,s2);
  73. s5.disp();
  74. s6.Div(s1,s2);
  75. cout<<endl;
  76. }