IvoSilva

[PROG2] Ficha 1 | Exercício 4

Mar 10th, 2012
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.44 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #define PI 3.14159265
  5. typedef struct ComplexRect
  6. {
  7.     float real , imag ;
  8. } ComplexRect ;
  9. typedef struct ComplexAng
  10. {
  11.     float mod , ang ;
  12. } ComplexAng ;
  13. void rect_to_polar(ComplexRect *nrects , ComplexAng *nangs , int n)
  14. {
  15.     int i ;
  16.     for (i = 0 ; i < n ; i++)
  17.     {
  18.         nangs[i].mod = sqrt(nrects[i].imag * nrects[i].imag + nrects[i].real * nrects[i].real) ;
  19.         nangs[i].ang = atan2(nrects[i].imag , nrects[i].real) * 180 / PI ;
  20.     }
  21. }
  22. int main()
  23. {
  24.     int i , n ;
  25.     ComplexRect *nrects ;
  26.     ComplexAng *nangs ;
  27.     printf ("Quantidade de números complexos a converter : ") ;
  28.     scanf ("%d" , &n) ;
  29.     printf ("Insira o coeficiente das partes real e imaginária :\n") ;
  30.     nrects = (ComplexRect*) malloc (n * sizeof(ComplexRect)) ;
  31.     if (nrects == NULL)
  32.     {
  33.         system ("clear") ;
  34.         printf ("Memória não disponível !\n") ;
  35.         return -1 ;
  36.     }
  37.     for (i = 0 ; i < n ; i++)
  38.     {
  39.         printf ("... do %dº número complexo: " , i + 1) ;
  40.         scanf ("%f %f" , &nrects[i].real , &nrects[i].imag) ;
  41.     }
  42.     nangs = (ComplexAng*) malloc (n * sizeof(ComplexAng)) ;
  43.     rect_to_polar(nrects , nangs , n) ;
  44.     printf ("As respectivas representações polares são :\n") ;
  45.     for (i= 0 ; i < n ; i++) printf ("mod=%.2f   ang=%.f\n" , nangs[i].mod , nangs[i].ang) ;
  46.     free (nrects) ;
  47.     free (nangs) ;
  48.     return 0 ;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment