CS 1320 (Principles of Algorithm Design I):
Homework #9
- Assigned:
- April 19, 2000.
- Due:
- April 26, 2000, at start of class.
- Credit:
- 20 points (10 points per problem).
Instructions
Write a C++ program to solve each of the problems below.
Your code should begin with comments that describe
what the program is supposed to do, in terms of its inputs
and outputs.
Any function you write should begin with brief comments
describing its purpose and preconditions/postconditions.
See the sample programs
for examples.
Problems
Currency conversion
For this problem, you are to write a simple currency-conversion
program that will convert one currency to another, using a list of
exchange rates provided in a file.
The program should work as follows:
- Prompt the user for the name of the file containing the
list of exchange rates.
Each line of this file will contain
a "from" currency (a text string with
no embedded blanks)
a "to" currency (a text string with no
embedded blanks), and an exchange rate (a number,
not necessarily an integer).
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.
If the user enters a q, the program should quit.
If the user enters an s, the program should print
out all the exchange rates in the file, one per line.
(You do not need to worry about making things line up in
columns; just print the rates one per line in any format
that is easy to do.)
If the user enters a c, the program should:
- 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.
For example, using the exchange rates listed above:
- If the user enters "dollars", "pounds", and 100,
the program should print 25 (100 times the
conversion factor 0.25).
- If the user enters "dollars" and "euros", the
program should print an error message (no
such conversion was found).
The program does not need to compute reverse conversion
factors; for example, the exchange rates listed above
specify a conversion from yen to dollars but no conversion
from dollars to yen, so the program would not need to
convert dollars to yen.
The program should not make any assumptions about the
number of exchange rates. (Hence it should not try to store all
the exchange rates in an array.)
You are free to obtain data for the table of exchange rates from
any source you like, including making it up, or you may use file
sample_rates.data
(data for which was obtained from Yahoo's Finance Site at
http://finance.yahoo.com/m3?u).
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.
What to turn in
Submit your source code as described in the
guidelines for programming assignments.
For this assignment:
- Use a subject header of "cs1320 homework 9".
- Submit two source files, one for each problem.
Please choose names for the files that indicate
which program solves which problem.
(For example,
you could use convert_currency.cc and
simple_grep.cc.)
You do not need to submit any input files;
I will supply the required input files when I test your
programs.