Presentation is loading. Please wait.

Presentation is loading. Please wait.

Homework K&R chapter 4. HW3: ASCII Hex to Integer int axtoi (char s[ ]) { int i, n, flag; n = 0; flag = 1; for ( i = 0; flag; i++) /* for (i = 0; ; i++)

Similar presentations


Presentation on theme: "Homework K&R chapter 4. HW3: ASCII Hex to Integer int axtoi (char s[ ]) { int i, n, flag; n = 0; flag = 1; for ( i = 0; flag; i++) /* for (i = 0; ; i++)"— Presentation transcript:

1 Homework K&R chapter 4

2 HW3: ASCII Hex to Integer int axtoi (char s[ ]) { int i, n, flag; n = 0; flag = 1; for ( i = 0; flag; i++) /* for (i = 0; ; i++) with ending 2 */ switch (s[i]) { case '0':case '1':case '2':case '3':case '4': /* fall... */ case '5':case '6':case '7':case '8':case '9': /* through */ n = 16*n + (s[i] - '0'); break; /* need this so it doesn't fall through */

3 ASCII Hex to Integer (Ending 1) case 'a':case 'b':case 'c':case 'd':case 'e': case 'f': n = 16*n + (s[i] - 'a' + 10); break; /* could also have cases for A... F here */ default: /* stop “for” loop on non hex digit */ flag = 0; break; } return n; }

4 ASCII Hex to Integer (Ending 2) case 'a':case 'b':case 'c':case 'd':case 'e': case 'f': n = 16*n + (s[i] - 'a' + 10); break; /* could also have cases for A... F here */ default: /* stop “for” loop on non hex digit */ goto ret; } ret: return n; }

5 Multi-Module Programs (Glass & Ables Linux Book p. 411) To compile all.c files and create executable gcc main.c getop.c stack.c getch.c –o calcit To compile only one and create executable with objects of previously compiled files gcc main.c getop.o stack.o getch.o –o calcit To build an executable from a complex set of separate files skipping any unneeded steps make –f makefile (makefile documents build rules) (-f option allows you to specify makefile name)

6 Dependency Tree for a Build calcit main.ogetop.ostack.ogetch.o main.cgetop.cstack.cgetch.c getop.hstack.hgetch.h

7 Make (Glass & Ables p. 412) makefile rule format target:dependency list for needed files command list to create target Example: calcit: main.o getop.o stack.o getch.o gcc main.o getop.o stack.o getch.o -o calcit main.o: main.c getop.h stack.h gcc -c main.c ….. Just compile, no linking

8 Macros in Make Macro CC is defined early in the makefile with the line: CC = gcc Then using $(CC) causes substitution calcit: main.o getop.o stack.o getch.o $(CC) main.o getop.o stack.o getch.o -o calcit You can override the macro in the make command line make CC=gccx86

9 Make The user can type the command "make", or make with a named target, for example: “make getop.o” to cause the rules leading up to the target getop.o to be executed, if needed. When user types "make" without a target name, the FIRST rule listed will be the default target constructed. Thus, the "calcit" rule should be the first rule in the makefile

10 Make At the end of the makefile, there is a rule with a target name "clean". clean: rm *.o Since "clean" is not a file: –There is no dependency list (ages of files don't matter) –It is called when you give the command "make clean". –It deletes.o files to reduce the clutter in the directory.

11 Implicit Build Rules makefile can use implicit rule to build intermediate files, e.g. calcit: main.o getop.o stack.o getch.o gcc main.o getop.o stack.o getch.o -o calcit main.o: main.c getop.h stack.h getop.o:getop.c getop.h getch.h …… Since there is no explicit rule, the files main.o, getop.o … will be built using built-in implicit rules. For C, it is using a compiler named CC with options CFLAGS, e.g CC= gcc CFLAGS = -m32 calcit: main.o getop.o stack.o getch.o $(CC) $(CFLAGS) main.o getop.o stack.o getch.o -o calcit main.o:…. You can bypass the implicit rules by defining pattern rules (see https://www.gnu.org/software/make/manual/html_node/Implicit-Rules.html for details ) No build rules

12 Header Files Principles for contents of.h files –Each.h file includes all of the function prototypes and symbolic constants needed to invoke those functions int function (int ); /* no code for function */ #define VALUE0_FOR_ARGUMENT 0 –No statements that allocate memory in.h files!! Don’t use a single “calc.h” file (K&R, pg 82) A reusable solution shown in “Dependency Tree” –An xxx.h file for each xxx.c source file of functions –#include getop.h in getop.c and all source files that call functions provided in the getop.c source file (i.e. main)

13 Header Files – Math Library When you want to use math functions Example: SIN( ) or SQRT( ) Write #include But also invoke compiler with special flag: gcc... -lm (lm means library, math) *Note: -lm has to be at the end

14 Reverse Polish Notation(RPN) For hw4, you need to know how to convert an algebraic (infix) expression to RPN Some calculators work this way, e.g H-P Algebraic(123 + 21) * (567 – 432) RPN123 21 + 567 432 - * No parentheses are needed! How does that work?

15 Reverse Polish Notation Enter: 123 21 + 567 432 - * Stack States: 123 21144 567 144 567 432 144 13519440 Empty

16 RPN Calculator Program Pseudo-code for RPN calculator (See K&R, pg 75) while (next the character is not an EOF) if (number) push it on the stack else if (operator) pop operand(s) /* may be one or two */ do operation push result back on the stack else if (newline) pop and print value from top of stack else error

17 RPN Calculator Program See K&R, page 76 and then page 78 Operand order is not important for some ops +, *, &, | Operand order is important for some ops! -, /, % There is only one operand for some ops ~, !,

18 RPN Calculator Program Break down the pseudo-code into functions –push / pop –get next number or operator, i.e. get op ( ) –getch / ungetch Both push and pop access a set of variables for the stack  static external Both getch and ungetch access a buffer for characters from stdin  static external

19 Communication Between Functions Sometimes closely related functions need to share access to variables in situations where they never call each other and therefore can’t pass arguments to the shared variables Ex: push and pop share the stack variables Declare “static” variables inside source file for the functions (before / outside the braces for any of the functions)  “Static External” per K&R Makes them “globally” accessible to all functions in the source file

20 Back to RPN Calculator Program Look at getop, getch, ungetch functions In getop( ), we try to input a char string for a number. It uses getch() to read a char. If it is a number, how do we know where the number ends? But if the next char is an operator (e.g. '+'), then we know the string of numbers has ended. But, we have to put the operator back in stdin. We do this with ungetch( ) Next invocation of getch() will read the operator from stdin.

21 getop.c /* * getop.c * * gets next token: operator or numeric operand */ #include #include "calc.h" int getop(char s[]) { int i, c; while ((s[0] = c = getch()) = = ' ' || c = = '\t') ; s[1] = '\0'; if (!isdigit(c) && c != '.') return c; /* not a number */

22 getop.c (cont’d) /* collect integer part in string s */ i = 0; if (isdigit(c)) while (isdigit(s[++i] = c = getch())) ; /* collect fractional part in string s */ if (c = = '.') while (isdigit(s[++i] = c = getch())) ; s[i] = '\0'; if (c != EOF) ungetch(c); return NUMBER; }

23 getch.c #define BUFSIZE 100 static char buf[BUFSIZE]; /* buffer for ungetch */ static int bufp = 0; /* next free position in buf */ /* get a possibly pushed back char from stdin */ int getch(void) { return (bufp > 0) ? buf[--bufp] : getchar( ); }

24 ungetch.c /* push char back for getch later */ void ungetch(int c) { if (bufp >= BUFSIZE) printf("ungetch: too many characters in buffer.\n"); else buf[bufp++] = c; }


Download ppt "Homework K&R chapter 4. HW3: ASCII Hex to Integer int axtoi (char s[ ]) { int i, n, flag; n = 0; flag = 1; for ( i = 0; flag; i++) /* for (i = 0; ; i++)"

Similar presentations


Ads by Google