//========================================================================= #define PROGRAMMER "SOLUTIONS, Nofrills" #define PROG_CODE "soln" #define COURSE "ECE-1021" #define YEAR (2004) #define TERM "Spring" #define SECTION (0) #define ASSIGNMENT "HW #8A" #define REVISION (0) #define TITLE "Complex Math" #define SUBTITLE "Last Modified on 23 APR 04" #define EMAIL "ece1021@eas.uccs.edu" #define FILENAME "08a0soln.c" //========================================================================= //========================================================================= // PROBLEM //========================================================================= // // Programming Problem #10.3 // // Implement primitive functions and utility functions before implementing // the functions called for in the problem. // //========================================================================= // PSEUDOCODE //========================================================================= // // This is a set of very specific functions and so no detailed pseudocode // is really appropriate. But a description of each function is warranted. // // Primitive Functions: // // double GetCOMPLEXreal(COMPLEX a); // double GetCOMPLEXimag(COMPLEX a); // double SetCOMPLEXreal(COMPLEX *a, double real); //returns real // double SetCOMPLEXimag(COMPLEX *a, double imag); //returns imag // // Utility Functions: // // Re(a): returns the real part of the complex number a // Im(a): returns the imaginary part of the complex number a // cartesian(real, imaginary): returns a complex number // Mag(a): returns the magnitude of the complex number a // Arg(a): returns the arg (the angle in radians) of the complex number a // polar(magnitude, arg): returns a complex number // // Functions: // add_c(a,b) return a + b // sub_c(a,b) return a - b // mul_c(a,b) return a * b // div_c(a,b) return a / b // read_c(ptr2c) read a complex number from the keyboard and store in c // print_c(a) print out the complex number a // conj_c(a) returns complex conjugate // abs_c(a) returns magnitude of a (result is double) // exp_c(a) returns exp(a) (result is complex) // //========================================================================= // DEVIATIONS FROM SUBMITTED PSEUDOCODE //========================================================================= // // While not really a deviation, the name 'complex' is apparently defined // in so the tag of the structure here was changed to 'mycomplex' // // The read_c() function was enhanced to behave like the time() function. // If the pointer passed to it is NULL, then it returns the new complex // number by value. If the pointer is not NULL, then is stores the new // complex number to the indicated location in addition to returning it // by value; // //========================================================================= // 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") //== COMPLEX structure =================================================== struct mycomplex { double real; double imag; }; 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; 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: 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)); } //========================================================================= // Utility Functions: 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)) ); } //========================================================================= // 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)) ) ); }