PDA

View Full Version : [SOLVED] [C] Custom operators?

Can+~
March 20th, 2008, 09:08 PM
I was given this "bonus" task at university, about building home-made complex numbers. So far so good, I could do everything on the task (sum and multiplication).

Now I'm wondering, if it's possible that I could do something like, building my own operators for this struct, like "complex a + complex b".

typedef struct
{
float r,i;
}complex;

// blah blah...
complex a, b;

//the user fills a and b with something like:
// a = 2+3i
// b = 4-6i
c = a + b

//And c should be 6-3i

C is kinda awkward after months with python+gtk.

PD: The task is finished, it could be sent right now, but I'm wondering if this is possible.

eye208
March 20th, 2008, 09:11 PM
Now I'm wondering, if it's possible that I could do something like, building my own operators for this struct, like "complex a + complex b".
It's possible in C++, but not in C.

pmasiar
March 21st, 2008, 03:31 AM

jpkotta
March 21st, 2008, 04:02 AM
C99 has a complex type.

#include <complex.h>
#include <stdio.h>

int main(void)
{
complex z = 1+2*I;
z *= z;

printf("z squared is (%f + j*%f).\n",
creal(z), cimag(z));

return 0;
}

[jpkotta@gauss w](1)\$ gcc -o complex complex.c

[jpkotta@gauss w](1)\$ ./complex
z squared is (-3.000000 + j*4.000000).

But in general, you can't do operator overloading like the others said.