Problem Statement
You are to build the unit conversion code that will be put into an electronic voice-enabled unit-conversion device. Your conversion routine will be called convertQuantity and will take a String[] representing a list of conversions (conversionList), each of which conforms to the formatting for a <conversion>, given below. It will also take a String, quantityToConvert, which is formatted as an <amount>, and a String, targetMeasurement, which is a <unit>. You are to perform the given conversion, from the <unit> in quantityToConvert to the targetMeasurement and return the proper measurement amount formatted similarly to an <amount>. The key difference between the formatting of the returned String and an <amount> is that the returned value may require numbers with more than 1 digit, and so, in the returned String, <integer> can represent any positive integer. (see examples)
<conversion> ::= <amount>=<amount> <amount> ::= <mixed number><sp><unit> <mixed number> ::= <integer> | <integer><sp><integer>'/'<integer> | <integer>'/'<integer> <unit> ::= A measurement unit (1 to 20 lowercase or uppercase letters, inclusive) <integer> ::= '1' to '9', inclusive <sp> ::= ' '
In addition to the above formatting rules, the mixed numbers in both the input and the returned String must be in one of 3 forms (quotes for clarity only):
- "a b/c" where a,b,c are integers greater than 0, and b is less than c and b/c is in reduced form (they have no common divisor greater than 1). This form represents the sum of the integer a with the fraction b/c
- "a" where a is an integer greater than 0. This form simply represents the integer a
- "b/c" where b,c are integers greater than 0 and b is less than c and b/c is in reduced form. This form simply represents the fraction b/c
For example(quotes for clarity): "2 3/4", "5", and "7/9" are valid whereas "9/7", "2 9/7", "3/6", "4 2/4", "0/8","0", and "3 0/4" are invalid.
The conversion list will follow these rules:
- If there are two paths to a given conversion, they will both return the same quantity. In other words, the conversionList will always be consistent across all quantities.
- The conversion will always be possible using the rules given.
Definition
- Class:
- UnitConverter
- Method:
- convertQuantity
- Parameters:
- String[], String, String
- Returns:
- String
- Method signature:
- String convertQuantity(String[] conversionList, String quantityToConvert, String targetMeasurement)
- (be sure your method is public)
Notes
- The 64 bit datatype for C++ is long long.
- Conversions are identical to their reverse, so "1 gallon=4 quarts" means the same thing as "4 quarts=1 gallon".
Constraints
- conversionList will contain between 1 and 5 elements, and will be consistent among its own conversions.
- You will always be able to make the conversion using the list given.
- Each element of conversionList will be formatted as a <conversion>, as described above.
- quantityToConvert will always be of the form of an <amount> as described above.
- All mixed numbers will be in the form of a mixed number as described above.
- conversionList will be consistent, and there will be a way to make the requested conversion.
- targetMeasurement will be a <unit> as described above.
- The <unit> in quantityToConvert will not be the same as targetMeasurement.
Examples
{"6 gill=1 1/2 cup", "2 cup=1 teacup"}"1 teacup"
"gill"
Returns: "8 gill"
Gramatically this should be 8 gills and not 8 gill, but we are not taking plurality into account.{"2 peck=1/2 bushel", "1/2 peck=4 dryquarts", "1/2 dryquarts=1 drypint", "1/2 drypint=1 UScups", "4 UScups=5 Britishcups"}"9 bushel"
"Britishcups"
Returns: "1440 Britishcups"