need help with complex C program using struct

March 14, 2011 at 18:38:23
Specs: Linux i686
write a C program to add, subtract, multiply, divide, and take absolute value of complex numbers using data structures.

#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <string.h>


typedef struct { double real,
imag;
} complex_t;

/*
* Complex number input function returns standard scanning error code
* 1 => valid scan, 0 => error, negative EOF value => end of file
*/
int scan_complex(complex_t *c) /* output - address of complex variable to fill*/
{
int status;

// status = scanf("%lf%lf", &c->real, &c->imag);
status = scanf("%lf%lf",&c->real,&c->imag);

if (status == 2)
status = 1;
else if (status != EOF)
status = 0;

return (status);
}

/*
* Complex output function displays value as (a + bi) or (a - bi),
* dropping a or b if they round to 0 unless both round to 0
*/
void print_complex(complex_t c) /* input - complex number to display */
{
double a, b;
char sign;

a = c.real;
b = c.imag;

printf("(");

if (fabs(a) < .005 && fabs(b) < .005) {
printf("%.2f", 0.0);
}
else if (fabs(b) < .005) {
printf("%.2f", a);
}
else if (fabs(a) < .005) {
printf("%.2fi", b);
} else {
if (b <= 0)
sign = '-';
else {
sign = '+';
printf("%.2f %c %.2fi", a, sign, fabs(b));
}
}

printf(")");
}

/*
* Sum c1 + c2
*/
complex_t add_complex(complex_t c1, complex_t c2)
{
complex_t csum;

csum.real = c1.real + c2.real;
csum.imag = c1.imag + c2.imag;

return (csum);
}

/*
* Difference c1 - c2
*/
complex_t subtract_complex(complex_t c1, complex_t c2) /* input parameters */
{
complex_t cdiff;
cdiff.real = c1.real - c2.real;
cdiff.imag = c1.imag - c2.imag;

return (cdiff);
}

/*
* Absolute value abs(c)
*/
complex_t abs_complex(complex_t c) /* input parameter */
{
complex_t cabs;

cabs.real = sqrt(c.real * c.real + c.imag * c.imag);
cabs.imag = 0;

return (cabs);
}

/* ** Stub **
* Product c1 * c2
*/
complex_t multiply_complex(complex_t c1, complex_t c2) /* input parameters */
{
complex_t cmult;

cmult.real = c1.real * c2.real;
cmult.imag = (c1.real * c2.real) - (c1.imag * c2.imag) + ((c1.real * c2.imag) + (c1.imag * c2.real));

return (cmult);
}


/* ** Stub **
* Quotient c1 / c2
*/
complex_t divide_complex(complex_t c1, complex_t c2) /* input parameters */
{
complex_t cdivide;

cdivide.real = c1.real / c2.real;
cdivide.imag = ( (c1.real * c2.real + c1.imag * c2.imag) / ((pow(c2.real,2) + pow(c2.imag,2))) ) + ( (c1.imag * c2.real - c1.real * c2.imag) / ((pow(c2.real,2) + pow(c2.imag,2))) ) ;

return (cdivide);
}


int main(void)
{
complex_t com1, com2;

printf("Enter the real and imaginary parts of a complex number");
printf("separated by a space\n");

printf("first complex number > ");
scan_complex(&com1);

printf("second complex number> ");
scan_complex(&com2);

/* Forms and displays com1+com2 */
print_complex(com1);
printf(" + ");
print_complex(com2);
printf(" = ");
print_complex(add_complex(com1, com2));

/* Forms and displays com1-com2 */
printf("\n");
print_complex(com1);
printf(" - ");
print_complex(com2);
printf(" = ");
print_complex(subtract_complex(com1, com2));

/* Forms and displays com1 * com2 */
printf("\n");
print_complex(com1);
printf(" * ");
print_complex(com2);
printf(" = ");
print_complex(multiply_complex(com1, com2)); /* LINE 173 */

/* Forms and displays com1 / com2 */
printf("\n");
print_complex(com1);
printf(" / ");
print_complex(com2);
printf(" = ");
print_complex(divide_complex(com1, com2));

/* Forms and displays abs(com1) */
printf("\n|");
print_complex(com1);
printf("| = ");
print_complex(abs_complex(com1));

/* Forms and displays abs(com2) */
printf("\n|");
print_complex(com2);
printf("| = ");
print_complex(abs_complex(com2));

printf("\n");

return (0);
}


output format should be like this:

Enter the real and imaginary parts of a complex number separated by a space
first complex number > 3 2
second complex number> 3 6
(3.00 + 2.00i) + (3.00 + 6.00i) = (6.00 + 8.00i)
(3.00 + 2.00i) - (3.00 + 6.00i) = (-4.00i)
(3.00 + 2.00i) * (3.00 + 6.00i) = (-3.00 + 24.00i)
(3.00 + 2.00i) / (3.00 + 6.00i) = (0.47 - 0.27i)
|(3.00 + 2.00i)| = (3.61)
|(3.00 + 6.00i)| = (6.71)


my output format resulted in:

Enter the real and imaginary parts of a complex numberseparated by a space
first complex number > 3 2
second complex number> 3 6
(3.00 + 2.00i) + (3.00 + 6.00i) = (6.00 + 8.00i)
(3.00 + 2.00i) - (3.00 + 6.00i) = (-4.00i)
(3.00 + 2.00i) * (3.00 + 6.00i) = (9.00 + 21.00i)
(3.00 + 2.00i) / (3.00 + 6.00i) = (1.00 + 0.20i)
|(3.00 + 2.00i)| = (3.61)
|(3.00 + 6.00i)| = (6.71)

so my multiplication and division is wrong.
can anybody tell me my calculation error ?


See More: need help with complex C program using struct

Report •

#1
March 15, 2011 at 05:52:48
Well, you know where your problem is, now all you need to do is think of the correct formula. Wikipedia's Complex Number article has the formulas, so it's literally just a matter of copying down the formula and doing a Find / Replace.

To that end:

cmult.real = c1.real * c2.real - c1.imag * c2.imag;
cmult.imag = c1.real * c2.imag + c1.imag * c2.real;

cdivide.real = (c1.real * c2.real + c1.imag * c2.imag) / (c2.real * c2.real + c2.imag * c2.imag);
cdivide.imag = (c1.real * c2.imag - c1.imag * c2.real) / (c2.real * c2.real + c2.imag * c2.imag);

How To Ask Questions The Smart Way


Report •
Related Solutions


Ask Question