Advertisement
STANAANDREY

ad nr compl 1

Oct 30th, 2019
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. using namespace std;
  6.  
  7. typedef struct
  8. {
  9.     float real, im;
  10. } complexNr;
  11.  
  12. int main()
  13. {
  14.     char s[100], *p, sep[] = " +-i";
  15.     complexNr a, b, sum, prod;
  16.  
  17.     cout << "a si b - complexe forma:x+yi" << endl;
  18.     cout << "a este:" << endl;
  19.     gets(s);
  20.     int pozsim = 0;
  21.     for (int i = 1; s[i] && !pozsim; i++)
  22.         if (s[i] == '-')
  23.             pozsim = i;
  24.  
  25.     if (s[0] == '-')
  26.         a.real = -1;
  27.     else
  28.         a.real = 1;
  29.     p = strtok(s, sep);
  30.     a.real *= atof(p);
  31.     p = strtok(NULL, sep);
  32.     a.im = atof(p);
  33.     if (pozsim)
  34.         a.im = -a.im;
  35.  
  36.     cout << "b este:" << endl;
  37.     gets(s);
  38.     pozsim = 0;
  39.     for (int i = 1; s[i] && !pozsim; i++)
  40.         if (s[i] == '-')
  41.             pozsim = i;
  42.     if (s[0] == '-')
  43.         b.real = -1;
  44.     else
  45.         b.real = 1;
  46.     p = strtok(s, sep);
  47.     b.real *= atof(p);
  48.     p = strtok(NULL, sep);
  49.     b.im = atof(p);
  50.     if (pozsim)
  51.         b.im = -b.im;
  52.  
  53.     sum.real = a.real + b.real;
  54.     sum.im = a.im + b.im;
  55.     cout << "suma: ";
  56.     cout << sum.real;
  57.     if (sum.im >= 0)
  58.         cout << '+';
  59.     cout << sum.im << 'i' << endl;
  60.  
  61.     prod.real = a.real * b.real - a.im * b.im;
  62.     prod.im = a.real * b.im + a.im * b.real;
  63.     cout << "produs: ";
  64.     cout << prod.real;
  65.     if (prod.im >= 0)
  66.         cout << '+';
  67.     cout << prod.im << 'i';//*/
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement