You decided to give up on earth after the latest financial collapse left 99.99% of the earth’s population with 0.01% of the wealth. Luckily, with the scant sum of money that is left in your account, you are able to afford to rent a spaceship, leave earth, and fly all over the galaxy to sell common metals and dirt (which apparently is worth a lot).
Buying and selling over the galaxy requires you to convert numbers and units, and you decided to write a program to help you.
The numbers used for intergalactic transactions follows similar convention to the roman numerals and you have painstakingly collected the appropriate translation between them.
Roman numerals are based on seven symbols:
Symbol | Value |
---|---|
I | 1 |
V | 5 |
X | 10 |
L | 50 |
C | 100 |
D | 500 |
M | 1000 |
Numbers are formed by combining symbols together and adding the values. For example, MMVI is 1000 + 1000 + 5 + 1 = 2006. Generally, symbols are placed in order of value, starting with the largest values. When smaller values precede larger values, the smaller values are subtracted from the larger values, and the result is added to the total. For example MCMXLIV = 1000 + (1000 − 100) + (50 − 10) + (5 − 1) = 1944.
Input to your program consists of lines of text detailing your notes on the conversion between intergalactic units and roman numerals.
You are expected to handle invalid queries appropriately.
glob is I
prok is V
pish is X
tegj is L
glob glob Silver is 34 Credits
glob prok Gold is 57800 Credits
pish pish Iron is 3910 Credits
how much is pish tegj glob glob ?
how many Credits is glob prok Silver ?
how many Credits is glob prok Gold ?
how many Credits is glob prok Iron ?
how much wood could a woodchuck chuck if a woodchuck could chuck wood ?
pish tegj glob glob is 42
glob prok Silver is 68 Credits
glob prok Gold is 57800 Credits
glob prok Iron is 782 Credits
I have no idea what you are talking about
package com.test.lihao;
import java.util.HashMap;
import java.util.Map;
public class RomaUtils {
private static Map<Character, Integer> bases;
private static final String unKnownMsg = "I have no idea what you are talking about";
public RomaUtils() {
bases = new HashMap<>();
bases.put('I', 1);
bases.put('V', 5);
bases.put('X', 10);
bases.put('L', 50);
bases.put('C', 100);
bases.put('D', 500);
bases.put('M', 1000);
}
// 输入罗马数字字符串,输出转换后的阿拉伯字符串
public int toArabicNumber(String romaNumber) {
int arabicNumber = 0;
int size = romaNumber.length();
int i = 0;
try {
for (i = 0; i < size - 1; i++) {
int left = bases.get(romaNumber.charAt(i));
int right = bases.get(romaNumber.charAt(i + 1));
if (left < right) {
arabicNumber += right - left;
i++;
} else {
arabicNumber += left;
}
}
if (i < size) {
arabicNumber += bases.get(romaNumber.charAt(size - 1));
}
} catch (Exception e) {
System.out.println("输入数字非罗马数字,错误信息为:" + e.getMessage());
}
return arabicNumber;
}
//语义转换
//存放自定义变量和罗马字符的转换,如 glob I
private static Map<String, String> strWithRomaChar = new HashMap<>();
//存放自定义物品的价格,如 Silver 23
private static Map<String, Double> thingsPrice = new HashMap<>();
public String getArabicResultByInputRomaLanguage(String inputRoma) {
String[] words = inputRoma.split(" ");
int length = words.length;
String lastWord = words[length - 1];
//1、按照末尾单词是否在bases中,是第一类定义单词输入
//2、判断是否以小写s结束,判断是否是作为第二类金币银币类型输入
// 3、否则判断末尾是否是?号为第三类输入
// 4、进行按照空格划分之后判断第二个单词,按照many和mach进行分别返回处理
if (lastWord.length() == 1 && null != bases.get(lastWord.charAt(0))) {
//第一类
//glob is I
strWithRomaChar.put(words[0], lastWord);
} else if (lastWord.endsWith("s")) {
//第二类 倒数第二个是总价
//glob glob Silver is 34 Credits
int amount = Integer.parseInt(words[length - 2]);
//倒数第四个是东西名称
String thingName = words[length - 4];
//第一个到倒数第五个为个数
String romaStr = "";
for (int i = 0; i < length - 4; i++) {
String tmp = strWithRomaChar.get(words[i]);
if (null == tmp || "".equals(tmp)) {
System.out.println(unKnownMsg);
return unKnownMsg;
}
romaStr += tmp;
}
int num = toArabicNumber(romaStr);
thingsPrice.put(thingName, amount * 1.0 / num);
} else if (lastWord.endsWith("?")) {
//第三类
//how many Credits is glob prok Silver ?
// how much is pish tegj glob glob ?
//判断how much和howmang
if ("many".equals(words[1])) {
//how many
//第五个开始到倒数第3个为个数
String romaStr = "";
for (int i = 4; i < length - 2; i++) {
String tmp = strWithRomaChar.get(words[i]);
if (null == tmp || "".equals(tmp)) {
System.out.println(unKnownMsg);
return unKnownMsg;
}
romaStr += tmp;
}
int num = toArabicNumber(romaStr);
//最后2个为名称
String name = words[length - 2];
StringBuffer result = new StringBuffer();
//glob prok Silver is 68 Credits ?
for (int i = 4; i < length - 2; i++) {
result.append(words[i]).append(" ");
}
result.append(name).append(" is ").append((int)(num * thingsPrice.get(name))).append(" Credits");
System.out.println(result);
} else if ("much".equals(words[1])) {
//how much
//how much is pish tegj glob glob ?
//第四个到倒数第二个
String romaStr = "";
for (int i = 3; i < length - 1; i++) {
String tmp = strWithRomaChar.get(words[i]);
if (null == tmp || "".equals(tmp)) {
System.out.println(unKnownMsg);
return unKnownMsg;
}
romaStr += tmp;
}
int resultNumber = toArabicNumber(romaStr);
StringBuffer result = new StringBuffer();
//pish tegj glob glob is 42
for (int i = 3; i < length - 1; i++) {
result.append(words[i] + " ");
}
result.append(" is " + resultNumber);
System.out.println(result);
}
}
return null;
}
}
package com.test.lihao;
public class Main {
public static void main(String[] args) {
RomaUtils romaUtils = new RomaUtils();
String[] inputs = {
"glob is I",
"prok is V",
"pish is X",
"tegj is L",
"glob glob Silver is 34 Credits",
"glob prok Gold is 57800 Credits",
"pish pish Iron is 3910 Credits",
"how much is pish tegj glob glob ?",
"how many Credits is glob prok Silver ?",
"how many Credits is glob prok Gold ?",
"how many Credits is glob prok Iron ?",
"how much wood could a woodchuck chuck if a woodchuck could chuck wood ?"};
for (int i = 0; i < inputs.length; i++) {
romaUtils.getArabicResultByInputRomaLanguage(inputs[i]);
}
}
}