gpt4 book ai didi

c - 这段C代码如何解决?

转载 作者:行者123 更新时间:2023-11-30 20:07:39 25 4
gpt4 key购买 nike

当我尝试运行代码时,输​​出出现问题。

当输入 s= 1、m=1 时,即可输出太阳能和电源接触器结果。

当两个输入均为 0 即 s =0 且 m = 0 时,则给出“没有功率”。(正确输出)

当 s = 1、m = 0 时,应该显示“没有功率”。并且能够得到正确的输出。

当s=0,m=1时,它应该说“没有电源”,而是继续太阳能接触器信息(不是正确的输出)。

那么谁能解释一下这可能是什么问题?

#include<stdio.h>
#include<stdbool.h>
#define TRUE 1
#define FALSE 0

int main()
{
bool s, m; //input parameters
bool a, b;
bool p, q;
bool t; //output parameters

printf("Enter the value of solar VMD : \n"); //scanning vmd values of solar
scanf("%d", &s);
printf("Enter the value of Mains VMD : \n"); //scanning vmd values of Mains
scanf("%d", &m);

if(s == 1,m == 1)
{
printf("Scan solar contactor : \n"); //scanning solar contactor
scanf("%d", &a);
printf("Scan Mains contactor : \n"); //scanning mains contactor
scanf("%d", &b);

if(a == 1, b == 1) //when solar & mains contactor are close
{
q = FALSE;
p = TRUE;
printf("Solar contactor and Mains contactor: %d %d", p, q);
}
else if(a == 0, b == 1) //when solar contctor is open and mains is closed
{
q = FALSE;
p = TRUE;
printf("Solar contactor and Mains contactor: %d %d", p, q);
}
else if(a == 1, b == 0) //when solar contactor is closed and mains is open
{
q = FALSE;
p = TRUE;
printf("Solar contactor and Mains contactor: %d %d", p, q);
}
else if(a == 0, b == 0) //when both solar and mains are open
{
q = FALSE;
p = TRUE;
printf("Solar contactor and Mains contactor: %d %d", p, q);
}
else
{
printf("Problem with contactors");
}
}

else
{
printf("There is no power");
}

getchar();
getchar();
return 0;
}

最佳答案

if(s == 1,m == 1)

这段代码并没有做你想象的那样。了解 operators of the C language ,特别是logical operatorsthe comma operator .

更新

另一个错误是 %d 无法与 bool 一起使用。事实上,没有 scanf 格式可以做到这一点。因此,您必须要么坚持使用 int (这是执行 bool 运算符的完美类型),要么编写自己的函数来从流中读取 bool 。 p>

关于c - 这段C代码如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9347247/

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