fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct SoPhuc {
  5. double thuc, ao;
  6. };
  7.  
  8. SoPhuc Cong(SoPhuc a, SoPhuc b) {
  9. return {a.thuc + b.thuc, a.ao + b.ao};
  10. }
  11.  
  12. SoPhuc Tru(SoPhuc a, SoPhuc b) {
  13. return {a.thuc - b.thuc, a.ao - b.ao};
  14. }
  15.  
  16. SoPhuc Nhan(SoPhuc a, SoPhuc b) {
  17. return {(a.thuc * b.thuc) - (a.ao * b.ao), (a.thuc * b.ao) + (b.thuc * a.ao)};
  18. }
  19.  
  20. void HienThi(SoPhuc a) {
  21. if (a.thuc != 0) cout << a.thuc;
  22. if (a.ao != 0) {
  23. if (a.ao == -1) {
  24. cout << (a.thuc != 0 ? " - i" : "-i");
  25. } else if (a.ao == 1) {
  26. cout << (a.thuc != 0 ? " + i" : "i");
  27. } else {
  28. if (a.thuc != 0) {
  29. cout << (a.ao > 0 ? " + " : " - ") << abs(a.ao) << "i";
  30. } else {
  31. cout << a.ao << "i";
  32. }
  33. }
  34. } else if (a.thuc == 0) {
  35. cout << "0";
  36. }
  37. cout << '\n';
  38. }
  39.  
  40. void Input(SoPhuc &a) {
  41. cin >> a.thuc >> a.ao;
  42. }
  43. int main() {
  44. int n;
  45. cin >> n;
  46. while (n--) {
  47. SoPhuc a, b;
  48. Input(a);
  49. Input(b);
  50.  
  51. HienThi(Cong(a, b));
  52. HienThi(Tru(a, b));
  53. HienThi(Nhan(a, b));
  54. }
  55. return 0;
  56. }
  57.  
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout
Standard output is empty