我想將我的程式從 C 翻譯(或手動編譯)到 HLA。程式讀取輸入的數字。然后減去三和十或僅十,確定該值是否以零或三結尾。連續三個這樣的數字贏得比賽!不以這些數字結尾的值將輸掉比賽。
我不知道如何使用 HLA 中由 AND 運算子連接的兩個條件進行 while 回圈。
while ((iend != 1) && (iscore < 3))
這是我用 C 撰寫的完整代碼,我想將其轉換為 HLA:
#include <iostream>
using namespace std;
int main() {
int inum;
int iend = 0;
int iscore = 0;
int icheckthree; //To check if it ends in 3
int icheckzero; //To check if it ends in zero
while ((iend != 1) && (iscore < 3)) {
cout << "Gimme a number: ";
cin >> inum;
//Case 1: ends in three
icheckthree = inum - 3;
while (icheckthree > 0) {
icheckthree = icheckthree - 10;
if (icheckthree == 0) {
cout << "It ends in three!" << endl;
iscore ;
}
}
icheckzero = inum;
while (icheckzero > 0) {
icheckzero = icheckzero - 10;
}
//Case 2: ends in zero
if (icheckzero == 0) {
cout << "It ends in zero!" << endl;
iscore ;
}
//Case 3: Loose the game
else {
if (icheckzero != 0) {
if(icheckthree != 0) {
iend = 1;
}
}
}
if (iend == 1) {
cout << "\n";
cout << "Sorry Charlie! You lose the game!" << endl;
}
else if (iscore == 3) {
cout << "\n";
cout << "You Win The Game!" << endl;
} else {
cout << "Keep going..." << endl;
cout << "\n";
}
}
}
uj5u.com熱心網友回復:
使用邏輯轉換。
例如,宣告:
if ( <c1> && <c2> ) { <do-this-when-both-true> }
可以翻譯成:
if ( <c1> ) {
if ( <c2> ) {
<do-this-when-both-true>
}
}
這兩個結構是等價的,但后者不使用連詞。
可以對 if-goto-label 進行 while 回圈,如下所示:
while ( <condition> ) {
<loop-body>
}
Loop1:
if ( <condition> is false ) goto EndLoop1;
<loop-body>
goto Loop1;
EndLoop1:
接下來,單獨的 if 陳述句涉及連詞的倒置,&&,如下所示:
if ( <c1> && <c2> is false ) goto label;
又名
if ( ! ( <c1> && <c2> ) ) goto label;
簡化如下:
if ( ! <c1> || ! <c2> ) goto label;
這是根據德摩根的邏輯定律,將否定與合取和析取聯系起來。
最后,上面的析取可以很容易地簡化(類似于上面的連詞簡化)如下:
if ( ! <c1> ) goto label;
if ( ! <c2> ) goto label;
如果 while 回圈的條件是連詞 (&&),則可以將上述轉換放在一起以創建條件退出析取序列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/337097.html
上一篇:匯編程式(在MARIE中),如何填寫第一遍和第二遍?
下一篇:如何建立緩沖區溢位有效負載
