- #include <stdio.h> /* Include standard input/output library*/
- typedef struct /* Defines data type for complex numbers */
- {
- float real;
- float imaginary;
- } complex; /* Name of structure */
- /* Function prototypes */
- int mainMenu(); /* Prototype for main menu*/
- complex inputComplexNumber1(void); /* prototype to get first complex number */
- complex inputComplexNumber2(void); /* prototype to get second complex number */
- complex addTwoComplexNumbers(complex a, complex b); /* Prototype for adding two complex numbers */
- complex subTwoComplexNumbers(complex a, complex b); /* Prototype for subtracting two complex numbers */
- complex multwoComplexNumbers(complex a, complex b); /* Prototype for multiplying two complex numbers */
- void clearStdin(void); /*prototype for removing old input values*/
- int main (void)
- {
- int selection; /* Number selected from the menu */
- do
- {
- selection = mainMenu(); /* Function call for the main menu */
- switch(selection)
- {
- case 1: { complex a = inputComplexNumber1(); /* function call for the complex numbers */
- complex b = inputComplexNumber2();
- complex result = addTwoComplexNumbers(a, b); /* function call to add the two numbers */
- printf("(%.2f %.2f) - (%.2f %.2f) = (%.2f %.2f)\n\n",a.real, a.imaginary, b.real, b.imaginary, result.real, result.imaginary); /* prints the results*/
- break;
- }
- case 2: { complex a = inputComplexNumber1(); /* function call for the complex numbers */
- complex b = inputComplexNumber2();
- complex result = subTwoComplexNumbers(a, b); /* function call to subtract the two numbers */
- printf("(%.2f %.2f) - (%.2f %.2f) = (%.2f %.2f)\n\n",a.real, a.imaginary, b.real, b.imaginary, result.real, result.imaginary); /* prints the results*/
- break;
- }
- case 3: { complex a = inputComplexNumber1(); /* function call for the complex numbers */
- complex b = inputComplexNumber2();
- complex result = multwoComplexNumbers(a, b); /* function call to multiply the two numbers */
- printf("(%.2f %.2f) - (%.2f %.2f) = (%.2f %.2f)\n\n",a.real, a.imaginary, b.real, b.imaginary, result.real, result.imaginary); /* prints the results*/
- break;
- }
- case 4: printf("The program is completed successfully");
- break;
- default: printf("Too many attempts at menu selction");
- break;
- }
- }while (selection > 0 & selection < 4);
- return(0);
- }
- int mainMenu() /* function for menu input */
- { int menuIn, /* Value for the menu input */
- counter; /* Counter is used to give a limited number of attempts */
- counter=1;
- printf("-------------Menu-------------\n"); /* Prints the menu */
- 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");
- do
- { ++counter;
- printf("Please select an option (1, 2, 3 or 4):");
- scanf("%d", &menuIn);
- if ((menuIn >0) && (menuIn <5) ) /* If statement checks if the input is valid */
- return (menuIn);
- else printf("\ninput error: %d is not a valid option\n", menuIn);
- }
- while (counter <3); /* Allows only two attempts at the menu */
- return (-1);
- }
- complex inputComplexNumber1(void) /* Function to get the first complex number */
- { float actuallyEntrd; /* Used to test if valid input */
- complex a;
- do /* do-while statement allows the input to repeat*/
- {
- printf("Please enter the first complex number in the format a b, then press enter: ");
- if ( (actuallyEntrd = scanf("%f %f", &a.real, &a.imaginary)) != 2 )
- { printf("! Input Failure !\n");
- clearStdin(); /* Function call to clear the input if not valid */
- }
- else (actuallyEntrd !=2);
- }while (actuallyEntrd !=2);
- return (a); /* Returns complex number a */
- }
- complex inputComplexNumber2(void) /* Function to get the first complex number */
- { float actuallyEntrd; /* Used to test if valid input */
- complex b;
- do /* do-while statement allows the input to repeat*/
- {
- printf("Please enter the first complex number in the format a b, then press enter: ");
- if ( (actuallyEntrd = scanf("%f %f", &b.real, &b.imaginary)) != 2 )
- { printf("! Input Failure !\n");
- clearStdin(); /* Function call to clear the input if not valid */
- }
- else (actuallyEntrd !=2);
- }while (actuallyEntrd !=2);
- return (b); /* Returns complex number B */
- }
- complex addTwoComplexNumbers(complex a, complex b) /* Function for adding two complex numbers */
- {
- complex result; /* Declares the result as a struct */
- result.real= a.real + b.real; /* Calculates the real result */
- result.imaginary = a.imaginary + b.imaginary; /* Calculates the imaginary result */
- return result; /* Returns the result */
- }
- complex subTwoComplexNumbers(complex a, complex b) /* Function for subtracting two complex numbers */
- {
- complex result; /* Declares the result as a struct */
- result.real= a.real - b.real; /* Calculates the real result */
- result.imaginary = a.imaginary - b.imaginary; /* Calculates the imaginary result */
- return result; /* Returns the result */
- }
- complex multwoComplexNumbers(complex a, complex b) /* Function for multiplying two complex numbers */
- {
- complex result; /* Declares the result as a struct */
- result.real= (a.real*b.real) - (a.imaginary*b.imaginary); /* Calculates the real result */
- result.imaginary = (a.real*b.real) + (a.imaginary*b.imaginary); /* Calculates the imaginary result */
- return result; /* Returns the result */
- }
- void clearStdin(void) /* Function for clearing the input*/
- {
- scanf("%*[^\n]"); /* skip to the end-of-line */
- scanf("%*1[\n]"); /* skip one new-line */
- return;
- }