我想将我的程序从c++转换(或手动编译)到HLA。程序读取输入的数字。然后减去3和10或只有10,确定该值是以零结尾还是以3结尾。连续三个这样的数字赢了比赛!一个不以这些数字结尾的值会输掉游戏。
我不知道如何在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";
}
}
}发布于 2021-10-24 20:10:22
使用逻辑转换。
例如,声明:
if ( <c1> && <c2> ) { <do-this-when-both-true> }可翻译为:
if ( <c1> ) {
if ( <c2> ) {
<do-this-when-both-true>
}
}这两个构造是等价的,但后者不使用连接。
while循环可以按以下方式转到if-goto标签:
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://stackoverflow.com/questions/69700258
复制相似问题