我正在为用户定义的与门输入数量摆弄动态数组。
我遇到的问题是我不知道用户要测试多少输入,我需要能够有一个 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/