坚持逻辑门C程序。 (C新手)

时间:2016-10-09 18:46:18

标签: c logic

感谢您帮助我,这是我的代码部分。

printf("Thank you, now please enter the logic gate");
scanf("%s", &C);

if (C == 'AND')
{
    if (A == 1 && B == 1)
    {
        (A && B == 1);
        printf("You have chosen the AND logic gate \n");
        printf("%d\n", A);
    }
    else
    {
        printf("You have chosen the AND logic gate \n");
        A = 0;
        printf("%d\n", A);
    }
}

我真的很困惑我将如何存储多个字符的输入。如“AND”或其他东西。

我的代码似乎没有输入任何IF语句,非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

  1. 首先使用字符数组来读取逻辑门char str[10]; scanf("%s", str)
  2. 的类型
  3. 然后将字符串比作if (!strcmp(str, "AND")) { // matched }

答案 1 :(得分:0)

您的代码是否编译? 显示A,B和C的变量声明。

printf("Thank you, now please enter the logic gate");
scanf("%s", &C);
if (C == 'AND') 
// '' means char, you most likely want string here
// so you need to use strncmp() to compare strings

if (A == 1 && B == 1)
{
    (A && B == 1);
    // ^ This is not a valid syntax for comparing values