Section 0101, 0102, 0103 and Honors, Fall 1995

Project 5

Due: Friday, December 8, 1995


Practice working with pointers and parameter passing by reference.


Your assignment is to implement the ScanFraction function which has the following function prototype:
     int ScanFraction(string str, int index, fraction *fptr) ;
When ScanFraction is called it looks for a fraction in the string str starting at position index. If a fraction is found then the value of the fraction is stored in the fraction that the pointer fptr points to. Moreover, ScanFraction returns the position of the character immediately after the fraction it found. For example, after the following function call
     new_index = ScanFraction("abc 23/45 xyz", 3, &frac) ;
the fraction frac will have the value 23/45 and new_index will have the value 9.

If ScanFraction fails to find a correctly formatted fraction, then it will return a negative number to indicate an error. A return value of -1 indicates that ScanFraction reached the end of the string and did not find a fraction. A return value of -2 indicates that ScanFraction encountered an illegal character for fractions. In both cases, the fraction that fptr points to should remain unchanged. For example, after the function call

     new_index = ScanFraction("abc     ", 3, &frac) ;
new_index will have the value -1; and after the function call
     new_index = ScanFraction("abc 23/45 xyz", 0, &frac) ;
new_index will have the value -2.

Implementation Notes

You will once again use the functions from Project 3 to handle the actual operations on the fractions. You should leave those functions in a separate file and include proj3.h at the top of your file. For this project, a properly formatted fraction should start with a sequence of digits followed by a '/' character and another sequence of digits. White space may be at the beginning of the fraction, between the numerator and the '/', and between the '/' and the denominator. White space may not appear between the digits. Thus, the following strings are examples of properly formatted fractions:
     "   24/ 135"   "24/135"   "24  / 135"   " 24  /135"
and the following strings are not properly formatted:
     " 2  4/ 13 5"   "24/  1 35"   "2 4  /135"
If you want to, you may implement an extra feature that allows the numerator to be preceded by a negative sign. No other extra features should be implemented.

What to turn in

In the same file that has your ScanFraction program, write a main function which calls the ScanFraction and tests all of its features. Your grade will depend upon choosing good test cases that demonstrate all the features of the function. Your main function from Project 4 is an acceptable choice (you will need to have all the other functions from Project 4 in the same file, too). When you are certain that your program works correctly, use the script command to record some sample runs which prove that you have implemented the ScanFraction function correctly. Again, your grade will depend upon choosing good test cases.

If your ScanFraction function does not work completely correctly, you should nevertheless include the sample runs which show what went wrong. You will receive more points for showing that you know a problem exists because a bug that you are not aware of is twice as bad as a bug that you know exists.

Last Modified: Tue Nov 21 09:16:07 EST 1995

Richard Chang,