Java 到具有两个数组的 Mips
Java to Mips with two arrays
我是 MIPS 的新手,但我正在尝试转换我的 java 程序,该程序以罗马数字的形式从用户那里获取输入并将其转换为整数,然后将其打印出来。这是我的 Java 程序:
private static int decodeSingle(char letter) {
switch(letter) {
case 'M': return 1000;
case 'D': return 500;
case 'C': return 100;
case 'L': return 50;
case 'X': return 10;
case 'V': return 5;
case 'I': return 1;
default: return 0;
}
}
public static int decode(String roman) {
int result = 0;
String uRoman = roman.toUpperCase(); //case-insensitive
for(int i = 0; i < uRoman.length() - 1; i++) {//loop over all but the last character
//if this character has a lower value than the next character
if (decodeSingle(uRoman.charAt(i)) < decodeSingle(uRoman.charAt(i + 1))) {
//subtract it
result -= decodeSingle(uRoman.charAt(i));
} else {
//add it
result += decodeSingle(uRoman.charAt(i));
}
}
//decode the last character, which is always added
result += decodeSingle(uRoman.charAt(uRoman.length() - 1));
return result;
}
public static void main(String[] args) {
System.out.println(decode("MCMXC")); //1990
System.out.println(decode("MMVIII")); //2008
System.out.println(decode("MDCLXVI")); //1666
}
我想用以下两个数组来设置程序。我的想法是我可以将用户输入的任何内容与 all_numerals
进行比较(即用户输入是 V
与 V
相比,这将在索引中为其赋值。一旦我们有了我们可以将索引值与 all_values
中的索引值进行比较。我显然也需要一个循环来遍历用户输入。
# put this somewhere in the data section
all_numerals: .asciiz "IVXLCDMivxlcdm"
all_values: .byte 1, 5, 10, 50, 100, 500, 1000, 1, 5, 10, 50, 100, 500, 1000
我的问题是:您是否必须将 all_numerals
和 all_values
的值插入到寄存器中,或者您可以直接比较数组吗?完全不熟悉 MIPS 这是最有效和最合乎逻辑的方式吗?
您可以用嵌套的 if-checks 代替 switch 并摆脱子函数调用。这样你就不需要在数据块中保留罗马字母和它们的对应物之间的映射。它们可以被硬编码为常量。此外,这种优化使我的速度提高了两倍:
public static int decode(String roman) {
int result = 0;
String uRoman = roman; //case-insensitive
int prevPart = -1;
for(int i = 0; i < uRoman.length(); i++) {//loop over all but the last character
int curPart = 0;
int letter = (int)uRoman.charAt(i);
if (letter >= 'a' && letter <= 'z')
letter -= (int)'a' - (int)'A'; // toUpper emulation
if (letter <= (int)'I') {
if (letter == (int)'C') {
curPart = 100;
} else if (letter == (int)'D') {
curPart = 500;
} else if (letter == (int)'I') {
curPart = 1;
}
} else if (letter <= (int)'M') {
if (letter == (int)'L') {
curPart = 50;
} else if (letter == (int)'M') {
curPart = 1000;
}
} else if (letter == (int)'V') {
curPart = 5;
} else if (letter == (int)'X') {
curPart = 10;
}
if (prevPart > 0) {
//if this character has a lower value than the next character
if (prevPart < curPart) {
//subtract it
result -= prevPart;
} else {
//add it
result += prevPart;
}
}
prevPart = curPart;
}
//decode the last character, which is always added
result += prevPart;
return result;
}
我是 MIPS 的新手,但我正在尝试转换我的 java 程序,该程序以罗马数字的形式从用户那里获取输入并将其转换为整数,然后将其打印出来。这是我的 Java 程序:
private static int decodeSingle(char letter) {
switch(letter) {
case 'M': return 1000;
case 'D': return 500;
case 'C': return 100;
case 'L': return 50;
case 'X': return 10;
case 'V': return 5;
case 'I': return 1;
default: return 0;
}
}
public static int decode(String roman) {
int result = 0;
String uRoman = roman.toUpperCase(); //case-insensitive
for(int i = 0; i < uRoman.length() - 1; i++) {//loop over all but the last character
//if this character has a lower value than the next character
if (decodeSingle(uRoman.charAt(i)) < decodeSingle(uRoman.charAt(i + 1))) {
//subtract it
result -= decodeSingle(uRoman.charAt(i));
} else {
//add it
result += decodeSingle(uRoman.charAt(i));
}
}
//decode the last character, which is always added
result += decodeSingle(uRoman.charAt(uRoman.length() - 1));
return result;
}
public static void main(String[] args) {
System.out.println(decode("MCMXC")); //1990
System.out.println(decode("MMVIII")); //2008
System.out.println(decode("MDCLXVI")); //1666
}
我想用以下两个数组来设置程序。我的想法是我可以将用户输入的任何内容与 all_numerals
进行比较(即用户输入是 V
与 V
相比,这将在索引中为其赋值。一旦我们有了我们可以将索引值与 all_values
中的索引值进行比较。我显然也需要一个循环来遍历用户输入。
# put this somewhere in the data section
all_numerals: .asciiz "IVXLCDMivxlcdm"
all_values: .byte 1, 5, 10, 50, 100, 500, 1000, 1, 5, 10, 50, 100, 500, 1000
我的问题是:您是否必须将 all_numerals
和 all_values
的值插入到寄存器中,或者您可以直接比较数组吗?完全不熟悉 MIPS 这是最有效和最合乎逻辑的方式吗?
您可以用嵌套的 if-checks 代替 switch 并摆脱子函数调用。这样你就不需要在数据块中保留罗马字母和它们的对应物之间的映射。它们可以被硬编码为常量。此外,这种优化使我的速度提高了两倍:
public static int decode(String roman) {
int result = 0;
String uRoman = roman; //case-insensitive
int prevPart = -1;
for(int i = 0; i < uRoman.length(); i++) {//loop over all but the last character
int curPart = 0;
int letter = (int)uRoman.charAt(i);
if (letter >= 'a' && letter <= 'z')
letter -= (int)'a' - (int)'A'; // toUpper emulation
if (letter <= (int)'I') {
if (letter == (int)'C') {
curPart = 100;
} else if (letter == (int)'D') {
curPart = 500;
} else if (letter == (int)'I') {
curPart = 1;
}
} else if (letter <= (int)'M') {
if (letter == (int)'L') {
curPart = 50;
} else if (letter == (int)'M') {
curPart = 1000;
}
} else if (letter == (int)'V') {
curPart = 5;
} else if (letter == (int)'X') {
curPart = 10;
}
if (prevPart > 0) {
//if this character has a lower value than the next character
if (prevPart < curPart) {
//subtract it
result -= prevPart;
} else {
//add it
result += prevPart;
}
}
prevPart = curPart;
}
//decode the last character, which is always added
result += prevPart;
return result;
}