当前正在为import os.path
def root_relative_path(root : str, path : str) -> str:
return (os.path.join(root,
os.path.join(os.sep, os.path.normpath(path)).split(os.sep)[1:])))
编写Java程序。我试图从Tomasulo's Algorithm
数组中提取一个数字,将其设置为单词。
这是从一个看起来像这样的文本文件中读取的:
multidimensional
前两行会绕开其他内容,但之后我希望将每行的第一个数字设置为诸如10
7
0 2 2 1
0 3 3 2
0 4 2 3
2 5 2 4
2 6 3 5
3 7 3 4
至'0'
,{{1 }}至'add'
,'1'
至'sub'
和'2'
至'mult'
我该怎么办?
'3'
答案 0 :(得分:0)
我可能会使用switch语句来处理此问题,因为您最多只能处理10个可能的结果(0-9)。在每种情况下,我都将转换后的值存储在单独的String数组中,该数组可以通过文件中的行数进行索引。以下程序应注意所有事项,包括尾随数字的存储:
更新:该程序实际上与其他提交程序非常相似,但应该可以回答整个问题
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class TomasuloAlgorithimDriver {
public static void main(String[] args) throws FileNotFoundException {
File file = new File("C:/PathToFile/File.txt");
Scanner scanInt = new Scanner(file);
String line = "";
int cycle = 0;
//Initializations are specific to your example
String[] convertedFirstNums = new String[6];
int[][] nums = new int[8][3];
while (scanInt.hasNextLine()) {
line = scanInt.nextLine();
cycle++;
if(cycle > 2) {//Only executes after first two lines are parsed
//Grab first integer for conversion
String firstNum = "" + line.toCharArray()[0];
convertedFirstNums[cycle-3] = switchInteger(Integer.parseInt(firstNum));
//Parse the rest of the line, ignoring the first integer
int ind = 0;
for(char c : line.substring(1).toCharArray()) {
switch(c) {
case ' ': //Skip all spaces in the line
break;
default: //Take following integers and saves them to num array
String num = "" + c;
nums[cycle-1][ind] = Integer.parseInt(num);
ind++;
break;
}
}
} else {
//Pick up first two lines
nums[cycle-1][0] = Integer.parseInt(line);
}
}
scanInt.close();
for(int i=0; i < nums.length; i++) {
if(i < 2) //Print first two numbers from first two lines
System.out.printf("%d\n", nums[i][0]);
else {
System.out.print(convertedFirstNums[i-2] + " : ");
for(int j=0; j < nums[i].length; j++) {
System.out.printf("%d ", nums[i][j]);
}
System.out.println();
}
}
}
public static String switchInteger(int i) {
switch(i) {
case 0: return "add ";
case 1: return "sub ";
case 2: return "mult";
case 3: return "div ";
}
return "err ";
}
}
答案 1 :(得分:0)
您可以使用字符串数组来代替整数数组,该字符串数组可以根据您从文件中读取的整数值来设置值。
下面是满足您要求的代码:
public class ArrayTest {
public static void main(String[] args) throws FileNotFoundException {
String stringArray[][] = new String[10][4];
Scanner scanInt = new Scanner(new File("File Path"));
int n = 0; // for the number of instructions
int displayCycle = 0; // cycle to be displayed
if (scanInt.hasNext()) {
n = scanInt.nextInt(); // number of instructions taken from file
displayCycle = scanInt.nextInt(); // cycle number taken from file
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < 4; j++) {
if (scanInt.hasNext()) {
stringArray[i][j] = getStringValue(scanInt.nextInt());
}
}
}
// Printing the data
for (String[] strArray : stringArray) {
for (String s : strArray) {
System.out.print(s+" ");
}
System.out.println();
}
}
static String getStringValue(int number) {
String value = null;
switch (number) {
case 0:
value = "add";
break;
case 1:
value = "sub";
break;
case 2:
value = "mult";
break;
case 3:
value = "div";
break;
default:
value = "";
break;
}
return value;
}
}