/*=======================================================================*/ #define PROGRAMMER "SOLUTIONS, Nofrills" #define PROG_CODE "SOLN" #define COURSE "ECE-1021" #define YEAR (2004) #define TERM "Fall" #define SECTION (0) #define ASSIGNMENT "HW3" #define REVISION (0) #define TITLE "Integer and Floating Point Output" #define SUBTITLE "Last Modified on 06 SEP 04" #define EMAIL "ece1021@eas.uccs.edu" #define FILENAME "3_0SOLN0.c" /*=======================================================================*/ /*=======================================================================*/ /* WAIVED COMPILER WARNINGS */ /*=======================================================================*/ /* Linker Warning: No module definition file specified: using defaults Reason for Waiver: Can't suppress. Does not have adverse impact. */ /*=======================================================================*/ /* NOTES TO THE USER/GRADER */ /*=======================================================================*/ /* */ /*=======================================================================*/ /* PROBLEM STATEMENT */ /*=======================================================================*/ /* Create and test function that display integer and floating point values. Test each function with both typical and extreme data. Display the internal representation of each value. */ /*=======================================================================*/ /* DEVELOPMENT NOTES */ /*=======================================================================*/ /* */ /*=======================================================================*/ /* PSEUDOCODE */ /*=======================================================================*/ /* */ /*=======================================================================*/ /* DEVIATIONS FROM SUBMITTED PSEUDOCODE */ /*=======================================================================*/ /* */ /*=======================================================================*/ /*=======================================================================*/ /* CODE SECTION */ /*=======================================================================*/ /*=======================================================================*/ /*== INCLUDE FILES ======================================================*/ #include /* stdout, putc(), printf() */ #include /* UINT_MIN, UINT_MAX, INT_MIN, INT_MAX */ /* ULONG_MIN, ULONG_MAX, LONG_MIN, LONG_MAX */ #include /* FLT_MAX, DBL_MAX */ /*== MACRO DEFINITIONS (OBJECT-LIKE) ====================================*/ #define FALSE (0) #define TRUE (!FALSE) #define BLANKLINE printf("\n") #define MARGIN (2) /* Left Margin in PrintHeader() */ #define INDENT_SIZE (2) /* Indent relative to */ #define LMARG printc(' ', MARGIN) /* Print Left Margin */ #define INDENT printc(' ', INDENT_SIZE) /* Indent relative to margin */ #define TSYM 'T' #define FSYM '-' #define DEFAULT_BASE (10) /* Must be 2 through 36 */ /*== EXIT CODE DEFINITIONS ==============================================*/ #define EXIT_PASS (0) #define EXIT_FAIL (1) /*== MACRO DEFINITIONS (FUNCTION-LIKE) ==================================*/ #define PutC(c) (putc((char)(c),stdout)) #define PutD(d) (PutC( (char) ((d)<10)?('0'+(d)):('A'+(d)-10) )) #define Put_u(n) (Put_ubase((n), DEFAULT_BASE)) #define PutH_u(n)(Put_ubase((n), 16)) /*== TYPEDEF STATEMENTS =================================================*/ /*=======================================================================*/ /* STRUCTURE DEFINITIONS and FUNCTIONS */ /*=======================================================================*/ /*=======================================================================*/ /* SUPPORT FUNCTIONS ( functions not called directly by main() ) */ /*=======================================================================*/ int printc(char c, int n) { while ( (n--) && (c == putc(c, stdout)) ); /* EMPTY LOOP */ return n; } /*=======================================================================*/ /* INTEGER OUTPUT FUNCTIONS */ /*=======================================================================*/ int PutV_u(unsigned int n) { unsigned int m; int i, count; /* Set m to the largest power or ten <= n */ for (m = 1; n/m >=10; m*=10) /* EMPTYLOOP */; /* Print out the digits in n one-by-one */ for (count = 0; m > 0; m /= 10) { for(i = 0; n >= m; i++) n -= m; PutD(i); count++; } return count; } int PutV_i(int n) { if (n < 0) { PutC('-'); return 1 + PutV_u(-n); } return PutV_u(n); } int PutV_lu(unsigned long n) { unsigned long m; int i, count; /* Set m to the largest power or ten <= n */ for (m = 1; n/m >=10; m*=10) /* EMPTYLOOP */; /* Print out the digits in n one-by-one */ for (count = 0; m > 0; m /= 10) { for(i = 0; n >= m; i++) n -= m; PutD(i); count++; } return count; } int PutV_li(long int n) { if (n < 0) { PutC('-'); return 1 + PutV_lu(-n); } return PutV_lu(n); } /*=======================================================================*/ /* FLOATING POINT OUTPUT FUNCTIONS */ /*=======================================================================*/ int PutV_f(float n) { float m; int i, count; count = 0; if (n < 0) { PutC('-'); count++; n = -n; } /* Set m to the largest power or ten <= n */ for (m = 1; n/m >=10; m*=10) /* EMPTYLOOP */; /* Print out the digits in n (left of decimal point) one-by-one */ while (m > 0.5) { for(i = 0; n >= m; i++) n -= m; PutD(i); count++; m /= 10; } /* Print out decimal point */ PutC('.'); count++; /* Print out the digits in n (right of decimal point) one-by-one */ while (m > 0.5e-6) { for(i = 0; n >= m; i++) n -= m; PutD(i); count++; m /= 10; } return count; } int PutV_lf(double n) { double m; int i, count; count = 0; if (n < 0) { PutC('-'); count++; n = -n; } /* Set m to the largest power or ten <= n */ for (m = 1; n/m >=10; m*=10) /* EMPTYLOOP */; /* Print out the digits in n (left of decimal point) one-by-one */ while (m > 0.5) { for(i = 0; n >= m; i++) n -= m; PutD(i); count++; m /= 10; } /* Print out decimal point */ PutC('.'); count++; /* Print out the digits in n (right of decimal point) one-by-one */ while (m > 0.5e-6) { for(i = 0; n >= m; i++) n -= m; PutD(i); count++; m /= 10; } return count; } int PutV_e(float n) { float mantissa; int exponent; int count; count = 0; if (n < 0) { PutC('-'); count++; n = -n; } mantissa = n; exponent = 0.0; if ( 0 != n ) { while (mantissa >= 10.0) { mantissa /= 10.0; exponent++; } while (mantissa < 1.0) { mantissa *= 10.0; exponent--; } } count+=PutV_f(mantissa); PutC('e'); count++; count+=PutV_i(exponent); return count; } int PutV_le(double n) { double mantissa; int exponent; int count; count = 0; if (n < 0) { PutC('-'); count++; n = -n; } mantissa = n; exponent = 0.0; if ( 0 != n ) { while (mantissa >= 10.0) { mantissa /= 10.0; exponent++; } while (mantissa < 1.0) { mantissa *= 10.0; exponent--; } } count+=PutV_lf(mantissa); PutC('e'); count++; count+=PutV_i(exponent); return count; } /*=======================================================================*/ /* INTERNAL REPRESENTATION FUNCTIONS */ /*=======================================================================*/ /* NOTE: This code taken directly from the Lesson 6 material without mod */ /* LABELS FOR BITS IN THE 'HOW' MASK */ #define IR_BIT_BASE (0x01) #define IR_BIT_PACK (0x02) #define IR_BIT_ORDER (0x04) /* SYMBOLIC CONSTANTS FOR EACH 'HOW' MODE */ #define IR_BIN (0x00) #define IR_HEX (IR_BIT_BASE) #define IR_PACKED (0x00) #define IR_SPACED (IR_BIT_PACK) #define IR_FWD (0x00) #define IR_REV (IR_BIT_ORDER) /* TO USE: ADD or BITWISE-OR TOGETHER. EG: */ /* FOR REVERSED, SPACED, BIN: how = IR_REV + IR_SPACED + IR_BIN */ /* ALTERNATE WAY: how = IR_REV | IR_SPACED | IR_BIN */ /* The format chosen for this demo */ #define FORMAT ( IR_FWD | IR_SPACED | IR_BIN ) int PutIR(void *base, size_t size, int how) { unsigned char *ptr, *start; unsigned char mask; int i, nibble, step, chars; /* Set up based on byte order */ start = (unsigned char *) base + ((how & IR_BIT_ORDER)? (size-1) : 0); step = (how & IR_BIT_ORDER)? -1: 1; /* Process bytes one at a time */ for (i = 0, chars = 0, ptr = start; i < size; ptr += step, i++) { /* Inter-byte space if requested */ if ( (how & IR_BIT_PACK) && (ptr != start) ) { PutC(' '); chars++; } /* Print out the byte */ if (how & IR_BIT_BASE) for (nibble = (CHAR_BIT - 1)/4; nibble >= 0; nibble--) { PutD( ( (0x0F << 4*nibble) & (*ptr)) >> 4*nibble ); chars++; } else for (mask = 1 << (CHAR_BIT - 1); mask; mask >>= 1) { PutD( (mask & *ptr)? 1 : 0 ); chars++; } } return chars; } /*=======================================================================*/ /* INDIVIDUAL TEST CASE FUNCTIONS */ /*=======================================================================*/ void Test_u(unsigned int n) { int d; PutC('['); d = PutV_u(n); PutC(']'); /* output */ PutC('('); PutV_i(d); PutC(')'); /* chars */ PutC('{'); PutIR(&n, sizeof(n), FORMAT); PutC('}'); /* Int. Rep */ PutC('\n'); return; } void Test_i(int n) { int d; PutC('['); d = PutV_i(n); PutC(']'); /* output */ PutC('('); PutV_i(d); PutC(')'); /* chars */ PutC('{'); PutIR(&n, sizeof(n), FORMAT); PutC('}'); /* Int. Rep */ PutC('\n'); return; } void Test_lu(unsigned long int n) { int d; PutC('['); d = PutV_lu(n); PutC(']'); /* output */ PutC('('); PutV_i(d); PutC(')'); /* chars */ PutC('{'); PutIR(&n, sizeof(n), FORMAT); PutC('}'); /* Int. Rep */ PutC('\n'); return; } void Test_li(long int n) { int d; PutC('['); d = PutV_li(n); PutC(']'); /* output */ PutC('('); PutV_i(d); PutC(')'); /* chars */ PutC('{'); PutIR(&n, sizeof(n), FORMAT); PutC('}'); /* Int. Rep */ PutC('\n'); return; } void Test_f(float n) { int d; PutC('['); d = PutV_f(n); PutC(']'); /* output */ PutC('('); PutV_i(d); PutC(')'); /* chars */ PutC('{'); PutIR(&n, sizeof(n), FORMAT); PutC('}'); /* Int. Rep */ PutC('\n'); return; } void Test_lf(double n) { int d; PutC('['); d = PutV_lf(n); PutC(']'); /* output */ PutC('('); PutV_i(d); PutC(')'); /* chars */ PutC('{'); PutIR(&n, sizeof(n), FORMAT); PutC('}'); /* Int. Rep */ PutC('\n'); return; } void Test_e(float n) { int d; PutC('['); d = PutV_e(n); PutC(']'); /* output */ PutC('('); PutV_i(d); PutC(')'); /* chars */ PutC('{'); PutIR(&n, sizeof(n), FORMAT); PutC('}'); /* Int. Rep */ PutC('\n'); return; } void Test_le(double n) { int d; PutC('['); d = PutV_le(n); PutC(']'); /* output */ PutC('('); PutV_i(d); PutC(')'); /* chars */ PutC('{'); PutIR(&n, sizeof(n), FORMAT); PutC('}'); /* Int. Rep */ PutC('\n'); return; } /*=======================================================================*/ /* PRIMARY FUNCTIONS ( functions called directly by main() ) */ /*=======================================================================*/ /*== FUNCTION PROTOTYPES (to Document Support Functions) ================*/ int printc(char c, int n); int PutV_u(unsigned int n); int PutV_i(int n); int PutV_lu(unsigned long int n); int PutV_li(long int n); int PutV_f(float n); int PutV_lf(double n); int PutV_e(float n); int PutV_le(double n); int PutIR(void *base, size_t size, int how); void Test_u(unsigned int n); void Test_i(int n); void Test_lu(unsigned long int n); void Test_li(long int n); void Test_f(float n); void Test_lf(double n); void Test_e(float n); void Test_le(double n); /*== PRIMARY FUNCTIONS ==================================================*/ void PrintHeader(void) { LMARG; printc('=', (78 - MARGIN)); putc('\n', stdout); LMARG; printf("Course....... %s-%i (%s %i)\n",COURSE,SECTION,TERM,YEAR); LMARG; printf("Programmer... %s (%s)\n", PROGRAMMER, PROG_CODE); LMARG; printf("Assignment... %s (Rev %i)", ASSIGNMENT, REVISION); LMARG; printf("(Source Code in %s)\n", FILENAME); LMARG; printf("Description.. %s\n", TITLE); LMARG; printf(" %s\n", SUBTITLE); LMARG; printc('=', (78 - MARGIN)); putc('\n', stdout); } void TestSuite_u(void) { /*--------------------------------------------------------------------*/ /* PutV_u() Test Code */ /*--------------------------------------------------------------------*/ BLANKLINE; PutC('_'); PutC('u'); PutC(':'); PutC('\n'); Test_u(0); /* Smallest Possible Value */ Test_u(1); /* Tyical Value */ Test_u(UINT_MAX); /* Larget Possible Value */ return; } void TestSuite_i(void) { /*--------------------------------------------------------------------*/ /* PutV_i() Test Code */ /*--------------------------------------------------------------------*/ BLANKLINE; PutC('_'); PutC('i'); PutC(':'); PutC('\n'); Test_i(INT_MIN); /* Extreme Negative Value */ Test_i(-1); /* Typical Negative Value */ Test_i(0); /* Neither Positive nor Negative */ Test_i(1); /* Typical Positive Value */ Test_i(INT_MAX); /* Extreme Positive Value */ return; } void TestSuite_lu(void) { /*--------------------------------------------------------------------*/ /* PutV_lu() Test Code */ /*--------------------------------------------------------------------*/ BLANKLINE; PutC('_'); PutC('l'); PutC('u'); PutC(':'); PutC('\n'); Test_lu(0); /* Smallest Possible Value */ Test_lu(1); /* Tyical Value */ Test_lu(ULONG_MAX); /* Larget Possible Value */ return; } void TestSuite_li(void) { /*--------------------------------------------------------------------*/ /* PutV_li() Test Code */ /*--------------------------------------------------------------------*/ BLANKLINE; PutC('_'); PutC('l'); PutC('i'); PutC(':'); PutC('\n'); Test_li(LONG_MIN); /* Extreme Negative Value */ Test_li(-1); /* Typical Negative Value */ Test_li(0); /* Neither Positive nor Negative */ Test_li(1); /* Typical Positive Value */ Test_li(LONG_MAX); /* Extreme Positive Value */ return; } void TestSuite_f(void) { /*--------------------------------------------------------------------*/ /* PutV_f() Test Code */ /*--------------------------------------------------------------------*/ BLANKLINE; PutC('_'); PutC('f'); PutC(':'); PutC('\n'); Test_f(-FLT_MAX); /* Extreme Negative Value */ Test_f(-1); /* Typical Negative Value */ Test_f(-1.0/FLT_MAX); /* Extreme Miniscule Negative Value */ Test_f(0); /* Neither Positive nor Negative */ Test_f( 1.0/FLT_MAX); /* Extreme Miniscule Positive Value */ Test_f(1); /* Typical Positive Value */ Test_f( FLT_MAX); /* Extreme Positive Value */ return; } void TestSuite_lf(void) { /*--------------------------------------------------------------------*/ /* PutV_lf() Test Code */ /*--------------------------------------------------------------------*/ BLANKLINE; PutC('_'); PutC('l'); PutC('f'); PutC(':'); PutC('\n'); Test_lf(-DBL_MAX); /* Extreme Negative Value */ Test_lf(-1); /* Typical Negative Value */ Test_lf(-1.0/DBL_MAX); /* Extreme Miniscule Negative Value */ Test_lf(0); /* Neither Positive nor Negative */ Test_lf( 1.0/DBL_MAX); /* Extreme Miniscule Positive Value */ Test_lf(1); /* Typical Positive Value */ Test_lf( DBL_MAX); /* Extreme Positive Value */ return; } void TestSuite_e(void) { /*--------------------------------------------------------------------*/ /* PutV_e() Test Code */ /*--------------------------------------------------------------------*/ BLANKLINE; PutC('_'); PutC('e'); PutC(':'); PutC('\n'); Test_e(-FLT_MAX); /* Extreme Negative Value */ Test_e(-1); /* Typical Negative Value */ Test_e(-1.0/FLT_MAX); /* Extreme Miniscule Negative Value */ Test_e(0); /* Neither Positive nor Negative */ Test_e( 1.0/FLT_MAX); /* Extreme Miniscule Positive Value */ Test_e(1); /* Typical Positive Value */ Test_e( FLT_MAX); /* Extreme Positive Value */ return; } void TestSuite_le(void) { /*--------------------------------------------------------------------*/ /* PutV_le() Test Code */ /*--------------------------------------------------------------------*/ BLANKLINE; PutC('_'); PutC('l'); PutC('e'); PutC(':'); PutC('\n'); Test_le(-DBL_MAX); /* Extreme Negative Value */ Test_le(-1); /* Typical Negative Value */ Test_le(-1.0/DBL_MAX); /* Extreme Miniscule Negative Value */ Test_le(0); /* Neither Positive nor Negative */ Test_le( 1.0/DBL_MAX); /* Extreme Miniscule Positive Value */ Test_le(1); /* Typical Positive Value */ Test_le( DBL_MAX); /* Extreme Positive Value */ return; } /*=======================================================================*/ /* MAIN FUNCTION */ /*=======================================================================*/ /*== FUNCTION PROTOTYPES (to Document Primary Functions) ================*/ void PrintHeader(void); void TestSuite_u(void); void TestSuite_i(void); void TestSuite_lu(void); void TestSuite_li(void); void TestSuite_f(void); void TestSuite_lf(void); void TestSuite_e(void); void TestSuite_le(void); int main(void) { PrintHeader(); BLANKLINE; TestSuite_u(); TestSuite_i(); TestSuite_lu(); TestSuite_li(); TestSuite_f(); TestSuite_lf(); TestSuite_e(); TestSuite_le(); return EXIT_PASS; } /*=======================================================================*/ /* END OF SOURCE CODE FILE */ /*=======================================================================*/