//========================================================================= #define PROGRAMMER "SOLUTIONS, Nofrills" #define PROG_CODE "soln" #define COURSE "ECE-1021" #define YEAR (2004) #define TERM "Spring" #define SECTION (0) #define ASSIGNMENT "HW #8C" #define REVISION (0) #define TITLE "Complex Math - extended" #define SUBTITLE "Last Modified on 23 APR 04" #define EMAIL "ece1021@eas.uccs.edu" #define FILENAME "08c0soln.c" //========================================================================= //========================================================================= // PROBLEM //========================================================================= // // Programming Problem #10.3 // // Augment the code from HW #8A to include an alternate form of the // stucture in polar coodinates and a set of primitive and utility // functions for each controlled by a #ifdef COMPLEX_POLAR constant. // Everything else is to remain the same. // //========================================================================= // PSEUDOCODE //========================================================================= // // There will be two structure definitions and two sets of primitive and // utility functions. Only one structure definition and one set of // functions will be compiled. Which one will be controlled by a symbolic // constant called COMPLEX_POLAR. // //========================================================================= // DEVIATIONS FROM SUBMITTED PSEUDOCODE //========================================================================= // //========================================================================= // WAIVED COMPILER WARNINGS //========================================================================= // // Linker Warning: No module definition file specified: using defaults // Reason for Waiver: Can't suppress. Does not have adverse impact. //========================================================================= // CODE SECTION //========================================================================= //== INCLUDE FILES ======================================================== #include // printf(), scanf() #include // sqrt(), atan2() //== MACRO DEFINITIONS ==================================================== #define FALSE (0) #define TRUE (!FALSE) #define BLANKLINE printf("\n") #define COMPLEX_POLAR //== COMPLEX structure =================================================== #ifdef COMPLEX_POLAR struct mycomplex { double mag; double arg; }; #else struct mycomplex { double real; double imag; }; #endif typedef struct mycomplex COMPLEX; // Utility Functions: double Re(COMPLEX a); double Im(COMPLEX a); COMPLEX cartesian(double real, double imaginary); double Mag(COMPLEX a); double Arg(COMPLEX a); COMPLEX polar(double magnitude, double arg); // User Functions: COMPLEX add_c(COMPLEX a, COMPLEX b); COMPLEX sub_c(COMPLEX a, COMPLEX b); COMPLEX mul_c(COMPLEX a, COMPLEX b); COMPLEX div_c(COMPLEX a, COMPLEX b); COMPLEX read_c(COMPLEX *p); void print_c(COMPLEX a); COMPLEX conj_c(COMPLEX a); double abs_c(COMPLEX a); COMPLEX exp_c(COMPLEX a); //========================================================================= //== FUNCTION PROTOTYPES (for Primary Functions) ========================== void PrintHeader(void); //== MAIN FUNCTION ======================================================== int main(void) { COMPLEX a, b; PrintHeader(); BLANKLINE; #ifdef COMPLEX_POLAR printf("Structure stores value in polar form\n"); #else printf("Structure stores value in cartesian form\n"); #endif BLANKLINE; printf("Enter a complex number (of the form a+bi) a = "); a = read_c(NULL); // One way of using read(c) printf("Enter a complex number (of the form a+bi) b = "); read_c(&b); // The other way of using read(c) BLANKLINE; printf("Output from User Functions:\n"); BLANKLINE; printf(" a + b = "); print_c(add_c(a, b)); printf("\n"); printf(" a - b = "); print_c(sub_c(a, b)); printf("\n"); printf(" a * b = "); print_c(mul_c(a, b)); printf("\n"); printf(" a / b = "); print_c(div_c(a, b)); printf("\n"); printf(" The conjugate of <"); print_c(a); printf("> is "); print_c(conj_c(a)); printf("\n"); printf(" The absolute value of <"); print_c(a); printf("> is %g\n", abs_c(a)); printf(" The exponential of <"); print_c(a); printf("> is "); print_c(exp_c(a)); printf("\n"); BLANKLINE; printf("End of User Functions test\n"); return(0); } //========================================================================= // PRIMARY FUNCTIONS ( functions called directly by main() ) //========================================================================= //== FUNCTION PROTOTYPES (for Support Functions) ========================== int printc(char c, int n); //== PRIMARY FUNCTIONS ==================================================== void PrintHeader(void) { printc('=', 79); printf("\n"); printf("Course....... %s-%i (%s %i)\n", COURSE, SECTION, TERM, YEAR); printf("Programmer... %s (%s)\n", PROGRAMMER, PROG_CODE); printf("Assignment... %s (Rev %i) (Source Code in %s)\n", ASSIGNMENT, REVISION, FILENAME); printf("Description.. %s\n", TITLE); printf(" %s\n", SUBTITLE); printc('=', 79); printf("\n"); return; } //========================================================================= // SUPPORT FUNCTIONS (functions not called directly by main() ) //========================================================================= int printc(char c, int n) { while(0 < n--) printf("%c", c); return(n); } //========================================================================= // FUNCTIONS for structure COMPLEX //========================================================================= // struct complex // { // double real; // double imag; // }; // typedef struct complex COMPLEX; //========================================================================= // Primitive Functions: #ifdef COMPLEX_POLAR double GetCOMPLEXmag(COMPLEX a) { return(a.mag); } double GetCOMPLEXarg(COMPLEX a) { return(a.arg); } double SetCOMPLEXmag(COMPLEX *a, double mag) { a->mag = mag; return(GetCOMPLEXmag(*a)); } double SetCOMPLEXarg(COMPLEX *a, double arg) { a->arg = arg; return(GetCOMPLEXarg(*a)); } #else double GetCOMPLEXreal(COMPLEX a) { return(a.real); } double GetCOMPLEXimag(COMPLEX a) { return(a.imag); } double SetCOMPLEXreal(COMPLEX *a, double real) { a->real = real; return(GetCOMPLEXreal(*a)); } double SetCOMPLEXimag(COMPLEX *a, double imag) { a->imag = imag; return(GetCOMPLEXimag(*a)); } #endif //========================================================================= // Utility Functions: #ifdef COMPLEX_POLAR double Re(COMPLEX a) { return( GetCOMPLEXmag(a) * cos(GetCOMPLEXarg(a)) ); } double Im(COMPLEX a) { return( GetCOMPLEXmag(a) * sin(GetCOMPLEXarg(a)) ); } COMPLEX cartesian(double real, double imaginary) { double mag, arg; mag = sqrt(real*real + imaginary*imaginary); arg = atan2(imaginary, real); return( polar(mag, arg) ); } double Mag(COMPLEX a) { return( GetCOMPLEXmag(a) ); } double Arg(COMPLEX a) { return( GetCOMPLEXarg(a) ); } COMPLEX polar(double magnitude, double arg) { COMPLEX c; SetCOMPLEXmag(&c, magnitude); SetCOMPLEXarg(&c, arg); return(c); } #else double Re(COMPLEX a) { return(GetCOMPLEXreal(a)); } double Im(COMPLEX a) { return(GetCOMPLEXimag(a)); } COMPLEX cartesian(double real, double imaginary) { COMPLEX c; SetCOMPLEXreal(&c, real); SetCOMPLEXimag(&c, imaginary); return(c); } double Mag(COMPLEX a) { return( sqrt( Re(a)*Re(a) + Im(a)*Im(a) ) ); } double Arg(COMPLEX a) { return( atan2( Im(a), Re(a) ) ); } COMPLEX polar(double magnitude, double arg) { return( cartesian(magnitude*cos(arg), magnitude*sin(arg)) ); } #endif //========================================================================= // User Functions: COMPLEX add_c(COMPLEX a, COMPLEX b) { return( cartesian( (Re(a) + Re(b)), (Im(a) + Im(b)) ) ); } COMPLEX sub_c(COMPLEX a, COMPLEX b) { return( cartesian( (Re(a) - Re(b)), (Im(a) - Im(b)) ) ); } COMPLEX mul_c(COMPLEX a, COMPLEX b) { return( polar( (Mag(a) * Mag(b)), (Arg(a) + Arg(b)) ) ); } COMPLEX div_c(COMPLEX a, COMPLEX b) { return( polar( (Mag(a) / Mag(b)), (Arg(a) - Arg(b)) ) ); } COMPLEX read_c(COMPLEX *p) { COMPLEX c; double real, imag; scanf("%lf%lfi", &real, &imag); c = cartesian(real, imag); if(NULL != p) *p = c; return(c); } void print_c(COMPLEX a) { printf(" %g%+gi ", Re(a), Im(a) ); return; } COMPLEX conj_c(COMPLEX a) { return( cartesian( Re(a), -Im(a) ) ); } double abs_c(COMPLEX a) { return(Mag(a)); } COMPLEX exp_c(COMPLEX a) { return(cartesian( exp(Mag(a))*cos(Im(a)), exp(Mag(a))*sin(Im(a)) ) ); }