Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h> /* atan */
- #define PI 3.14159265
- /* run this program using the console pauser or add your own getch, system("pause") or input loop */
- typedef struct ComplexNumber
- {
- /*Aim to calculate complex number a+bi*/
- float a ; //real part
- float bi; //imaginary part
- float r ; //r = |a+bi| = sqrt(pow(a,2)+pow(bi,2)) which means the distance of the point Z to the Origin .
- float theta ; // 1 (degree) = PI / 180 (radian)
- }Complex ;
- void InitialCom(Complex *Z, float R, float I)
- {
- Z->a = R ;
- Z->bi = I ;
- Z->r = sqrt( ( pow(R,2) + pow(I,2) ) ) ;
- Z->theta = atan(I/R) * 180 / PI; // theta = arctan (y/x)
- }
- Complex PolarToXY(float r, float degree)
- {
- /*Transformimg Polar coordinate to XY coordinate*/
- Complex Z;
- float radian ;
- radian = degree * PI / 180 ;
- Z.a = r * cos( radian );
- Z.bi = r * sin( radian );
- return Z;
- }
- Complex AddComplex(Complex A,Complex B)
- {
- /*To do an addition with A and B*/
- Complex Result ;
- Result.a = A.a + B.a ;
- Result.bi = A.bi + B.bi ;
- return Result ;
- }
- Complex SubComplex(Complex A,Complex B)
- {
- /*To do a subtraction with A and B*/
- Complex Result ;
- Result.a = A.a - B.a ;
- Result.bi = A.bi - B.bi ;
- return Result ;
- }
- Complex MulComplex(Complex A,Complex B)
- {
- /*To do a subtraction with A and B*/
- Complex Result ;
- Result.a = (A.a * B.a - A.bi * B.bi) ;
- Result.bi = (A.a * B.bi + A.bi * B.a) ;
- return Result ;
- }
- Complex DivComplex(Complex A,Complex B)
- {
- /*To do a subtraction with A and B --> A/B */
- /*c - di is the complex conjugate of the denominator c + di.
- The real part c and the imaginary part d of the denominator must not both be zero for division to be defined.*/
- Complex Result ;
- Result.a = (A.a * B.a + A.bi * B.bi) / (pow(B.a,2) + pow(B.bi,2)) ;
- Result.bi = (A.a * B.bi - A.bi * B.a) / (pow(B.a,2) + pow(B.bi,2)) ;
- return Result ;
- }
- /*
- void swap(int *a,int *b)
- {
- *a=*a^*b;
- *b=*a^*b;
- *a=*a^*b;
- }
- */
- int main(int argc, char *argv[])
- {
- Complex Z1 , Z2 ;
- InitialCom(&Z1, 1.0, 1.0);
- InitialCom(&Z2, 1.0, 1.732);
- printf("Z1 = (%.2f) + (%.2fi) |Z1| = %.2f angle = %.2f\n",Z1.a,Z1.bi,Z1.r,Z1.theta);
- printf("Z2 = (%.2f) + (%.2fi) |Z2| = %.2f angle = %.2f\n",Z2.a,Z2.bi,Z2.r,Z2.theta);
- /*
- int x=5,y=3;
- printf("\n\n %d XOR %d = %d ",x,y,x=x^y);
- printf(" %d XOR %d = %d ",x,y,y=x^y);
- printf(" %d XOR %d = %d \n\n",x,y,x=x^y);
- */
- Complex Z3 ;
- InitialCom(&Z3, 0, 0);
- Z3 = AddComplex(Z1,Z2);
- printf("Z1 + Z2 = (%.2f) + (%.2fi)\n",Z3.a,Z3.bi);
- Z3 = SubComplex(Z1,Z2);
- printf("Z1 - Z2 = (%.2f) + (%.2fi)\n",Z3.a,Z3.bi);
- Z3 = MulComplex(Z1,Z2);
- printf("Z1 * Z2 = (%.2f) + (%.2fi)\n",Z3.a,Z3.bi);
- Z3 = DivComplex(Z1,Z2);
- printf("Z1 / Z2 = (%.2f) + (%.2fi)\n",Z3.a,Z3.bi);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment