c++ - 向条件语句添加条件

标签 c++ c++11 if-statement dynamic-arrays

我正在为用户定义的与门输入数量摆弄动态数组。

我遇到的问题是我不知道用户要测试多少输入,我需要能够有一个 if-else 语句来测试每个输入。

#include <iostream>
#include <iomanip>
#include <string> 

using namespace std;

class logic_gate {
public:
    int x = 0;

};

int main() {

int userInput = 0;

cout << "How many inputs do you want on your and gate?: ";
cin >> userInput;
cout << endl;

logic_gate *and_gate = new logic_gate[userInput];

cout << endl << "Please enter the values of each bit below . . ." << endl << 
endl;

int userTest1 = 0;


for (int i = 0; i < userInput; i++) {

    cout << "#" << i + 1 << ": ";
    cin >> userTest1;
    and_gate[i].x = userTest1;

}

return 0;
}

这是我目前正在尝试寻找解决方案的代码。

最佳答案

要使用 n 输入实现与门,您只需执行以下操作:

int output = 1;
for (int i = 0; i < n; ++i)
{
    if (!and_gate [i])
    {
        output = 0;
        break;
    }
}

// ...

关于c++ - 向条件语句添加条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53696343/

相关文章:

c++ - 生成不同范围内的随机 double

MySQL If (SELECT count (*) 返回 #1064 错误

c++ - z3 中的常量——c++ api 中的段错误

c++ - 每当我尝试在 C++ 中使用 setfill 操纵器时,它只会显示空白

c++ - 最小化 Linux 共享库的依赖性

c++ - 为什么拥有多行 constexpr 函数是非法的?

c++ - dup2 复制而不是重定向

c++ - ffmpeg:对 'uncompress' 的 undefined reference

java - java中如何动态检查条件?

Python if 语句延迟