/*=======================================================================*/ #define PROGRAMMER "SOLUTIONS, Nofrills" #define PROG_CODE "SOLN" #define COURSE "ECE-1021" #define YEAR (2004) #define TERM "Fall" #define SECTION (0) #define ASSIGNMENT "HW1" #define REVISION (1) #define TITLE "Character Functions" #define SUBTITLE "Last Modified on 06 SEP 04" #define EMAIL "ece1021@eas.uccs.edu" #define FILENAME "1_0SOLN.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 */ /*=======================================================================*/ /* Generate a Table of the Character Functions. Print a header with all of the column headings. For each row: (0 -> 150) Print the integer Print the character or, if not graphical, the name Print the results of the logical functions if True, print 'T', else print '-' Print the results of the conversion functions if graphical character, print the character, else print '.' */ /*=======================================================================*/ /* DEVELOPMENT NOTES */ /*=======================================================================*/ /* */ /*=======================================================================*/ /* PSEUDOCODE */ /*=======================================================================*/ /* 1) TASK: Print the Column Headings 2) TASK: Print the Table 2.1) SET: c = 0 2.2) WHILE: c <= 150 2.2.1) TASK: Print the integer c using exactly three spaces 2.2.2) TASK: Print the character (name if non graphical) 2.2.3) TASK: Print the results of the logical functions 2.2.4) TASK: Print the results of the conversion functions 2.2.5) SET: c = c + 1 */ /*=======================================================================*/ /* DEVIATIONS FROM SUBMITTED PSEUDOCODE */ /*=======================================================================*/ /* */ /*=======================================================================*/ /*=======================================================================*/ /* CODE SECTION */ /*=======================================================================*/ /*=======================================================================*/ /*== INCLUDE FILES ======================================================*/ #include /* stdout, putc(), printf() */ /*== 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; } void Put_ubase(unsigned int n, int base) { /* NOTE: 2 <= base <= 36 */ unsigned int m; int i; /* Determine how many digits there are */ for (m = 1; n/m >= base; m*=base ) /* EMPTY LOOP */; /* Print out the digits one-by-one */ do { for(i = 0; n >= m; i++ ) n = n - m; PutD(i); m = m / base; } while ( m >= 1 ); } /*=======================================================================*/ /* PRIMARY FUNCTIONS ( functions called directly by main() ) */ /*=======================================================================*/ /*== FUNCTION PROTOTYPES (to Document Support Functions) ================*/ int printc(char c, int 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); } int Putf_u(int d, int w, int how) { int digits; int wt; int chars; /* determine how many digits are required */ for(digits = 0, wt = 1; d/wt >= 10; digits++) wt *= 10; /* how many characters actually printed */ chars = 0; /* if how = 0, left justify the result */ /* if how = 1, right justify and pad with spaces */ /* if how = 3, right justify and pad with leading zeros */ if (how) { while ( (w-chars) > digits ) { PutC( (3==how)? '0': ' '); chars++; } } /* Output basic integer */ Put_u(d); chars += digits; /* Pad any remaining space in output field */ while ( w-chars ) { PutC(' '); chars++; } return chars; } int isascii(int c) { return (0 <= c) && (c <= 0x7F); } int iscntrl(int c) { return (c < ' ') || (c == 0x7F); } int isprint(int c) { return isascii(c) && !iscntrl(c); } int isgraph(int c) { return (' ' != c) && isprint(c); } int isspace(int c) { switch(c) { case ' ' : /* Blank Space */ case '\b': /* Back Space */ case '\t': /* Horizontal Tab */ case '\n': /* New Line */ case '\v': /* Vertical Tab */ case '\f': /* Form Feed */ case '\r': /* Carriage Return */ return TRUE; } return FALSE; } int isdigit(int c) { return ('0' <= c) && (c <= '9') ; } int ishexchar(int c) { return (('A' <= c)&&(c <= 'F')) || (('a' <= c)&&(c <= 'f')); } int isxdigit(int c) { return isdigit(c) || ishexchar(c); } int isupper(int c) { return ('A' <= c) && (c <= 'Z') ; } int islower(int c) { return ('a' <= c) && (c <= 'z') ; } int isalpha(int c) { return isupper(c) || islower(c); } int isalnum(int c) { return isdigit(c) || isalpha(c) ; } int ispunct(int c) { return isgraph(c) && !isalnum(c) ; } int toupper(int c) { return islower(c)? (c - 'a' + 'A') : c ; } int tolower(int c) { return isupper(c)? (c - 'a' + 'A') : c ; } void PutSymbol(int c) { /* Prints character or name if not a graphical character */ /* Always prints exactly three characters */ /* - right pads with spaces if necessary */ if(isgraph(c)) { PutC(' '); PutC(c); PutC(' '); } else { switch(c) { case 0x00: PutC('N'); PutC('U'); PutC('L'); break; case 0x01: PutC('S'); PutC('O'); PutC('H'); break; case 0x02: PutC('S'); PutC('T'); PutC('X'); break; case 0x03: PutC('E'); PutC('T'); PutC('X'); break; case 0x04: PutC('E'); PutC('O'); PutC('T'); break; case 0x05: PutC('E'); PutC('N'); PutC('Q'); break; case 0x06: PutC('A'); PutC('C'); PutC('K'); break; case 0x07: PutC('B'); PutC('E'); PutC('L'); break; case 0x08: PutC('B'); PutC('S'); PutC(' '); break; case 0x09: PutC('H'); PutC('T'); PutC(' '); break; case 0x0A: PutC('L'); PutC('F'); PutC(' '); break; case 0x0B: PutC('V'); PutC('T'); PutC(' '); break; case 0x0C: PutC('F'); PutC('F'); PutC(' '); break; case 0x0D: PutC('C'); PutC('R'); PutC(' '); break; case 0x0E: PutC('S'); PutC('O'); PutC(' '); break; case 0x0F: PutC('S'); PutC('I'); PutC(' '); break; case 0x10: PutC('D'); PutC('L'); PutC('E'); break; case 0x11: PutC('D'); PutC('C'); PutC('1'); break; case 0x12: PutC('D'); PutC('C'); PutC('2'); break; case 0x13: PutC('D'); PutC('C'); PutC('3'); break; case 0x14: PutC('D'); PutC('C'); PutC('4'); break; case 0x15: PutC('N'); PutC('A'); PutC('K'); break; case 0x16: PutC('S'); PutC('Y'); PutC('N'); break; case 0x17: PutC('E'); PutC('T'); PutC('B'); break; case 0x18: PutC('C'); PutC('A'); PutC('N'); break; case 0x19: PutC('E'); PutC('M'); PutC(' '); break; case 0x1A: PutC('S'); PutC('U'); PutC('B'); break; case 0x1B: PutC('E'); PutC('S'); PutC('C'); break; case 0x1C: PutC('F'); PutC('S'); PutC(' '); break; case 0x1D: PutC('G'); PutC('S'); PutC(' '); break; case 0x1E: PutC('R'); PutC('S'); PutC(' '); break; case 0x1F: PutC('U'); PutC('S'); PutC(' '); break; case 0x20: PutC('S'); PutC('P'); PutC(' '); break; case 0x7F: PutC('D'); PutC('E'); PutC('L'); break; default : PutC('?'); PutC('?'); PutC('?'); break; } } } void PrintBorder(void) { LMARG; INDENT; PutC('|'); printc('=', 67); PutC('|'); PutC('\n'); } void PrintHeadingLine1(void) { LMARG; INDENT; PutC('|'); /* | */ printc(' ', 2); PutC(' '); /* integer */ printc(' ', 5); PutC('c'); /* character */ printc(' ', 4); PutC(' '); /* isascii */ printc(' ', 3); PutC(' '); /* iscntrl */ printc(' ', 3); PutC(' '); /* isprint */ printc(' ', 3); PutC(' '); /* isgraph */ printc(' ', 3); PutC(' '); /* isspace */ printc(' ', 3); PutC(' '); /* ispunct */ printc(' ', 3); PutC(' '); /* isdigit */ printc(' ', 3); PutC(' '); /* isxdigit */ printc(' ', 3); PutC(' '); /* isalnum */ printc(' ', 3); PutC(' '); /* isalpha */ printc(' ', 3); PutC(' '); /* isupper */ printc(' ', 3); PutC(' '); /* islower */ printc(' ', 3); PutC(' '); /* toupper */ printc(' ', 3); PutC(' '); /* tolower */ PutC(' '); PutC('|'); PutC('\n'); } void PrintHeadingLine2(void) { LMARG; INDENT; PutC('|'); /* | */ printc(' ', 2); PutC('i'); /* integer */ printc(' ', 5); PutC('h'); /* character */ printc(' ', 4); PutC('i'); /* isascii */ printc(' ', 3); PutC('i'); /* iscntrl */ printc(' ', 3); PutC('i'); /* isprint */ printc(' ', 3); PutC('i'); /* isgraph */ printc(' ', 3); PutC('i'); /* isspace */ printc(' ', 3); PutC('i'); /* ispunct */ printc(' ', 3); PutC('i'); /* isdigit */ printc(' ', 3); PutC('i'); /* isxdigit */ printc(' ', 3); PutC('i'); /* isalnum */ printc(' ', 3); PutC('i'); /* isalpha */ printc(' ', 3); PutC('i'); /* isupper */ printc(' ', 3); PutC('i'); /* islower */ printc(' ', 3); PutC('t'); /* toupper */ printc(' ', 3); PutC('t'); /* tolower */ PutC(' '); PutC('|'); PutC('\n'); } void PrintHeadingLine3(void) { LMARG; INDENT; PutC('|'); /* | */ printc(' ', 2); PutC('n'); /* integer */ printc(' ', 5); PutC('a'); /* character */ printc(' ', 4); PutC('s'); /* isascii */ printc(' ', 3); PutC('s'); /* iscntrl */ printc(' ', 3); PutC('s'); /* isprint */ printc(' ', 3); PutC('s'); /* isgraph */ printc(' ', 3); PutC('s'); /* isspace */ printc(' ', 3); PutC('s'); /* ispunct */ printc(' ', 3); PutC('s'); /* isdigit */ printc(' ', 3); PutC('s'); /* isxdigit */ printc(' ', 3); PutC('s'); /* isalnum */ printc(' ', 3); PutC('s'); /* isalpha */ printc(' ', 3); PutC('s'); /* isupper */ printc(' ', 3); PutC('s'); /* islower */ printc(' ', 3); PutC('o'); /* toupper */ printc(' ', 3); PutC('o'); /* tolower */ PutC(' '); PutC('|'); PutC('\n'); } void PrintHeadingLine4(void) { LMARG; INDENT; PutC('|'); /* | */ printc(' ', 2); PutC('t'); /* integer */ printc(' ', 5); PutC('r'); /* character */ printc(' ', 4); PutC('a'); /* isascii */ printc(' ', 3); PutC('c'); /* iscntrl */ printc(' ', 3); PutC('p'); /* isprint */ printc(' ', 3); PutC('g'); /* isgraph */ printc(' ', 3); PutC('s'); /* isspace */ printc(' ', 3); PutC('p'); /* ispunct */ printc(' ', 3); PutC('d'); /* isdigit */ printc(' ', 3); PutC('x'); /* isxdigit */ printc(' ', 3); PutC('a'); /* isalnum */ printc(' ', 3); PutC('a'); /* isalpha */ printc(' ', 3); PutC('u'); /* isupper */ printc(' ', 3); PutC('l'); /* islower */ printc(' ', 3); PutC('u'); /* toupper */ printc(' ', 3); PutC('l'); /* tolower */ PutC(' '); PutC('|'); PutC('\n'); } void PrintHeadingLine5(void) { LMARG; INDENT; PutC('|'); /* | */ printc(' ', 2); PutC('e'); /* integer */ printc(' ', 5); PutC('a'); /* character */ printc(' ', 4); PutC('s'); /* isascii */ printc(' ', 3); PutC('n'); /* iscntrl */ printc(' ', 3); PutC('r'); /* isprint */ printc(' ', 3); PutC('r'); /* isgraph */ printc(' ', 3); PutC('p'); /* isspace */ printc(' ', 3); PutC('u'); /* ispunct */ printc(' ', 3); PutC('i'); /* isdigit */ printc(' ', 3); PutC('d'); /* isxdigit */ printc(' ', 3); PutC('l'); /* isalnum */ printc(' ', 3); PutC('l'); /* isalpha */ printc(' ', 3); PutC('p'); /* isupper */ printc(' ', 3); PutC('o'); /* islower */ printc(' ', 3); PutC('p'); /* toupper */ printc(' ', 3); PutC('o'); /* tolower */ PutC(' '); PutC('|'); PutC('\n'); } void PrintHeadingLine6(void) { LMARG; INDENT; PutC('|'); /* | */ printc(' ', 2); PutC('g'); /* integer */ printc(' ', 5); PutC('c'); /* character */ printc(' ', 4); PutC('c'); /* isascii */ printc(' ', 3); PutC('t'); /* iscntrl */ printc(' ', 3); PutC('i'); /* isprint */ printc(' ', 3); PutC('a'); /* isgraph */ printc(' ', 3); PutC('a'); /* isspace */ printc(' ', 3); PutC('n'); /* ispunct */ printc(' ', 3); PutC('g'); /* isdigit */ printc(' ', 3); PutC('i'); /* isxdigit */ printc(' ', 3); PutC('n'); /* isalnum */ printc(' ', 3); PutC('p'); /* isalpha */ printc(' ', 3); PutC('p'); /* isupper */ printc(' ', 3); PutC('w'); /* islower */ printc(' ', 3); PutC('p'); /* toupper */ printc(' ', 3); PutC('w'); /* tolower */ PutC(' '); PutC('|'); PutC('\n'); } void PrintHeadingLine7(void) { LMARG; INDENT; PutC('|'); /* | */ printc(' ', 2); PutC('e'); /* integer */ printc(' ', 5); PutC('t'); /* character */ printc(' ', 4); PutC('i'); /* isascii */ printc(' ', 3); PutC('r'); /* iscntrl */ printc(' ', 3); PutC('n'); /* isprint */ printc(' ', 3); PutC('p'); /* isgraph */ printc(' ', 3); PutC('c'); /* isspace */ printc(' ', 3); PutC('c'); /* ispunct */ printc(' ', 3); PutC('i'); /* isdigit */ printc(' ', 3); PutC('g'); /* isxdigit */ printc(' ', 3); PutC('u'); /* isalnum */ printc(' ', 3); PutC('h'); /* isalpha */ printc(' ', 3); PutC('e'); /* isupper */ printc(' ', 3); PutC('e'); /* islower */ printc(' ', 3); PutC('e'); /* toupper */ printc(' ', 3); PutC('e'); /* tolower */ PutC(' '); PutC('|'); PutC('\n'); } void PrintHeadingLine8(void) { LMARG; INDENT; PutC('|'); /* | */ printc(' ', 2); PutC('r'); /* integer */ printc(' ', 5); PutC('e'); /* character */ printc(' ', 4); PutC('i'); /* isascii */ printc(' ', 3); PutC('l'); /* iscntrl */ printc(' ', 3); PutC('t'); /* isprint */ printc(' ', 3); PutC('h'); /* isgraph */ printc(' ', 3); PutC('e'); /* isspace */ printc(' ', 3); PutC('t'); /* ispunct */ printc(' ', 3); PutC('t'); /* isdigit */ printc(' ', 3); PutC('i'); /* isxdigit */ printc(' ', 3); PutC('m'); /* isalnum */ printc(' ', 3); PutC('a'); /* isalpha */ printc(' ', 3); PutC('r'); /* isupper */ printc(' ', 3); PutC('r'); /* islower */ printc(' ', 3); PutC('r'); /* toupper */ printc(' ', 3); PutC('r'); /* tolower */ PutC(' '); PutC('|'); PutC('\n'); } void PrintHeadingLine9(void) { LMARG; INDENT; PutC('|'); /* | */ printc(' ', 2); PutC(' '); /* integer */ printc(' ', 5); PutC('r'); /* character */ printc(' ', 4); PutC(' '); /* isascii */ printc(' ', 3); PutC(' '); /* iscntrl */ printc(' ', 3); PutC(' '); /* isprint */ printc(' ', 3); PutC(' '); /* isgraph */ printc(' ', 3); PutC(' '); /* isspace */ printc(' ', 3); PutC(' '); /* ispunct */ printc(' ', 3); PutC(' '); /* isdigit */ printc(' ', 3); PutC('t'); /* isxdigit */ printc(' ', 3); PutC(' '); /* isalnum */ printc(' ', 3); PutC(' '); /* isalpha */ printc(' ', 3); PutC(' '); /* isupper */ printc(' ', 3); PutC(' '); /* islower */ printc(' ', 3); PutC(' '); /* toupper */ printc(' ', 3); PutC(' '); /* tolower */ PutC(' '); PutC('|'); PutC('\n'); } void PrintHeadings(void) { PrintBorder(); PrintHeadingLine1(); PrintHeadingLine2(); PrintHeadingLine3(); PrintHeadingLine4(); PrintHeadingLine5(); PrintHeadingLine6(); PrintHeadingLine7(); PrintHeadingLine8(); PrintHeadingLine9(); PrintBorder(); } void PutLogicColumn(int true) { PutC(' '); PutC( true ? TSYM : FSYM ); PutC(' '); PutC('|'); } void PutCharacterColumn(int c) { PutC(' '); PutC( isgraph(c) ? c : '.' ); PutC(' '); PutC('|'); } /*=======================================================================*/ /* MAIN FUNCTION */ /*=======================================================================*/ /*== FUNCTION PROTOTYPES (to Document Primary Functions) ================*/ void PrintHeader(void); int main(void) { int c; PrintHeader(); BLANKLINE; PrintHeadings(); for(c = 0; c < 150; c++) { LMARG; INDENT; PutC('|'); PutC(' '); Putf_u(c, 2, 3); PutC(' '); PutC(':'); PutC(' '); PutSymbol(c); PutC(' '); PutC('|'); PutC(' '); PutC( isascii(c) ? TSYM : FSYM ); PutC(' '); PutC('|'); PutC(' '); PutC( iscntrl(c) ? TSYM : FSYM ); PutC(' '); PutC('|'); PutC(' '); PutC( isprint(c) ? TSYM : FSYM ); PutC(' '); PutC('|'); PutC(' '); PutC( isgraph(c) ? TSYM : FSYM ); PutC(' '); PutC('|'); PutC(' '); PutC( isspace(c) ? TSYM : FSYM ); PutC(' '); PutC('|'); PutC(' '); PutC( ispunct(c) ? TSYM : FSYM ); PutC(' '); PutC('|'); PutC(' '); PutC( isdigit(c) ? TSYM : FSYM ); PutC(' '); PutC('|'); PutC(' '); PutC( isxdigit(c) ? TSYM : FSYM ); PutC(' '); PutC('|'); PutC(' '); PutC( isalnum(c) ? TSYM : FSYM ); PutC(' '); PutC('|'); PutC(' '); PutC( isalpha(c) ? TSYM : FSYM ); PutC(' '); PutC('|'); PutC(' '); PutC( isupper(c) ? TSYM : FSYM ); PutC(' '); PutC('|'); PutC(' '); PutC( islower(c) ? TSYM : FSYM ); PutC(' '); PutC('|'); PutC(' '); if (isgraph(toupper(c))) PutC(tolower(c)); else PutC('.'); PutC(' '); PutC('|'); PutC(' '); if (isgraph(tolower(c))) PutC(tolower(c)); else PutC('.'); PutC(' '); PutC('|'); PutC('\n'); } PrintBorder(); return EXIT_PASS; } /*=======================================================================*/ /* END OF SOURCE CODE FILE */ /*=======================================================================*/