C编程中的布尔值 [英] Boolean in C Programming

查看:150
本文介绍了C编程中的布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,很不幸,我尝试创建的程序遇到了另一个问题.首先,我是C编程的新手,我正在尝试创建一个单词搜索 .

So , unfortunately I encountered another problem with a program I'm trying to create. First of all I'm totally new to C Programming and I'm trying to create a Word Search .

我有这段使用C ++编写的代码,我正在尝试将其转换为C:

I have this piece of code which is in C++ and I'm trying to turn it into 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编程不支持布尔值.

However , I've encountered a problem as I just noticed that C Programming doesn't support Booleans.

我正在使用它,因此用户输入了他要搜索的单词(例如:boy),用户还输入了长度(3),然后用户将输入第一个字母和最后一个字母的坐标这个词.当用户输入以下内容时,我打算从上面的代码中获取坐标,然后将其与用户输入的内容进行比较.如果不匹配,则用户猜错了,如果匹配,则用户猜对了.

I'm using it so the user enters the word he is searching for ( for example: boy) , the user also enters the length ( 3 ) and then the user will enter the co-ordinates of the first and last letters of the word. When the user enters the following I'm planning to get the co-ordinates from the code above and than compare them with what the user entered. If they doesn't match the user guessed incorrectly , and if they match the user guessed it correctly.

我也尝试过stdbool.h库,但是由于找不到该库而无法使用.

I've also tried the stdbool.h library , however it didn't work because the library wasn't found.

还有其他方法可以代替stdbool.h吗?我知道您使用true = 1,false = 0,但是我不知道如何在下面的代码中解释它.

Is there any other way instead of stdbool.h ? I know you use true = 1 , false = 0 however I don't know exactly how to interpret it in the following code.

谢谢.

推荐答案

你这么说

我也尝试过stdbool.h库,但是由于找不到该库而无法正常工作.

I've also tried the stdbool.h library , however it didn't work because the library wasn't found.

那么,我倾向于建议您找到并使用符合要求的C99或C2011编译器.至少,前者应该不难动手.肯定会提供标题,并且使用它可能是您最方便的前进方式.

I'm inclined to suggest, then, that you find and use a conforming C99 or C2011 compiler. The former, at least, should not be too hard to put your hands on. Either will assuredly provide the header, and using it is probably your most convenient way forward.

由于您的代码仍然包含某些C ++形式(例如coutusing语句和C ++样式#include),因此我倾向于认为您正在使用C ++编译器进行编译.这是为什么找不到stdbool.h标头的一个可能原因.如果要转换为C,请确保使用C编译器来构建代码.

Inasmuch as your code still contains some C++-isms (e.g. cout, a using statement, and C++-style #include), I'm inclined to believe that you are compiling with a C++ compiler. That is one conceivable reason why the stdbool.h header is not found. If you want to convert to C, then be sure to build your code with a C compiler.

这篇关于C编程中的布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆