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:
  1. 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
    
  2. Prompt the user repeatedly to enter one of the following commands:

    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:

    1. Prompt the user for "from" and "to" currencies.
    2. Look up the corresponding exchange rate in the list.
    3. 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: 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: