gpt4 book ai didi

c - 逻辑检查未按预期输出。 (数独检查器)C

转载 作者:行者123 更新时间:2023-11-30 16:05:49 24 4
gpt4 key购买 nike

我有一个函数可以检查数独板的所有行,如果无效则输出并退出。

(有效行在 9 方格行上有 1-9)

我已经盯着我的逻辑看了 30 分钟,不明白为什么一个我知道有效的董事会却不断地吐出无效的结果。

为了尝试使其更易于阅读,这是有问题的部分......

void* checkRow(void* p){

int check[9] = {0};
parameters* temp = (parameters*) p;
int tempRow = temp -> row;
int tempCol = temp -> col;

for (int i = 0; i < SIZE; i++){ // looping to find each # 1-9

for (int j = 0; j < SIZE; j++){

if (board[i][j] == 1)
check[0] = 1;
if (board[i][j] == 2)
check[1] = 1;
if (board[i][j] == 3)
check[2] = 1;
if (board[i][j] == 4)
check[3] = 1;
if (board[i][j] == 5)
check[4] = 1;
if (board[i][j] == 6)
check[5] = 1;
if (board[i][j] == 7)
check[6] = 1;
if (board[i][j] == 8)
check[7] = 1;
if (board[i][j] == 9) // changing value to 1 if found
check[8] = 1;

int k = 0;
while(k < 9){
if (check[k] == 0){
printf("invalid solution"); // should only print if 1-9 isn't found right?
exit(0);
}
k++;
}

memset(check, 0, sizeof(check)); // resetting array to zero

}

}

}

这里是所有内容,以防万一。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>

#define SIZE 9

typedef struct{ // referenced from assignment page
int row;
int col;
} parameters;

int board[SIZE][SIZE]; // global variable holding board

pthread_t tRow, tCol, t1, t2, t3, t4, t5, t6, t7, t8, t9;

void setThreads();
void* checkRow(void* p);

int main(int argc, char **argv){ // referenced project 1. Supplies command line input for file

FILE *fp;

fp = fopen(argv[1], "r");

if (fp == NULL) // validity check
exit(1);

for (int i = 0; i < SIZE; i++){
for(int j = 0; j < SIZE; j++){
fscanf(fp, "%d", &board[i][j]);
}
}

setThreads();
printf("rows check out");

return 0;

}

void setThreads(){

parameters *rowcolparameter = (parameters *) malloc(sizeof(parameters));
rowcolparameter -> row = 0;
rowcolparameter -> col = 0;

parameters *square1 = (parameters *) malloc(sizeof(parameters));
square1 -> row = 0;
square1 -> col = 0;

parameters *square2 = (parameters *) malloc(sizeof(parameters));
square2 -> row = 0;
square2 -> col = 3;

parameters *square3 = (parameters *) malloc(sizeof(parameters));
square3 -> row = 0;
square3 -> col = 6;

parameters *square4 = (parameters *) malloc(sizeof(parameters));
square4 -> row = 3;
square4 -> col = 0;

parameters *square5 = (parameters *) malloc(sizeof(parameters));
square5 -> row = 3;
square5 -> col = 3;

parameters *square6 = (parameters *) malloc(sizeof(parameters));
square6 -> row = 3;
square6 -> col = 6;

parameters *square7 = (parameters *) malloc(sizeof(parameters));
square7 -> row = 6;
square7 -> col = 0;

parameters *square8 = (parameters *) malloc(sizeof(parameters));
square8 -> row = 6;
square8 -> col = 3;

parameters *square9 = (parameters *) malloc(sizeof(parameters));
square9 -> row = 6;
square9 -> col = 6;

pthread_create(&tRow, NULL, checkRow, rowcolparameter);

pthread_join(tRow, NULL);

}

void* checkRow(void* p){

int check[9] = {0};
parameters* temp = (parameters*) p;
int tempRow = temp -> row;
int tempCol = temp -> col;

for (int i = 0; i < SIZE; i++){ // looping to find each # 1-9

for (int j = 0; j < SIZE; j++){

if (board[i][j] == 1)
check[0] = 1;
if (board[i][j] == 2)
check[1] = 1;
if (board[i][j] == 3)
check[2] = 1;
if (board[i][j] == 4)
check[3] = 1;
if (board[i][j] == 5)
check[4] = 1;
if (board[i][j] == 6)
check[5] = 1;
if (board[i][j] == 7)
check[6] = 1;
if (board[i][j] == 8)
check[7] = 1;
if (board[i][j] == 9)
check[8] = 1;

int k = 0;
while(k < 9){
if (check[k] == 0){
printf("invalid solution"); // it should only say invalid if 1-9 wasn't found right?
exit(0);
}
k++;
}

memset(check, 0, sizeof(check)); // resetting array to 0

}

}

}

谢谢。

最佳答案

似乎“检查部分”位于内循环内部!它不应该在内循环外部吗?喜欢:

  for (int i = 0; i < SIZE; i++){ // looping to find each # 1-9

for (int j = 0; j < SIZE; j++){

if (board[i][j] == 1)
check[0] = 1;
if (board[i][j] == 2)
check[1] = 1;
if (board[i][j] == 3)
check[2] = 1;
if (board[i][j] == 4)
check[3] = 1;
if (board[i][j] == 5)
check[4] = 1;
if (board[i][j] == 6)
check[5] = 1;
if (board[i][j] == 7)
check[6] = 1;
if (board[i][j] == 8)
check[7] = 1;
if (board[i][j] == 9) // changing value to 1 if found
check[8] = 1;
} // End the inner loop

// Now do the check
int k = 0;
while(k < 9){
if (check[k] == 0){
printf("invalid solution"); // should only print if 1-9 isn't found right?
exit(0);
}
k++;
}

memset(check, 0, sizeof(check)); // resetting array to zero

}

顺便说一句:您可以像这样减少内循环代码:

  // inner loop
for (int j = 0; j < SIZE; j++){
if (board[i][j] < 1 || board[i][j] > 9) exit(1); // Illegal board value

check[board[i][j] - 1] = 1; // Mark value as found
}

关于c - 逻辑检查未按预期输出。 (数独检查器)C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60163288/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com