/* Add, Subtract, Divide & Multiply Complex Numbers */
#include <stdio.h>
struct complexNumber {
float real, img;
};
typedef struct complexNumber complexNum;
complexNum add(complexNum, complexNum);
complexNum sub(complexNum, complexNum);
complexNum divide(complexNum, complexNum);
complexNum multiply(complexNum, complexNum);
int main() {
complexNum ab, cd, result;
ab.real = 0;
ab.img = 0;
cd.real = 0;
cd.img = 0;
printf("Enter real part of first number: ");
scanf("%f", &ab.real);
printf("Enter imaginary part of first number: ");
scanf("%f", &ab.img);
printf("Enter real part of second number: ");
scanf("%f", &cd.real);
printf("Enter imaginary part of second number: ");
scanf("%f", &cd.img);
result = add(ab, cd);
printf("\nAddition\n");
printf("Real: %.1f\n", result.real);
printf("Imaginary: %.1f\n", result.img);
printf("\n\n");
result = sub(ab, cd);
printf("Subtraction\n");
printf("Real: %.1f\n", result.real);
printf("Imaginary: %.1f\n", result.img);
printf("\n\n");
result = divide(ab, cd);
printf("Division\n");
printf("Real: %.1f\n", result.real);
printf("Imaginary: %.1f\n", result.img);
printf("\n\n");
result = multiply(ab, cd);
printf("Multiplication\n");
printf("Real: %.1f\n", result.real);
printf("Imaginary: %.1f\n", result.img);
printf("\n\n");
}
complexNum add(complexNum ab, complexNum cd) {
complexNum result;
result.real = ab.real + cd.real;
result.img = ab.img + cd.img;
return result;
}
complexNum sub(complexNum ab, complexNum cd) {
complexNum result;
result.real = ab.real - cd.real;
result.img = ab.img - cd.img;
return result;
}
complexNum divide(complexNum ab, complexNum cd) {
complexNum result;
float a, b, c, d;
a = ab.real;
b = ab.img;
c = cd.real;
d = ab.img;
result.real = (((a*c) + (b*d)) / ((c*c) + (d*d)));
result.img = (((b*c) - (a*d)) / ((c*c) + (d*d)));
return result;
}
complexNum multiply(complexNum ab, complexNum cd) {
complexNum result;
float a, b, c, d;
a = ab.real;
b = ab.img;
c = cd.real;
d = ab.img;
result.real = ((a*c) - (b*d));
result.img = ((b*c) + (a*d));
getch();
return result;
}
Tuesday, February 10, 2015
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment