4 - Functions and Program Structure
Exercise 4-1
Write the function
strrindex(s, t)
, which returns the position of the rightmost occurrence oft
ins
, or-1
if there is none. [1]
My solution
tbd
Official solution
tbd
Exercise 4-2
Extend
atof
to handle scientific notation of the form123.45e-6
where a floating-point number may be followed by
e
orE
and an optionally signed exponent. [1]
My solution
tbd
Official solution
tbd
Exercise 4-3
Given the basic framework, it's straightforward to extend the calculator. Add the modulus (
%
) operator and provisions for negative numbers. [1]
My solution
tbd
Official solution
tbd
Exercise 4-4
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. [1]
My solution
tbd
Official solution
tbd
Exercise 4-5
Add access to library functions like
sin
,exp
, andpow
. See<math.h>
in Appendix B, Section 4. [1]
My solution
tbd
Official solution
tbd
Exercise 4-6
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. [1]
My solution
tbd
Official solution
tbd
Exercise 4-7
Write a routine
ungets(s)
that will push back an entire string onto the input. Shouldungets
know aboutbuf
andbufp
, or should it just useungetch
? [1]
My solution
tbd
Official solution
tbd
Exercise 4-8
Suppose that there will never be more than one character of pushback. Modify
getch
andungetch
accordingly. [1]
My solution
tbd
Official solution
tbd
Exercise 4-9
Our
getch
andungetch
do not handle a pushed-backEOF
correctly. Decide what their properties ought to be if anEOF
is pushed back, then implement your design. [1]
My solution
tbd
Official solution
tbd
Exercise 4-10
An alternate organization uses
getline
to read an entire input line; this makesgetch
andungetch
unnecessary. Revise the calculator to use this approach. [1]
My solution
tbd
Official solution
tbd
Exercise 4-11
Modify
getop
so that it doesn't need to useungetch
. Hint: use an internalstatic
variable. [1]
My solution
tbd
Official solution
tbd
Exercise 4-12
Adapt the ideas of
printd
to write a recursive version ofitoa
; that is, convert an integer into a string by calling a recursive routine. [1]
My solution
tbd
Official solution
tbd
Exercise 4-13
Write a recursive version of the function
reverse(s)
, which reverses the strings
in place. [1]
My solution
tbd
Official solution
tbd
Exercise 4-14
Define a macro
swap(t, x, y)
that interchanges two arguments of typet
. (Block structure will help.) [1]
My solution
tbd
Official solution
tbd