CS 1320 (Principles of Algorithm Design I):
Homework #6
- Assigned:
- October 25, 1999.
- Due:
- November 1, 1999, at start of class.
- Credit:
- 20 points.
Programming problems
- Credit:
- 10 points each.
- Instructions:
- Write a C++ program to solve each of the problems below.
For each program, unless otherwise instructed, turn in:
- A printed listing of your program.
It should begin with comments giving your
name, the assignment number, and a description
of what the program does in terms of its inputs
and outputs. If you make choices or assumptions
in writing the program, this is the place to
mention them.
- If the program requires input, the input(s) you
used to test it.
- Your program's output, when executed with the input(s)
you used to test it.
See the
guidelines for
programming assignments for a more detailed description.
Remember that at least one other human will read your program,
so try to make it clear and readable. See the guidelines
for tips on what makes for readable code, and the
sample programs
for examples.
Simplified "grep"
In this problem, we will construct a program similar to the UNIX
grep command. Given a pattern, i.e., a string, and a
filename, grep prints all the lines that contain the pattern.
(This program is similar to the "find" function on a word processor,
where you choose "find", type a pattern,
and then the word processor shows you
all the matches. grep is similar except that it shows you all
lines that contain the pattern.)
For example, suppose we have a file called foo
that consists of the following lines:
1776
hello, world
21478756282
Given this file and a pattern of 1,
the program should print the first and third lines, because these lines
contain the pattern 1.
Your program should ask the user for a pattern and a filename. Then
it should print each line in the file that matches the pattern.
Here are some examples of how the program should run, given the
above file foo and assuming the program is called
mygrep.
(Here, text in italics is typed in by the user.)
$ mygrep
What is the pattern? 1
What is the file? foo
1776
21478756282
$ mygrep
What is the pattern? hello
What is the file? foo
hello, world
$ mygrep
What is the pattern? tr
What is the file? foo
$ mygrep
What is the pattern? wor
What is the file? foo
hello, world
$ mygrep
What is the pattern? 21
What is the file? foo
21478756282
Notice that if the pattern does not occur in the file (as in the example
with a pattern of tr), the program does not print anything.
When possible, define a function for each subtask. It may be useful
to declare a function that determines whether the pattern occurs
in the string. Be sure to check for the case that the pattern is
longer than the string.
You may assume that each line of the file, even the last line, ends
with a newline character '\n'.
You may also assume that the pattern contains no whitespace.
Currency conversion revisited
In a previous homework we wrote a simple currency-conversion program.
Its user interface was less than ideal, in part because we did not
have the tools to deal with text strings or obtaining input from more
than one source. For this homework, you are to write an improved
currency-conversion program that will read a similarly-formatted list
of currencies and conversion factors from a file (filename to be
provided by the user) and then read a sequence of conversions from
standard input. The program should work as follows:
- Prompt the user for the name of the file containing the
list of exchange rates.
As in the previous homework, each line of this file
will contain a "from" currency, a "to" currency,
and an exchange rate,
but this time the currency names will be strings.
For example, the file might contain these lines:
dollars pounds 0.25
pounds dollars 4.0
dollars rubles 1000.0
yen dollars 10.0
- Prompt the user repeatedly to enter one of the following commands:
- s to show all exchange rates in the file.
- c to perform a conversion.
- q to quit the program.
The program should quit in response to a q,
print out all exchange rates, one per line,
in response to a s, and
in response to a c, it should do the following:
- Prompt the user for "from" and "to" currencies.
- Look up the corresponding exchange rate in the list.
- If found, prompt the user for an amount to convert,
do the conversion, and print the result.
If not found, print an appropriate error message.
The program should not make any assumptions about the
number of exchange rates. (Hence it should not use arrays to store
the exchange rates.)
Hints, tips, and other help