gpt4 book ai didi

C 初学者的多个 boolean 运算符问题

转载 作者:行者123 更新时间:2023-11-30 16:43:50 25 4
gpt4 key购买 nike

我正在寻找一种在单个表达式中使用多个 boolean 运算符的方法,但无法弄清楚。下面的代码尝试将以下内容转换为 C:

伪代码:

if 
(100 = 100) AND (10 is either 10 or 100)
print "1"
else
print "0"

C:

#include <stdio.h>
#include <stdbool.h>

int main(void)
{
int num;
num= 10;
bool n;
n = ((100==100) && (num == (10 || 100)));
printf("%i", n);
}

显然我做错了什么。

最佳答案

这个表达式

(a = b) and (c = (d or f) 

可以用 C 语言的 if 语句来表示

#include <iso646.h>

//...

if ( ( a == b ) and ( c == d or c == f ) )

或者就像

if ( ( a == b ) && ( c == d || c == f ) ) 

或者在表达式语句中,例如

#include <iso646.h>
#include <stdbool.h>

//...

bool n = ( a == b ) and ( c == d or c == f );

#include <stdbool.h>

//...

bool n = ( a == b ) && ( c == d || c == f );

关于C 初学者的多个 boolean 运算符问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44937243/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com