【发布时间】:2016-04-13 12:09:30
【问题描述】:
所以,不幸的是,我在尝试创建的程序中遇到了另一个问题。首先,我对 C 编程完全陌生,我正在尝试创建一个 Word Search 。
我有这段 C++ 代码,我正在尝试将其转换为 C:
#include <iostream>
using namespace std;
int main()
{
char puzzle[5][5] = {
'A', 'J', 'I', 'P', 'N',
'Z', 'F', 'Q', 'S', 'N',
'O', 'W', 'N', 'C', 'E',
'G', 'L', 'S', 'X', 'W',
'N', 'G', 'F', 'H', 'V',
};
char word[5] = "SNOW"; // The word we're searching for
int i;
int len; // The length of the word
bool found; // Flag set to true if the word was found
int startCol, startRow;
int endCol, endRow;
int row, col;
found = false;
i = 0;
len = 4;
// Loop through each character in the puzzle
for(row = 0; row < 5; row ++) {
for(col = 0; col < 5; col ++) {
// Does the character match the ith character of the word
// we're looking for?
if(puzzle[row][col] == word[i]) {
if(i == 0) { // Is it the first character of the word?
startCol = col;
startRow = row;
} else if(i == len - 1) { // Is it the last character of the
// word?
endCol = col;
endRow = row;
found = true;
}
i ++;
} else
i = 0;
}
if(found) {
// We found the word
break;
}
}
if(found) {
cout << "The word " << word << " starts at (" << startCol << ", "
<< startRow << ") and ends at (" << endCol << ", " << endRow
<< ")" << endl;
}
return 0;
}
但是,我遇到了一个问题,因为我刚刚注意到 C 编程不支持布尔值。
我正在使用它,所以用户输入他正在搜索的单词(例如:boy),用户还输入长度(3),然后用户将输入第一个和最后一个字母的坐标这个词的。当用户输入以下内容时,我打算从上面的代码中获取坐标,然后将它们与用户输入的内容进行比较。如果它们不匹配,则用户猜错了,如果它们匹配,则用户猜对了。
我也尝试了 stdbool.h 库,但是由于找不到库,所以它不起作用。
除了stdbool.h 还有其他方法吗?我知道你使用 true = 1 , false = 0 但是我不知道如何在下面的代码中解释它。
提前致谢。
【问题讨论】:
-
您可以使用值为
true和false的枚举 -
有什么理由要将
C++代码转换为C? -
使用 #define 创建 TRUE 和 FALSE。这样,您的代码中的用法将是正确的,除了确保您使用定义的值(例如,将 true 更改为 TRUE 并将 false 更改为 FALSE)之外,您无需检查和编辑代码
-
您需要所有这些行来描述问题吗?问题最多可以简化为当前大小的 20%。