C 编程中的布尔值
Boolean in C Programming
所以,不幸的是,我在尝试创建的程序中遇到了另一个问题。首先,我是 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 编程不支持布尔值。
我正在使用它,因此用户输入他正在搜索的单词(例如:男孩),用户还输入长度 ( 3 ),然后用户将输入第一个和第二个的坐标单词的最后一个字母。当用户输入以下内容时,我打算从上面的代码中获取坐标,然后将它们与用户输入的内容进行比较。如果它们不匹配,则用户猜错了,如果它们匹配,则用户猜对了。
我也尝试过 stdbool.h
库,但是它没有用,因为找不到该库。
除了stdbool.h
还有其他方法吗?我知道你使用 true = 1 , false = 0 但是我不知道如何在下面的代码中解释它。
提前致谢。
只需使用 int
即可。否则,创建您自己的布尔类型:
enum { false, true };
typedef int bool;
如果要存储很多布尔值,请考虑
typedef char bool;
相反。请注意,两者都不是“真正的”布尔类型,因为它可以采用除 0 和 1 之外的其他值。使用 C 约定,即 0 表示错误,而所有其他值表示正确。
C99 有 <stdbool.h>
header.
您可以使用 int
,其中 1
等于 true
或 0
等于 false
但是我在程序顶部的代码中使用了这个,两个常量使用单词 TRUE
和 FALSE
就好像它们是布尔值一样:
#define FALSE 0
#define TRUE 1
你这么说
I've also tried the stdbool.h library , however it didn't work because the library wasn't found.
那么,我倾向于建议您找到并使用符合标准的 C99 或 C2011 编译器。至少,前者不应该太难下手。两者都肯定会提供 header,使用它可能是您最方便的前进方式。
由于您的代码仍然包含一些 C++ 主义(例如 cout
、using
语句和 C++ 风格的 #include
),我倾向于相信您正在使用 C++ 编译器进行编译。这是找不到 stdbool.h
header 的一个可能原因。如果要转换为 C,请务必使用 C 编译器构建代码。
所以,不幸的是,我在尝试创建的程序中遇到了另一个问题。首先,我是 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 编程不支持布尔值。
我正在使用它,因此用户输入他正在搜索的单词(例如:男孩),用户还输入长度 ( 3 ),然后用户将输入第一个和第二个的坐标单词的最后一个字母。当用户输入以下内容时,我打算从上面的代码中获取坐标,然后将它们与用户输入的内容进行比较。如果它们不匹配,则用户猜错了,如果它们匹配,则用户猜对了。
我也尝试过 stdbool.h
库,但是它没有用,因为找不到该库。
除了stdbool.h
还有其他方法吗?我知道你使用 true = 1 , false = 0 但是我不知道如何在下面的代码中解释它。
提前致谢。
只需使用 int
即可。否则,创建您自己的布尔类型:
enum { false, true };
typedef int bool;
如果要存储很多布尔值,请考虑
typedef char bool;
相反。请注意,两者都不是“真正的”布尔类型,因为它可以采用除 0 和 1 之外的其他值。使用 C 约定,即 0 表示错误,而所有其他值表示正确。
C99 有 <stdbool.h>
header.
您可以使用 int
,其中 1
等于 true
或 0
等于 false
但是我在程序顶部的代码中使用了这个,两个常量使用单词 TRUE
和 FALSE
就好像它们是布尔值一样:
#define FALSE 0
#define TRUE 1
你这么说
I've also tried the stdbool.h library , however it didn't work because the library wasn't found.
那么,我倾向于建议您找到并使用符合标准的 C99 或 C2011 编译器。至少,前者不应该太难下手。两者都肯定会提供 header,使用它可能是您最方便的前进方式。
由于您的代码仍然包含一些 C++ 主义(例如 cout
、using
语句和 C++ 风格的 #include
),我倾向于相信您正在使用 C++ 编译器进行编译。这是找不到 stdbool.h
header 的一个可能原因。如果要转换为 C,请务必使用 C 编译器构建代码。