Presentation is loading. Please wait.

Presentation is loading. Please wait.

Complex Number Module The code given here relies upon ANSI C’s ability to return a struct. Esakov and Weiss use the argument list to return a pointer to.

Similar presentations


Presentation on theme: "Complex Number Module The code given here relies upon ANSI C’s ability to return a struct. Esakov and Weiss use the argument list to return a pointer to."— Presentation transcript:

1 Complex Number Module The code given here relies upon ANSI C’s ability to return a struct. Esakov and Weiss use the argument list to return a pointer to a struct. This is how K & R C would do it. A programmer can choose either strategy. But, you should fully understand what the memory model is doing for both strategies. Can you see the overhead involved in passing and returning entire structs? Can you see that the C Compiler needs to create space when it returns a struct.

2 main.c globals.hcomplex.h complex.c bash> gcc -o complex -ansi -Wall main.c complex.c

3 /**********************************************************/ /* globals.h */ /**********************************************************/ #ifndef _globals #define _globals typedef enum{ FALSE = 0, TRUE = 1 } bool ; #endif

4 /***************************************************************************/ /* Programmer: Jim Canning */ /* */ /* Main driver to test complex number module. */ /***************************************************************************/ #include #include "complex.h" int main( int argc, char *argv[] ) { complex complex1, complex2, complex3 ; double r, i ; printf( "Complex 1: Please enter two floating point values: " ) ; scanf("%lf", &r) ; scanf("%lf", &i ) ; complex1 = load_complex( r, i ) ; printf("\nThe complex number is " ); print_complex( complex1 ) ; printf("\n"); printf( "Complex 2: Please enter two floating point values: " ) ; scanf("%lf", &r) ; scanf("%lf", &i ) ; complex2 = load_complex( r, i ) ; printf("\nThe complex number is " ); print_complex( complex2 ) ; printf("\n");

5 complex3 = add_complex( complex1, complex2 ) ; printf("The addition of the two complex numbers is "); print_complex( complex3 ) ; printf("\n") ; complex3 = subtract_complex( complex1, complex2 ) ; printf("The subtraction of the two complex numbers is "); print_complex( complex3 ) ; printf("\n") ; complex3 = multiply_complex( complex1, complex2 ) ; printf( "The multiplication of the two complex numbers is " ); print_complex( complex3 ) ; printf("\n") ; complex3 = divide_complex(complex1, complex2 ) ; printf( "The division of the two complex numbers is " ); print_complex( complex3 ) ; printf("\n") ; if ( equal_complex( complex1, complex2 ) ) printf("\nThey are two equal complex numbers.\n"); else printf("\nThe numbers are not equal.\n") ; return 0 ; }

6 /****************************************************************************/ /* complex.h */ /****************************************************************************/ #ifndef _complex #define _complex #include "globals.h" typedef struct complex{ double real ; double imaginary ; } complex ; extern complex load_complex( double real, double imaginary ) ; extern void retrieve( complex complex1, double *p_real, double *p_imaginary ); extern complex add_complex( complex complex1, complex complex2 ) ; extern complex multiply_complex( complex complex1, complex complex2 ) ; extern complex subtract_complex( complex complex1, complex complex2 ) ; extern bool equal_complex( complex complex1, complex complex2 ) ; extern complex divide_complex( complex complex1, complex complex2 ) ; extern void print_complex( complex complex1 ) ; #endif

7 /*****************************************************************************/ /* Programmer: Jim Canning */ /* complex.c */ /*****************************************************************************/ #include "globals.h" #include "complex.h” #include extern complex load_complex( double real, double imaginary ) { complex a ; a.real = real ; a.imaginary = imaginary ; return ( a ) ; } extern void retrieve( complex complex1, double *p_real, double *p_imaginary ){ *p_real = complex1.real ; *p_imaginary = complex1.imaginary ; return ; }

8 extern complex add_complex( complex complex1, complex complex2 ) { complex a ; a.real = complex1.real + complex2.real ; a.imaginary = complex1.imaginary + complex2.imaginary ; return ( a ) ; } extern complex multiply_complex( complex complex1, complex complex2 ) { complex a ; a.real = ( complex1.real * complex2.real ) - ( complex1.imaginary * complex2.imaginary ) ; a.imaginary = ( complex1.real * complex2.imaginary ) + ( complex1.imaginary * complex2.real ) ; return a ; }

9 extern void print_complex( complex complex1 ) { printf("%f ", complex1.real) ; printf("%f ", complex1.imaginary ) ; return ; }

10 extern bool equal_complex( complex complex1, complex complex2 ) { return (( complex1.real == complex2.real ) && ( complex1.imaginary == complex2.imaginary )) ; } extern complex divide_complex( complex complex1, complex complex2 ) { complex a ; a.real = (( complex1.real * complex2.real ) + ( complex1.imaginary * complex2.imaginary ) ) / ( ( complex2.real * complex2.real ) + ( complex2.imaginary * complex2.imaginary )) ; a.imaginary = (( complex1.imaginary * complex2.real ) - ( complex1.real * complex2.imaginary ) ) / ( ( complex2.real * complex2.real ) + ( complex2.imaginary * complex2.imaginary )) ; return ( a ) ; }

11 main.c globals.hcomplex.h complex.c bash> make Makefile

12 # # This is a Makefile for the complex number # exercise. # complex: main.o complex.o gcc -o complex main.o complex.o main.o: main.c complex.h gcc -ansi -Wall -c main.c complex.o: complex.c complex.h gcc -ansi -Wall -c complex.c clean: rm -f *.o complex

13 http://www.gnu.org/software/make/manual/make.html man make Learning about make Google make


Download ppt "Complex Number Module The code given here relies upon ANSI C’s ability to return a struct. Esakov and Weiss use the argument list to return a pointer to."

Similar presentations


Ads by Google