Created
July 10, 2012 21:26
-
-
Save onodes/3086340 to your computer and use it in GitHub Desktop.
ca-complex-lib.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
struct _complex{ | |
double re; | |
double im; | |
}; | |
typedef struct _complex complex; | |
void zprint(complex c); | |
complex zset(double x, double y); | |
complex zadd(complex a, complex b); | |
complex zsub(complex a, complex b); | |
complex zmul(complex a, complex b); | |
complex zdiv(complex a, complex b); | |
int main(){ | |
complex a, b, c, d, e; | |
a = zset(1.0, 2.0); | |
b = zset(-2.0, -0.5); | |
c = zadd(a,b); | |
printf("a+b="); | |
zprint(c); | |
printf("\n"); | |
c = zsub(a,b); | |
printf("a-b="); | |
zprint(c); | |
printf("\n"); | |
c = zmul(a,b); | |
printf("a*b="); | |
zprint(c); | |
printf("\n"); | |
c = zdiv(a,b); | |
printf("a/b="); | |
zprint(c); | |
printf("\n"); | |
return 0; | |
} | |
void zprint(complex c){ | |
printf("(%f)+j(%f)", c.re, c.im); | |
} | |
complex zset(double a, double b){ | |
complex c; | |
c.re = a; | |
c.im = b; | |
return c; | |
} | |
complex zadd(complex a, complex b){ | |
complex c; | |
c.re = a.re + b.re; | |
c.im = a.im + b.im; | |
return c; | |
} | |
complex zsub(complex a, complex b){ | |
complex c; | |
c.re = a.re - b.re; | |
c.im = a.im - b.im; | |
return c; | |
} | |
complex zmul(complex a, complex b){ | |
complex c; | |
c.re = a.re * b.re - a.im * b.im; | |
c.im = a.re * b.im + a.im * b.re; | |
return c; | |
} | |
complex zdiv(complex a, complex b){ | |
complex c; | |
double denominator=0; | |
denominator = b.re * b.re + b.im * b.im; | |
c.re = (a.re * b.re + a.im * b.im)/denominator; | |
c.im = (a.im * b.re - a.re * b.im)/denominator; | |
return c; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment