Skip to content

Instantly share code, notes, and snippets.

@onodes
Created July 10, 2012 21:26
Show Gist options
  • Save onodes/3086340 to your computer and use it in GitHub Desktop.
Save onodes/3086340 to your computer and use it in GitHub Desktop.
ca-complex-lib.c
#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