Jump to: navigation, search

Chapter 4 - Functions and Program Structure

If you are intending to attempt solutions 4-3 through 4-10, you will find testing your solution to be a somewhat less laborious task if you download the following code:

Kernighan and Ritchie's Polish calculator source

It was when he was typing up this lot that the problem of copyright first occurred to Richard Heathfield - after all, it's quite a substantial quote from the book. Fortunately for Richard, Dennis Ritchie had no objections (but he did check to make sure there wouldn't be any other verbatim quotes of comparable size!).

Ex No Pg No Exercise Solution Solved by
1 71

Write the function strrindex(s,t) , which returns the position of the rightmost occurrence of t in s , or -1 if there is none.

Listing krx401 Rick Dearman
2 73

Extend atof to handle scientific notation of the form 123.45e-6 where a floating-point number may be followed by e or E and an optionally signed exponent.

Listing krx402 Dann Corbit
3 79

Given the basic framework, it's straightforward to extend the calculator. Add the modulus ( % ) operator and provisions for negative numbers.

Listing krx403 Bob Wightman
4 79

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.

Listing krx404 Bob Wightman
5 79

Add access to library functions like sin , exp , and pow . See <math.h> in Appendix B, Section 4.

Listing krx405 Bob Wightman
6 79

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.

Listing krx406 Cat 1 Solution by Bob Wightman
Jesus Alvarez
7 79

Write a routine ungets(s) that will push back an entire string onto the input. Should ungets know about buf and bufp , or should it just use ungetch ?

Listing krx407 Steven Huang
8 79

Suppose that there will never be more than one character of pushback. Modify getch and ungetch accordingly.

Listing krx408 Steven Huang
Jesus Alvarez
9 79

Our getch and ungetch do not handle a pushed-back EOF correctly. Decide what their properties ought to be if an EOF is pushed back, then implement your design.

Listing krx409 Franz Fritsche
Jesus Alvarez
10 79

An alternate organization uses getline to read an entire input line; this makes getch and ungetch unnecessary. Revise the calculator to use this approach.

Listing krx410 Franz Fritsche
11 83

Modify getop so that it doesn't need to use ungetch. Hint: use an internal static variable.

Listing krx411 Gregory Pietsch
12 88

Adapt the ideas of printd to write a recursive version of itoa ; that is, convert an integer into a string by calling a recursive routine.

Listing krx412 Gregory Pietsch
Jesus Alvarez
13 88

Write a recursive version of the function reverse(s) , which reverses the string s in place.

Listing krx413 Gregory Pietsch
Jesus Alvarez
14 91

Define a macro swap(t,x,y) that interchanges two arguments of type t . (Block structure will help.)

Listing krx414 Gregory Pietsch, Lars Wirzenius
Personal tools