RenHao

ComplexNumberComputing

Oct 1st, 2013
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.86 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #include <math.h>       /* atan */
  5.  
  6. #define PI 3.14159265
  7. /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  8.  
  9. typedef struct ComplexNumber
  10. {
  11.     /*Aim to calculate complex number a+bi*/
  12.     float a ;  //real part
  13.     float bi;  //imaginary part
  14.     float r ;  //r = |a+bi| = sqrt(pow(a,2)+pow(bi,2))   which means the distance of the point Z to the Origin .
  15.     float theta ; // 1 (degree) = PI / 180 (radian)
  16. }Complex ;
  17.  
  18. void InitialCom(Complex *Z, float R, float I)
  19. {
  20.     Z->a  = R ;
  21.     Z->bi = I ;
  22.     Z->r  = sqrt( ( pow(R,2) + pow(I,2) ) ) ;
  23.     Z->theta = atan(I/R) * 180 / PI; // theta = arctan (y/x)
  24. }
  25.  
  26. Complex PolarToXY(float r, float degree)
  27. {
  28.     /*Transformimg Polar coordinate to XY coordinate*/
  29.    
  30.     Complex Z;
  31.     float radian ;
  32.    
  33.     radian = degree * PI / 180 ;
  34.    
  35.     Z.a  = r * cos( radian );
  36.     Z.bi = r * sin( radian );
  37.    
  38.     return Z;
  39. }
  40.  
  41. Complex AddComplex(Complex A,Complex B)
  42. {
  43.    
  44.     /*To do an addition with A and B*/
  45.    
  46.     Complex Result ;
  47.     Result.a = A.a + B.a ;
  48.     Result.bi = A.bi + B.bi ;
  49.    
  50.     return Result ;
  51. }
  52.  
  53. Complex SubComplex(Complex A,Complex B)
  54. {
  55.    
  56.     /*To do a subtraction with A and B*/
  57.    
  58.     Complex Result ;
  59.     Result.a = A.a - B.a ;
  60.     Result.bi = A.bi - B.bi ;
  61.    
  62.     return Result ;
  63. }
  64.  
  65. Complex MulComplex(Complex A,Complex B)
  66. {
  67.    
  68.     /*To do a subtraction with A and B*/
  69.    
  70.     Complex Result ;
  71.     Result.a =  (A.a * B.a - A.bi * B.bi) ;
  72.     Result.bi = (A.a * B.bi + A.bi * B.a) ;
  73.    
  74.     return Result ;
  75. }
  76.  
  77. Complex DivComplex(Complex A,Complex B)
  78. {
  79.    
  80.     /*To do a subtraction with A and B  --> A/B */
  81.     /*c - di is the complex conjugate of the denominator c + di.
  82.     The real part c and the imaginary part d of the denominator must not both be zero for division to be defined.*/
  83.    
  84.     Complex Result ;
  85.     Result.a  = (A.a * B.a + A.bi * B.bi) /  (pow(B.a,2) + pow(B.bi,2)) ;
  86.     Result.bi = (A.a * B.bi - A.bi * B.a) /  (pow(B.a,2) + pow(B.bi,2)) ;
  87.    
  88.     return Result ;
  89. }
  90.  
  91. /*
  92. void swap(int *a,int *b)
  93. {
  94.     *a=*a^*b;
  95.     *b=*a^*b;
  96.     *a=*a^*b;
  97. }
  98. */
  99. int main(int argc, char *argv[])
  100. {
  101.     Complex Z1 , Z2 ;
  102.     InitialCom(&Z1, 1.0, 1.0);
  103.     InitialCom(&Z2, 1.0, 1.732);
  104.    
  105.     printf("Z1 = (%.2f) + (%.2fi)   |Z1| = %.2f  angle = %.2f\n",Z1.a,Z1.bi,Z1.r,Z1.theta);
  106.     printf("Z2 = (%.2f) + (%.2fi)   |Z2| = %.2f  angle = %.2f\n",Z2.a,Z2.bi,Z2.r,Z2.theta);
  107. /* 
  108.     int x=5,y=3;
  109.    
  110.     printf("\n\n %d XOR %d = %d     ",x,y,x=x^y);
  111.     printf("     %d XOR %d = %d     ",x,y,y=x^y);
  112.     printf("     %d XOR %d = %d \n\n",x,y,x=x^y);
  113. */ 
  114.     Complex Z3 ;
  115.     InitialCom(&Z3, 0, 0);
  116.    
  117.     Z3 = AddComplex(Z1,Z2);
  118.     printf("Z1 + Z2 = (%.2f) + (%.2fi)\n",Z3.a,Z3.bi);
  119.    
  120.     Z3 = SubComplex(Z1,Z2);
  121.     printf("Z1 - Z2 = (%.2f) + (%.2fi)\n",Z3.a,Z3.bi);
  122.    
  123.     Z3 = MulComplex(Z1,Z2);
  124.     printf("Z1 * Z2 = (%.2f) + (%.2fi)\n",Z3.a,Z3.bi);
  125.    
  126.     Z3 = DivComplex(Z1,Z2);
  127.     printf("Z1 / Z2 = (%.2f) + (%.2fi)\n",Z3.a,Z3.bi);
  128.    
  129.    
  130.     return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment