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

标签 c implementation

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

当输入 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/

相关文章:

java - 一个接口(interface),其中所有方法具有多个实现类,每个实现类实现该接口(interface)中方法的子集

php - ArrayObject 上的 array_slice(或其他 array_* 函数)

c - __memcpy_sse2_unaligned - 这是什么意思?

c - ungetc() 的意外行为

c - 聚合子对象的隐式初始化

c# - 使用 IEnumerator 和使用数组作为属性之间的区别

c++ - 为什么分配器需要构造和销毁接口(interface)?

c# - 从其他接口(interface)继承的接口(interface)的显式 C# 接口(interface)实现

使用 Clang 进行交叉编译 - `crtbeginS.o: No such file or directory`

c - 如何心跳X11屏保?