Created
May 17, 2012 14:46
-
-
Save rjw57/2719411 to your computer and use it in GitHub Desktop.
Complex number arithmetic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Pre-processor directive */ | |
#include <stdio.h> | |
#include <math.h> | |
/*Type def */ | |
typedef struct | |
{ | |
float real; | |
float imag; | |
} complex; | |
/* Function prototypes */ | |
int mainMenu (); | |
complex inputComplexNumbers_first (); | |
complex inputComplexNumbers_second (); | |
complex addTwoComplexNumbers (complex a, complex b); | |
complex subTwoComplexNumbers (complex a, complex b); | |
complex multwoComplexNumbers (complex a, complex b); | |
int | |
main (void) | |
{ | |
int theirchoice; | |
complex numberchoice_first, numberchoice_second; | |
complex sumcomplex, diffComplex, multComplex; | |
while (-1) /* While loop, only to terminate when commanded or wrong twice */ | |
{ | |
theirchoice = mainMenu (); /* Function call 'mainMenu()' */ | |
if (theirchoice == 1) | |
{ | |
numberchoice_first = inputComplexNumbers_first (); | |
numberchoice_second = inputComplexNumbers_second (); | |
sumcomplex = | |
addTwoComplexNumbers (numberchoice_first, numberchoice_second); | |
printf ("\n Sum of two complex numbers is: %f %f \n\n", sumcomplex.real, sumcomplex.imag); | |
} | |
else if (theirchoice == 2) | |
{ | |
numberchoice_first = inputComplexNumbers_first (); | |
numberchoice_second = inputComplexNumbers_second (); | |
diffComplex = | |
subTwoComplexNumbers (numberchoice_first, numberchoice_second); | |
printf ("\n Difference of the two complex numbers is: %f %f \n", | |
diffComplex.real, diffComplex.imag); | |
} | |
else if (theirchoice == 3) | |
{ | |
numberchoice_first = inputComplexNumbers_first (); | |
numberchoice_second = inputComplexNumbers_second (); | |
multComplex = | |
multwoComplexNumbers (numberchoice_first, numberchoice_second); | |
printf ("\n Multiplication of the two complex numers is: %f %f \n", | |
multComplex.real, multComplex.imag); | |
} | |
else if (theirchoice == 4) | |
{ | |
exit (0); | |
} | |
} | |
return (0); | |
} | |
int | |
mainMenu () | |
{ | |
int numberchoice; | |
printf ("----------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", &numberchoice); | |
printf ("\n\n"); | |
if ((numberchoice >= 5) || (numberchoice <= 0)) /* If statement repeats if choice is wrong */ | |
{ | |
printf ("Invalid number choice, retry. \n\n\n:"); | |
printf ("----------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 ("%*[^\n]"); /* To remove their invalid characters */ | |
scanf ("%*1[\n]"); | |
scanf ("%d", &numberchoice); | |
printf ("\n\n"); | |
if ((numberchoice >= 5) || (numberchoice <= 0)) | |
{ | |
printf ("Invalid entry, program will now close. \n\n"); | |
exit (0); /* Exits program when entered incorrectly twice */ | |
} | |
else | |
return (numberchoice); | |
} | |
return (numberchoice); | |
} | |
complex | |
inputComplexNumbers_first () /* Function definition */ | |
{ | |
complex a; | |
printf (" \n Enter the first set of numbers in the format a b: "); | |
scanf ("%f %f", &a.real, &a.imag); | |
return (a); /* Suspect that it isn't scanning or returning correctly, however i cannot find a solution */ | |
} | |
complex | |
inputComplexNumbers_second () | |
{ | |
complex b; | |
printf (" \n Enter the second set of numbers numbers in the format a b: "); | |
scanf ("%f %f", &b.real, &b.imag); | |
return (b); /* Suspect that it isn't returning correctly, however i cannot find a solution */ | |
} | |
complex | |
addTwoComplexNumbers (complex a, complex b) | |
{ | |
complex c; | |
c.real = a.real + b.real; /* Isn't adding correctly due to suspected error in scanning */ | |
c.imag = a.imag + b.imag; | |
return (c); | |
} | |
complex | |
subTwoComplexNumbers (complex a, complex b) | |
{ | |
complex c; | |
c.real = (a.real - b.real); /* Isn't subracting correctly due to suspected error in scanning */ | |
c.imag = (a.imag - b.imag); | |
return (c); | |
} | |
complex | |
multwoComplexNumbers (complex a, complex b) | |
{ | |
complex c; | |
c.real = (a.real * b.real) - (a.imag * b.imag); /* Isn't multiplying correctly due to suspected error in scanning */ | |
c.imag = (a.imag * b.real) + (a.real * b.imag); | |
return (c); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment