Code:
#include <stdio.h> #include <math.h>
int add(void);
int sub(void);
int multi(void);
int exit(void);
int addTwoComplexNumbers1( float a1, float a2 );
int addTwoComplexNumbers2( float b1, float b2 );
int subTwoComplexNumbers1( float a1, float a2 );
int subTwoComplexNumbers2( float b1, float b2 );
int mulTwoComplexNumbers1( float a1, float a2, float b1, float b2 );
int mulTwoComplexNumbers2( float a1, float a2, float b1, float b2 );
int main(void)
{
int choice;
do
{
printf("---------- Main Menu ----------\n");
printf("1. Add two complex numbers\n");
printf("2. Subtract two complex numbers\n");
printf("3. Multiply two complex numbers\n");
printf("4. Exit the program\n");
printf(" Please select an option (1, 2, 3, or 4):");
scanf("%d",&choice);
switch(choice)
{
case 1: add();
break;
case 2: sub();
break;
case 3: multi();
break;
case 4: printf("\nThe program has exited successfully\n\n");
return;
break;
default: printf("Input error: %d is not a choice!\n\n\n", choice);
break;
}
}while (choice);
}
int add(void)
{
float a1, b1, a2, b2;
float c, d;
printf("Enter the first complex numbers in the format a b: ");
scanf("%f %f", &a1, &b1);
printf("Enter the second complex numbers in the format a b: ");
scanf("%f %f", &a2, &b2);
c = a1 + a2;
d = b1 + b2;
c = addTwoComplexNumbers1(a1, a2);
d = addTwoComplexNumbers2(b1, b2);
printf("(%1.1f , %1.1f) + (%1.1f , %1.1f) = (%1.1f , %1.1f)\n\n", a1, b1, a2, b2, c, d);
return 0;
}
int sub(void)
{
float a1, b1, a2, b2;
float c, d;
printf("Enter the first complex numbers in the format a b: ");
scanf("%f %f", &a1, &b1);
printf("Enter the first complex numbers in the format a b: ");
scanf("%f %f", &a2, &b2);
c = a1 - a2;
d = b1 - b2;
c = subTwoComplexNumbers1(a1, a2);
d = subTwoComplexNumbers2(b1, b2);
printf("(%1.1f , %1.1f) - (%1.1f , %1.1f) = (%1.1f , %1.1f)\n\n", a1, b1, a2, b2, c, d);
return 0;
}
int multi(void)
{
float a1, b1, a2, b2;
float c, d;
printf("Enter the first complex numbers in the format a b: ");
scanf("%f %f", &a1, &b1);
printf("Enter the first complex numbers in the format a b: ");
scanf("%f %f", &a2, &b2);
c = (a1 * a2) - (b1 * b2);
d = (a1 * a2) + (b1 * b2);
c = mulTwoComplexNumbers1(a1, a2, b1, b2);
d = mulTwoComplexNumbers2(a1, a2, b1, b2);
printf("(%1.1f , %1.1f) * (%1.1f , %1.1f) = (%1.1f , %1.1f)\n\n", a1, b1, a2, b2, c, d);
return 0;
}
int addTwoComplexNumbers1( float a1, float a2)
{
return(a1+a2);
}
int addTwoComplexNumbers2( float b1, float b2)
{
return(b1+b2);
}
int subTwoComplexNumbers1( float a1, float a2)
{
return(a1 - a2);
}
int subTwoComplexNumbers2( float b1, float b2)
{
return(b1 - b2);
}
int mulTwoComplexNumbers1( float a1, float a2, float b1, float b2 )
{
return((a1*a2) - (b1*b2));
}
int mulTwoComplexNumbers2( float a1, float a2, float b1, float b2 )
{
return((a1*a2) + (b1*b2));
}