From: Steven Baltakatei Sandoval Date: Mon, 20 Feb 2023 21:57:06 +0000 (+0000) Subject: feat(src/tm-notes/ch3.tm):Add ch1-8 exercises from K&R X-Git-Url: https://zdv2.bktei.com/gitweb/BK-2023-05.git/commitdiff_plain/c8f3c7949f21cb0b9add9c08ba2c9c174f6465cf feat(src/tm-notes/ch3.tm):Add ch1-8 exercises from K&R --- diff --git a/src/tm-notes/ch3.tm b/src/tm-notes/ch3.tm index 2249160..5e6b739 100644 --- a/src/tm-notes/ch3.tm +++ b/src/tm-notes/ch3.tm @@ -233,6 +233,503 @@ + + + + + + + + + <\description> + Our binary search makes two tests inside th eloop, + when one would suffice (at the price of more tests outside). Write a + version with only one test inside the loop and measure the difference in + run-time. + + + + + <\description> + Write a function that converts + characters like newline and tab into visible escape sequences like + and as it copies the string to . Use a + . Write a function for the other direction as well, + converting escape sequences into the real characters. + + + + + <\description> + Write a function that expands + shorthand notations like in the string into the + equivalent complete list in . Allow for letters + of either case and digits, and be prepared to handle cases like + and and . Arrange that a leading or + trailing is taken literally. + + + <\subsection> + Sec 3.6: Loops\UDo-while + + + <\description> + In a two's complement number representation, our + version of does not handle the largest negative number, that + is, the value of equal to -1>|)>>. + Explain why not. Modify it to print that value correctly, regardless of + the machine on which it runs. + + Write the function that converts + the integer into a base character representation in the + string . In particular, formats as a + hexadecimal integer in . + + Write a version of that accepts three + arguments instead of two. The third argument is a minimum field width; + the converted number must be padded with blanks on the left if necessary + to make it wide enough. + + + + + + + + + + + <\description> + Write the function , which + returns the position of the occurence of in + , or if there is none. + + + + + <\description> + Extend to handle scientific notation of + the form + + <\cpp-code> + 123.45e-6 + + + where a floating-point number may be followed by or and + an optionally signed exponent. + + + + + <\description> + Given the basic framework, it's straightforward to + extend the calculator. Add the modulus () operator and provisions + for negative numbers. + + Add commands to print the top element of the stack + without popping, to duplicate it, and to swap the top two elements. Add a + command to clear the stack. + + Add access to library functions like , + , and . See math.h\> in Appendix B, + Section 4. + + Add commands for handling variables. (It's easy to + provide twenty-six variables with single-letter names.) Add a variable + for the most recently printed value. + + Write a routine that will push back + an entire string onto the input. Should know about + and , or should it just use ? + + Suppose that there will never be more than one + character of pushback. Modify and accordingly. + + Our and do not handle a + pushed-back correctly. Decide what their properties ought to be + if an is pushed back, then implement your design. + + An alternate organization uses to read + an entire input line; this makes andj + unnecessary. Revise the calculator to use this approach. + + + + + + + + + + + + + + + + + <\description> + Adapt the ideas of to write a recursive + version of ; that is, convert an integer into a string by + calling a recursive routine. + + Write a recursive version of the function + , which reverses the string in place. + + + + + + + + + <\description> + Define a macro that interchanges + two arguments of type . (Block structure will help.) + + + + + + + + + + + <\description> + As written, treats a or + not followed by a digit as a valid representation of zero. Fix it to push + such a character back onto the input. + + Write , the floating-point analog of + . What type does return as its function value? + + + + + + + + + <\description> + Write a pointer version of the function + that we showed in Chapter 2: copies the string + to the end of . + + Write the function , which returns + if the string occurs at the end of the string , + and zero otherwise. + + Write versions of the library functions + , , and , which operate on at most + the first characters of their argument strings. For example, + copies at most characters of to + . Full descriptions are in Appendix B. + + Rewrite appropriate programs from earlier chapters + and exercises with pointers instead of array indexing. Good possibilities + include (Chapters 1 and 4), , , and + their variants (Chapters 2, 3, and 4), (Chapter 3), and + and (Chapter 4). + + + + + <\description> + Rewrite to store lines in an array + supplied by , rather than calling to maintain + storage. How much faster is the program? + + + + + <\description> + There is no error checking in or + . Remedy this defect. + + + + + + + <\description> + Rewrite the routines and + with pointers instead of reindexing. + + + + + \; + + <\description> + Write the program , which evaluates a + reverse Polish expression fromt he command line, where each operator or + operand is a separate argument. For example, + + <\cpp-code> + expr \ 2 \ 3 \ 4 \ + \ * + + + evaluates >. + + Modify the programs and + (written as exercises in Chapter 1) to accept a list of tab stops as + arguments. Use the default tab settings if there are no arguments. + + Extend and to accept the + shorthand + + <\cpp-code> + entab -> +> + + + to mean tab stops every columns, starting at column . Choose + convenient (for the user) default behavior. + + Write the program , which prints the last + lines of its input. By default, is , let us say, + but it canb e changed by an optional argument so that + + <\cpp-code> + tail -> + + + prints the last lines. The program should behave rationally no + matter how unreasonable the input or the value of . Write the + program so it makes the best use of available storage; lines should be + stored as in the sorting program of Section 5.6, not in a two-dimensional + array of fixed size. + + + + + <\description> + Modify the sort program to handle a flag, + which indicates sorting in reverse (decreasing) order. Be sure that + works with . + + Add the option to fold upper and lower case + together, so that case distinctions are not made during sorting; for + example, and compare equal. + + Add the (\Pdirectory\Q) option, which makes + comparisons only on letters, numbers, and blanks. Make sure it works in + conjunction with . + + Add a field-handling capability, so sorting may be + done on fields within lines, each field sorted according to an + independent set of options. (The index for this book was sorted with + for the index category and for the page numbers.) + + + + + <\description> + Make recover from input errors. + + Modify so that it does not add redundant + parentheses to declarations. + + Expand to handle declarations with + function argument types, qualifiers like , and so on. + + + + + + + + + + + <\description> + Our version of does not properly handle + underscores, string constants, comments, or preprocessor control lines. + Write a better version. + + + + + + + <\description> + Write a program that reads a program and + prints in alphabetical order each group of variable names that are + identical in the first characters, but different somewhere + thereafter. Don't count words within strings and comments. Make + a parameter that can be set from the command line. + + Write a cross-referencer that prints a list of all + words in a document, and, for each word, a lis tof the line numbers on + which it occurs. Remove noise words like \Pthe\Q, \Pand\Q, and so on. + + Write a program that prints the distinct words in its + input sorted into decreasing order of frequency of occurrence. Precede + each word by its count. + + + + + <\description> + Write a function that will remove a name + and definition from the table maintained by and + . + + Implement a simple version of the + processor (i.e., no arguments) suitable for use with programs, + based on the routines of this section. You may also find and + helpful. + + + + + + + + + + + + + <\description> + Write a program that converts upper case to lower or + lower case to upper, depending on the name it is invoked with, as found + in . + + + + + <\description> + Write a program that will print arbitrary input in a + sensible way. AS a minimum, it ishould print non-graphic characters in + octal or hexadecimal according to local custom, and break long text + lines. + + + + + <\description> + Revise to handle more of the other + facilities of . + + + + + <\description> + Write a private version of analogous to + from the previous section. + + Rewrite the postfix calculator of Chapter 4 to use + and/or to do the input and number conversion. + + + + + + + + + <\description> + Write a program to compare two files, printing the + first line where they differ. + + Modify the pattern finding program of Chapter 5 to + take its input from a set of named files or, if no files are named as + arguments, from the standard input. Should the file name be printed when + a matching line is found? + + Write a program to print a set of files, starting + each new one on a new page, with a title and a running page count for + each file. + + + + + + + + + + + + + + + + + + + <\description> + Functions like can be implemented to + save space or to save time. Explore both possibilities. + + + + + + + + + + + <\description> + Rewrite the program from Chapter 7 using + , , , and instead of their + standard library equivalents. Perform experiemnts to determine the + relative speeds of the two versions. + + + + + + + <\description> + Rewrite and with fields + instead of explicit bit operations. Compare code size and execution + speed. + + Design and write , , and + . + + The standard library function + + <\cpp-code> + int fseek(FILE *fp, long offset, int origin) + + + is identical to except that is a file pointer + instead of a file descriptor and the return value is an status, + not a position. Write . Make sure that your + coordinates properly with the buffering done for the other functions of + the library. + + + + + <\description> + Modify the program to print the other + information contained in the inode entry. + + + + + <\description> + The standard library function + returns a pointer to objects of size , with the storage + initialized to zero. Write , by calling or by + modifying it. + + accepts a size request without checking + its plausibility; believes that the block it is asked to free + contains a valid size field. Improve these routins so they take more + pains with error checking. + + Write a routine that will free an + arbitrary block of characters into the free list + maintained by and . By using , a user + can add a static or external array to the free list at any time. + + \; \; @@ -248,6 +745,7 @@ <\collection> > > + > > > > @@ -269,13 +767,82 @@ > > > - > + > + > + > + > + > + > + > + > + > + > > + > + > + > + > + > + > + > + > + > + > > + > + > + > + > + > + > + > + > + > + > > + > + > + > + > + > + > + > + > + > + > > + > + > + > + > + > + > + > + > + > + > > + > + > + > + > + > + > + > + > + > + > > + > + > + > + > + > + > + > + > + > + >