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
<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
- "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
, as described above. - quantityToConvert will always be of the form of an
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
as described above. - The
in quantityToConvert will not be the same as targetMeasurement.
Examples
{"2 wipf=3 cups"}
"1 1/2 wipf"
"cups"
Returns: "2 1/4 cups"
This is the first example from the introduction.
{"1 wipf=2 lwarnok","2 wipf=3 cups"}
"9 lwarnok"
"cups"
Returns: "6 3/4 cups"
Second example from the notes.
{"1 wipf=2 lwarnok","2 wipf=3 cups"}
"9 lwarnok"
"cups"
Returns: "6 3/4 cups"
Second example from the notes.
{"9 4/7 a=9 8/9 b","9 4/7 b=9 8/9 c","9 4/7 c=9 8/9 d","9 4/7 d=9 8/9 e","9 4/7 e=9 8/9 f"}
"9 3/5 f"
"a"
Returns: "8 72678310901944/469256435796715 a"
Testing to see if longs have been used, and used properly.
{"9 4/7 a=9 8/9 b","9 4/7 b=9 8/9 c","9 4/7 c=9 8/9 d","9 4/7 d=9 8/9 e","9 4/7 e=9 8/9 f"}
"9 3/5 f"
"a"
Returns: "8 72678310901944/469256435796715 a"
Testing to see if longs have been used, and used properly.
{"9 4/7 a=9 8/9 b","9 4/7 b=9 8/9 c","9 4/7 c=9 8/9 d","9 4/7 d=9 8/9 e","9 4/7 e=9 8/9 f"}
"9 3/5 f"
"a"
Returns: "8 72678310901944/469256435796715 a"
Testing to see if longs have been used, and used properly.
{"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.
{"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.
{"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.
{"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.
{"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.
{"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"
{"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"
{"1/3 jour=8 heures","2 jour=2/7 semaine","8 2/3 semaine=1/6 ans"}
"1 ans"
"jour"
Returns: "364 jour"
{"1/3 jour=8 heures","2 jour=2/7 semaine","8 2/3 semaine=1/6 ans"}
"1 ans"
"jour"
Returns: "364 jour"
{"1/3 jour=8 heures","2 jour=2/7 semaine","8 2/3 semaine=1/6 ans"}
"1 ans"
"jour"
Returns: "364 jour"
{"1/7 stunden=8 4/7 minute","8 4/7 sekund=1/7 minute","1/3 tage=8 stunden","7 tage=1 woche","8 2/3 woche=1 jahre"}
"1 sekund"
"jahre"
Returns: "1/5241600 jahre"
{"1 a=2 b","2 2/3 b=1 1/3 A"}
"1 a"
"A"
Returns: "1 A"
{"1/8 a=8 aa","1/8 aa=8 aaa","1/8 aaaaa=8 aaaaaa","1/8 aaaa=8 aaaaa","1/8 aaa=8 aaaa"}
"8 a"
"aaaaaa"
Returns: "8589934592 aaaaaa"
{"1/9 a=9 8/9 b" ,"1/9 b=9 8/9 c" ,"1/9 c=9 8/9 d" ,"1/9 d=9 8/9 e" ,"1/9 e=9 8/9 f"}
"9 8/9 a"
"f"
Returns: "55220143440 1/9 f"
{"1/9 a=9 8/9 b" ,"1/9 b=9 8/9 c" ,"1/9 c=9 8/9 d" ,"1/9 d=9 8/9 e" ,"1/9 e=9 8/9 f"}
"1/9 f"
"a"
Returns: "1/50256535041 a"
{"1/9 a=9 8/9 b" ,"1/9 b=9 8/9 c" ,"1/9 c=9 8/9 d" ,"1/9 e=9 8/9 d" ,"1/9 f=9 8/9 e"}
"1/9 a"
"f"
Returns: "9 8/9 f"
{ "1 a=9 8/9 b", "1 b=9 8/9 c", "1 c=9 8/9 d", "1 d=9 8/9 e", "1 e=9 8/9 f" }
"9 a"
"f"
Returns: "851098 5471/6561 f"
{ "3 1/7 bushel=1 1/4 cup", "1 1/7 cup=3 1/3 tea", "2 1/8 cup=9 1/5 pico", "9 1/8 pico=3 1/2 coolio" }
"2 1/7 bushel"
"coolio"
Returns: "1 5669/13651 coolio"
{ "9 a=1/9 b", "9 b=1/9 c", "9 c=1/9 d", "9 d=1/9 e", "9 e=1/9 f" }
"1/9 a"
"f"
Returns: "1/31381059609 f"
{ "1/9 a=9 b", "1/9 b=9 c", "1/9 c=9 d", "1/9 d=9 e", "1/9 e=9 f" }
"9 a"
"f"
Returns: "31381059609 f"
{ "9 8/9 b=1/9 a", "9 8/9 c=1/9 b", "9 8/9 d=1/9 c", "9 8/9 e=1/9 d", "9 8/9 f=1/9 e" }
"7/9 a"
"f"
Returns: "4343157349 2/9 f"
{ "1 a=2 b", "1 b=2 c", "1 b=1 1/2 d", "1 d=3 e" }
"1/2 a"
"e"
Returns: "4 1/2 e"
{ "1 a=1 1/4 b" }
"1 a"
"b"
Returns: "1 1/4 b"
{ "1/9 A=1 B", "3/4 B=1 C", "2/5 C=1 D" }
"1 D"
"A"
Returns: "1/30 A"
{ "1 a=9 1/9 b", "1 b=9 1/9 c", "1 c=9 1/9 d", "1 d=9 1/9 e", "1 e=9 1/9 f" }
"9 1/9 a"
"f"
Returns: "572042 98902/531441 f"
{ "1/9 A=9 8/9 B", "1/9 B=9 8/9 C", "1/9 C=9 8/9 D", "1/9 D=9 8/9 E", "1/9 E=9 8/9 F" }
"9 8/9 A"
"F"
Returns: "55220143440 1/9 F"