dzungchaos

C++ "Cộng trừ số phức + đa năng hóa toán tử"

Nov 6th, 2019
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. typedef struct SP{
  5.     double rel;
  6.     double img;
  7. } SP;
  8.  
  9. SP setSP(int _rel, int _img);
  10. void display(SP c);
  11. SP operator + (SP c1, SP c2); // overload +
  12. SP operator - (SP c1, SP c2); // overload -
  13.  
  14. int main(){
  15.     SP c1, c2, c3, c4;
  16.     c1 = setSP(1, -2);
  17.     c2 = setSP(3, 4);
  18.     c3 = c1 + c2;
  19.     printf("Tong hai so: "); display(c3);
  20.     c4 = c1 - c2;
  21.     printf("Hieu hai so: "); display(c4);
  22.     return 0;
  23. }
  24.  
  25. SP setSP(int _rel, int _img){
  26.     SP c;
  27.     c.rel = _rel; c.img = _img;
  28.     return c;
  29. }
  30.  
  31. void display(SP c){
  32.     cout << c.rel <<" + i*" << c.img << endl;
  33. }
  34.  
  35. SP operator + (SP c1, SP c2){
  36.     SP c;
  37.     c.rel = c1.rel + c2.rel;
  38.     c.img = c1.img + c2.img;
  39.     return c;
  40. }
  41.  
  42. SP operator - (SP c1, SP c2){
  43.     SP c;
  44.     c.rel = c1.rel - c2.rel;
  45.     c.img = c1.img - c2.img;
  46.     return c;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment