我已經在C++中完成了處理器仿真器分配,現在我正嘗試轉換Java中的整個代碼。這裏出現的問題是我們通過在C++中使用反向函數來改變進程。我知道在java中有一個反向函數,但我不知道如何在下面的例子中使用它。如何將C++中的反向函數代碼轉換爲Java
shuffle()
函數代碼正好在C++和Java下面!
這是C++代碼:
void processor::shuffle(){
int swapPriority;
int swapTime;
int swapProcessID;
string swapProcessName;
int end = used - 1;
int length = used - 1;
for (int counter = length - 1; counter>0; counter--){
for (int index = 0; index<end; index++){
if (priority[index]>priority[index + 1]){
//ist work
swapPriority = priority[index + 1];
swapTime = timeReq[index + 1];
swapProcessID = processId[index + 1];
swapProcessName = processName[index + 1];
//2nd
priority[index + 1] = priority[index];
timeReq[index + 1] = timeReq[index];
processId[index + 1] = processId[index];
processName[index + 1] = processName[index];
//3rd
priority[index] = swapPriority;
timeReq[index] = swapTime;
processId[index] = swapProcessID;
processName[index] = swapProcessName;
}
}end--;
}
**The problem is here !!!!**
//How to convert this below portion into Java code??
if (priority[0]<priority[1]){
reverse(priority, priority + length + 1);
reverse(timeReq, timeReq + length + 1);
reverse(processId, processId + length + 1);
reverse(processName, processName + length + 1);
}
}
這是Java代碼:
public void shuffle() {
int swapPriority;
int swapTime;
int swapProcessID;
String swapProcessName;
int end = used - 1;
int length = used - 1;
for (int counter = length - 1; counter > 0; counter--) {
for (int index = 0; index < end; index++) {
if (priority[index] > priority[index + 1]) {
//1st work
swapPriority = priority[index + 1];
swapTime = timeReq[index + 1];
swapProcessID = processId[index + 1];
swapProcessName = processName[index + 1];
//2nd
priority[index + 1] = priority[index];
timeReq[index + 1] = timeReq[index];
processId[index + 1] = processId[index];
processName[index + 1] = processName[index];
//3rd
priority[index] = swapPriority;
timeReq[index] = swapTime;
processId[index] = swapProcessID;
processName[index] = swapProcessName;
}
}
end--;
}
if (priority[0] < priority[1]) {
}
/* reverse(priority, priority + length + 1);
reverse(timeReq, timeReq + length + 1);
reverse(processId, processId + length + 1);
reverse(processName, processName + length + 1);
*/
}
在Java中發佈您迄今爲止所做的工作。 – ChristofferPass
我很難跟隨你想要做的事情。我知道你正在將一些C++代碼移植到Java中,但是這並不完全清楚你停滯不前。 –
@AhmedAdnan:這裏我們用代碼刪除註釋,並將其置於編輯的問題。 – Qiu