将 bool 结果打印为“false”的最佳方法是什么?或“真实”在C语言中?

发布于 2024-12-07 09:27:27 字数 2255 浏览 0 评论 0原文

我必须编写一个程序,其中 main 调用其他函数来测试一系列数字是否小于某个数字,是否所有系列数字都在两个限制之间,以及是否有负值。

我的代码返回值 1 表示 true,0 表示 false,但赋值要求将它们打印为“true”或“false”。我不知道如何从 printf 获取布尔答案作为字符串打印。我用了 if (atl == false) printf("false");在我的 at_least.c 文件和文件 main.c 中,但它只返回一长串 true 或 false(例如:truetruetrue....)。

我不确定这是否是正确的编码,我将其放在错误的位置,或者我需要使用其他一些代码。

这是我的 main.c 文件:

#include "my.h"

int main (void)
{
    int     x;
    int     count    = 0;
    int     sum      = 0;
    double  average  = 0.0;
    int     largest  = INT_MIN;
    int     smallest = INT_MAX;
    bool    atlst    = false;
    bool    bet      = true;
    bool    neg      = false;
    int     end;

    while ((end = scanf("%d",&x)) != EOF)
    {
        sumall(x, &sum);                        // Calling function sumall
        count++;
        larger_smaller(x, &largest, &smallest); // Calling function larger_smaller
        if (atlst == false)
           at_least(x, &atlst);                 // Calling function at_least if x < 50
        if (bet == true)
           between(x, &bet);                    // Calling function between if x is between 30 and 40 (inclusive)
        if (neg == false)
           negative(x, &neg);                   // Calling function negative if x < 0
    }

    average = (double) sum / count;
    print(count, sum, average, largest, smallest, atlst, bet, neg);
    return;
 }

我对一组数字的结果:

The number of integers is:           15
The sum is               :         3844
The average is           :       256.27
The largest is           :          987
The smallest is          :          -28
At least one is <  50    :            1     // This needs to be true
All between  30 and  40  :            0     // This needs to be false
At least one is negative :            1     // This needs to be true

这是用 C 语言编写的,我似乎找不到太多相关内容。

附录:

这是从下面的答案中重复的。

这适用于at_least函数,但不适用于 Between函数。我有

void between(int x, bool* bet)
{
    if (x >= LOWER && x <= UPPER)
        *bet = false;
    return;
}

作为我的代码。怎么了?

I have to write a program in which main calls other functions that test a series of number if any are less than a number, if all the series' numbers are between two limits, and if any are negative.

My code returns the values of 1 for true and 0 for false, but the assignment asks that they be printed as 'true' or 'false'. I'm not sure how to get the bool answers to print as a string from printf. I used if (atl == false) printf("false"); in my at_least.c file and in file main.c, but it returns only a long string of true or false (example: truetruetrue....).

I'm not sure if that is the correct coding and I'm putting it in the wrong spot or there was some other code that I need to use.

This is my main.c file:

#include "my.h"

int main (void)
{
    int     x;
    int     count    = 0;
    int     sum      = 0;
    double  average  = 0.0;
    int     largest  = INT_MIN;
    int     smallest = INT_MAX;
    bool    atlst    = false;
    bool    bet      = true;
    bool    neg      = false;
    int     end;

    while ((end = scanf("%d",&x)) != EOF)
    {
        sumall(x, &sum);                        // Calling function sumall
        count++;
        larger_smaller(x, &largest, &smallest); // Calling function larger_smaller
        if (atlst == false)
           at_least(x, &atlst);                 // Calling function at_least if x < 50
        if (bet == true)
           between(x, &bet);                    // Calling function between if x is between 30 and 40 (inclusive)
        if (neg == false)
           negative(x, &neg);                   // Calling function negative if x < 0
    }

    average = (double) sum / count;
    print(count, sum, average, largest, smallest, atlst, bet, neg);
    return;
 }

My results for a set of numbers:

The number of integers is:           15
The sum is               :         3844
The average is           :       256.27
The largest is           :          987
The smallest is          :          -28
At least one is <  50    :            1     // This needs to be true
All between  30 and  40  :            0     // This needs to be false
At least one is negative :            1     // This needs to be true

This is in C, which I can't seem to find much on.

Addendum:

This is repeated from an answer below.

This worked for the at_least and negative functions, but not for the between function. I have

void between(int x, bool* bet)
{
    if (x >= LOWER && x <= UPPER)
        *bet = false;
    return;
}

as my code. What's wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦中楼上月下 2024-12-14 09:27:27

备用无分支版本:

"false\0true"+6*x

Alternate branchless version:

"false\0true"+6*x
无风消散 2024-12-14 09:27:27

您可以使用 C 的 条件(或三元)运算符

  (a > b) ? "True" : "False";

或者也许在您的情况下:

  x ? "True" : "False";

You could use C's conditional (or ternary) operator:

  (a > b) ? "True" : "False";

Or perhaps in your case:

  x ? "True" : "False";
撩人痒 2024-12-14 09:27:27

<代码>x ? "true" : "false"

上面的表达式返回一个 char *,因此您可以像这样使用:

puts(x ? "true" : "false");< /代码>
或者
printf(" ... %s ... ", x ? "true" : "false");

您可能想为此创建一个宏。

x ? "true" : "false"

The above expression returns a char *, thus you can use like this:

puts(x ? "true" : "false");
or
printf(" ... %s ... ", x ? "true" : "false");

You may want to make a macro for this.

我很坚强 2024-12-14 09:27:27

使用这个:

#include <stdio.h>

#define BOOL_FMT(bool_expr) "%s=%s\n", #bool_expr, (bool_expr) ? "true" : "false"

int main(int iArgC, char ** ppszArgV)
{
    int x = 0;
    printf(BOOL_FMT(x));

    int y = 1;
    printf(BOOL_FMT(y));

    return 0;
}

这将打印出以下内容:

x=false
y=true

将此与类型 bool 一起使用,但 int 应该以相同的方式工作。

Use this one:

#include <stdio.h>

#define BOOL_FMT(bool_expr) "%s=%s\n", #bool_expr, (bool_expr) ? "true" : "false"

int main(int iArgC, char ** ppszArgV)
{
    int x = 0;
    printf(BOOL_FMT(x));

    int y = 1;
    printf(BOOL_FMT(y));

    return 0;
}

This prints out the following:

x=false
y=true

Using this with type bool, but int should work the same way.

信愁 2024-12-14 09:27:27

我有三个数组:

strbool.c:

#include "strbool.h"

const char alx_strbool[2][6]    = {"false", "true"};
const char alx_strBool[2][6]    = {"False", "True"};
const char alx_strBOOL[2][6]    = {"FALSE", "TRUE"};

strbool.h:

#pragma once

/* rename without alx_ prefix */
#if defined(ALX_NO_PREFIX)
#define strbool     alx_strbool
#define strBool     alx_strBool
#define strBOOL     alx_strBOOL
#endif

extern  const char alx_strbool[2][6];
extern  const char alx_strBool[2][6];
extern  const char alx_strBOOL[2][6];

免责声明:前缀是必要的,因为以 str 开头的名称为 保留(实际上,只有 strbool),但在 C 或 POSIX 实际使用它们之前,我将使用简短版本。

使用它们非常简单:

#include <stdio.h>

#define ALX_NO_PREFIX
#include "strbool.h"

int main(void)
{
        int var = 7;

        printf("var is %s\n", strBool[!!var]);

        return 0;
}
var is True

I have three arrays for that:

strbool.c:

#include "strbool.h"

const char alx_strbool[2][6]    = {"false", "true"};
const char alx_strBool[2][6]    = {"False", "True"};
const char alx_strBOOL[2][6]    = {"FALSE", "TRUE"};

strbool.h:

#pragma once

/* rename without alx_ prefix */
#if defined(ALX_NO_PREFIX)
#define strbool     alx_strbool
#define strBool     alx_strBool
#define strBOOL     alx_strBOOL
#endif

extern  const char alx_strbool[2][6];
extern  const char alx_strBool[2][6];
extern  const char alx_strBOOL[2][6];

Disclaimer: The prefixes are necessary because names starting with str are reserved (actually, only strbool), but until they are actually used by C or POSIX, I will use the short version.

It's very simple to use them:

#include <stdio.h>

#define ALX_NO_PREFIX
#include "strbool.h"

int main(void)
{
        int var = 7;

        printf("var is %s\n", strBool[!!var]);

        return 0;
}
var is True
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文