fork download
  1. #include<bits/stdc++.h>
  2. #define IOF ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  3. #define ll long long
  4. #define pb public:
  5. #define pr private:
  6. using namespace std;
  7. class Ccomplex{
  8. float real , imag;
  9. public:
  10. void print(){
  11. cout << real << (imag < 0 ? " - " : " + ") << abs(imag) << 'i' << '\n';
  12. }
  13. void setReal(float r){
  14. real = r;
  15. }
  16. void setImag(float i){
  17. imag = i;
  18. }
  19. float getReal(){
  20. return real;
  21. }
  22. float getImag(){
  23. return imag;
  24. }
  25. Ccomplex add(Ccomplex c){
  26. Ccomplex tmp;
  27. tmp.real = real + c.getReal();
  28. tmp.imag = imag + c.getImag();
  29. return tmp;
  30. }
  31. Ccomplex sub(Ccomplex c){
  32. Ccomplex tmp;
  33. tmp.real = real - c.getReal();
  34. tmp.imag = imag - c.getImag();
  35. return tmp;
  36. }
  37. };
  38.  
  39. int main() {
  40. Ccomplex c1 , c2 , res;
  41. c1.setImag(4) ;
  42. c1.setReal(3);
  43. c2.setImag(5) ;
  44. c2.setReal(6);
  45. c1.print();
  46. c2.print();
  47. res = c1.add(c2);
  48. res.print();
  49. res = c1.sub(c2);
  50. res.print();
  51. return 0;
  52. }
  53.  
Success #stdin #stdout 0.01s 5272KB
stdin
Standard input is empty
stdout
3 + 4i
6 + 5i
9 + 9i
-3 - 1i