我正在尝试测试一个由四个输入a、b、c和d组成的逻辑函数。
每个输入都是0或1。
我一直在尝试使用数组来完成这个任务。
如果logicXY数组中的列与combLogic数组匹配,我想返回1。
我知道这个函数并不完整,但我认为我没有正确地传递数组。我已经阅读了许多教程,但似乎无法掌握理论。
编辑 我已经向前迈了几步,但它仍然不起作用。这是我现在拥有的。
.h文件中的函数声明。
主.c文件的循环部分。
代码跨过了
编辑:
我对代码进行了一些更改,因此它似乎可以工作,并且只有一个编译器警告,即我无法修复.h中的函数声明。
每个输入都是0或1。
我一直在尝试使用数组来完成这个任务。
如果logicXY数组中的列与combLogic数组匹配,我想返回1。
int comb(int** combLogic, int** logicXY){
int x, y, logicOut;
for ( x = 0; x < 4; x++ ){
if (logicXY[0][x] == combLogic[x]){
logicOut = 1;
}
}
return logicOut;
}
int main void(){
int** combLogic[4] = {a,b,c,d}; /*logic input*/
int** logic[4][4] = {{1,0,0,1}, {1,0,1,1}, {0,1,0,0}, {0,1}}; /*return 1 if any of these combinations match the logic input*/
int comb(combLogic, logicXY); /*send to function*/
}
我知道这个函数并不完整,但我认为我没有正确地传递数组。我已经阅读了许多教程,但似乎无法掌握理论。
编辑 我已经向前迈了几步,但它仍然不起作用。这是我现在拥有的。
.h文件中的函数声明。
int comb(logicInput,logicTest);
.c文件中的函数
/* Function - Combination */
int comb(int** logicInput, int** logicTest){
int x, y, logicOut;
for ( x = 0; x < 4; x++ ){
if (logicTest[0][x] == logicInput[x]){
logicOut = 1;
}
}
return logicOut;
}
主.c文件的循环部分。
int output = 0;
int logicInput[4] = {0,1,1,1};
int logicTest[4][4] = {{1,0,0,1}, {1,0,1,1}, {0,1,0,0}, {0,1,1,1}};
int comb(logicInput,logicTest);
output = comb;
代码跨过了
int comb(logicInput,LogicTest)
,并且从未执行该函数。如果我从该行中删除int
,则它会执行该函数,返回值但是当该值写入输出时,它与从函数返回的值完全不同。编辑:
我对代码进行了一些更改,因此它似乎可以工作,并且只有一个编译器警告,即我无法修复.h中的函数声明。
warning: parameter names (without types) in function declaration [enabled by default]
.h文件中的函数声明
int comb(logicInput,logicTest);
.c中的函数
int comb(int** logicInput, int** logicTest){ /*Points to the arrarys in the main program*/
int x, i, logicOut;
for(i = 0; i < 4; i++){ /*test each column*/
for ( x = 0; x < 4; x++ ){ /*test each row*/
if (logicTest[i][x] == logicInput[i][x]){
logicOut = 1;
break;
}
}
if(logicOut == 1)break; /*Break when logicOut == 1 the first time it happens*/
}
return logicOut;
}
在main.c文件中循环
int output;
int logicInputC1[4] = {0,1,0,1};
int logicTestC1[4][4] = {{1,0,0,1}, {1,0,1,1}, {0,1,0,0}, {0,1,0,1}};
output = comb(logicInputC1,logicTestC1);
如果我偏离这段代码,似乎会导致编译器无法构建甚至更多的警告。
combLogic
和logic
的声明上,你可以查看这段代码以了解如何声明/传递int
二维矩阵?(链接:https://dev59.com/GHTYa4cB1Zd3GeqPs0rM#17567663) - Grijesh Chauhanmain()
函数中数组的声明和参数列表中。请注意,int **param
和int param[][4]
不是同一类型。您的编译器应该会对数组中使用1
作为初始化值而产生警告。它还应该会对调用comb()
时的类型不匹配产生警告。 - Jonathan Leffler