简体   繁体   English

为什么我永远不会超过 1?

[英]Why does i never get past 1?

I am trying to create a program to solve a wordsearch from a 2D array using only pointers.我正在尝试创建一个程序来仅使用指针从二维数组中解决单词搜索。 In my primary function for doing the actual search for the words I have a while loop that is supposed to keep going as long as i doesn't exceed the length of the word and as long as the letters from the word correspond to the letters on the grid.在我的主要 function 中,用于实际搜索单词,我有一个 while 循环,只要我不超过单词的长度并且单词中的字母对应于网格。 After finding the word it should make the letters on the grid lowercase and print out that it found the word.找到单词后,它应该将网格上的字母小写并打印出它找到了单词。 Here is my entire program right now.这是我现在的整个程序。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

void printPuzzle(char** arr, int n);
void searchPuzzle(char** arr, int n, char** list, int listSize);
void searchWord(int j, int k, int gridsize, char** arr, char** list, int listPos);

int main(int argc, char **argv) {
    int bSize = 15;
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <puzzle file name>\n", argv[0]);
        return 2;
    }
    int i, j;
    FILE *fptr;
    char **block = (char**)malloc(bSize * sizeof(char*));
    char **words = (char**)malloc(50 * sizeof(char*));

    fptr = fopen(argv[1], "r");
    if (fptr == NULL) {
        printf("Cannot Open Puzzle File!\n");
        return 0;
    }

    for(i=0; i<bSize; i++){
        *(block+i) = (char*)malloc(bSize * sizeof(char));

        fscanf(fptr, "%c %c %c %c %c %c %c %c %c %c %c %c %c %c %c\n", *(block+i), *(block+i)+1, *(block+i)+2, *(block+i)+3, *(block+i)+4, *(block+i)+5, *(block+i)+6, *(block+i)+7, *(block+i)+8, *(block+i)+9, *(block+i)+10, *(block+i)+11, *(block+i)+12, *(block+i)+13, *(block+i)+14 );
    }
    fclose(fptr);

    fptr = fopen("states.txt", "r");
    if (fptr == NULL) {
        printf("Cannot Open Words File!\n");
        return 0;
    }

    for(i=0; i<50; i++){
        *(words+i) = (char*)malloc(20 * sizeof(char));
        fgets(*(words+i), 20, fptr);        
    }

    for(i=0; i<49; i++){
        *(*(words+i) + strlen(*(words+i))-2) = '\0';    
    }

    printf("Printing list of words:\n");
    for(i=0; i<50; i++){
        printf("%s\n", *(words + i));       
    }
    printf("\n");

    printf("Printing puzzle before search:\n");
    printPuzzle(block, bSize);
    printf("\n");

    searchPuzzle(block, bSize, words, 50);
    printf("\n");

    printf("Printing puzzle after search:\n");
    printPuzzle(block, bSize);
    printf("\n");

    return 0;
}

void printPuzzle(char** arr, int n){
    for(int i = 0; i < n; i++){
        printf("\n");
        for(int j = 0; j < 15; j++){
            printf("%c ", *(*(arr+i)+j));
        }
    }

}

void searchPuzzle(char** arr, int n, char** list, int listSize){
    for(int i = 0; i < listSize; i++){
        for(int j = 0; j < 15; j++){
            for(int k = 0; k < 15; k++){
                    searchWord(j, k, 15, arr, list, i);
                }
            }

        }

}


void searchWord(int j, int k, int gridsize, char** arr, char** list, int listPos){
    int wordlength = strlen(*(list+listPos));
    if(j+wordlength <= gridsize){ //Horizontal
        int i = 0;
        while(i < wordlength && *(*(arr+(j+i))+k) == *(*(list+listPos)+i)){
            i++;
        }
        if(i == wordlength){
            while(i > 0 && *(*(arr+(j+i))+k) == *(*(list+listPos)+i)){
            *(*(arr+(j+i))+k) = tolower(*(*(arr+(j+i))+k));
            i--;
            }
            printf("Word found: ");
            for(i = 0; i < wordlength; i++)
                printf("%c", *(*(list+listPos)+i));
        }
    }

    if(k+wordlength <= gridsize){ //Vertical
        int i = 0;
        while(i < wordlength && *(*(arr+j)+(k+i)) == *(*(list+listPos)+i)){
            i++;
        }
        if(i == wordlength){
            while(i > 0 && *(*(arr+j)+(k+i)) == *(*(list+listPos)+i)){
            *(*(arr+(j+i))+k) = tolower(*(*(arr+(j+i))+k));
            i--;
            }
            printf("Word found: ");
            for(i = 0; i < wordlength; i++){
                printf("%c", *(*(list+listPos)+i));
            }
        }
    }

    if(j+wordlength <= gridsize && k+wordlength <= gridsize){ //Diagonal
        int i = 0;
        while(i < wordlength && *(*(arr+(j+i))+k) == *(*(list+listPos)+i)){
            i++;
        }
        if(i == wordlength){
            while(i > 0 && *(*(arr+(j+i))+(k+i)) == *(*(list+listPos)+i)){
            *(*(arr+(j+i))+(k+i)) = tolower(*(*(arr+(j+i))+(k+i)));
            i--;
            }
            printf("Word found: ");
            for(i = 0; i < wordlength; i++)
                printf("%c", *(*(list+listPos)+i));
        }
    }



}

This is the part I believe there is a problem with.这是我认为有问题的部分。

while(i < wordlength && *(*(arr+(j+i))+k) == *(*(list+listPos)+i)){
            i++;
        }

If I print out the decimal value of i after this while loop it should at some point approach the length of one of the words as I know some words are horizontal, however every time it loops through and prints the value of i it never exceeds 1 which means the while loop never runs more than once which is impossible with the wordsearch files I am using to test this.如果我在这个 while 循环之后打印出 i 的十进制值,它应该在某个时候接近其中一个单词的长度,因为我知道有些单词是水平的,但是每次它循环并打印 i 的值它永远不会超过 1这意味着 while 循环永远不会运行超过一次,这对于我用来测试它的 wordsearch 文件是不可能的。 What is causing this to happen?是什么导致这种情况发生? It is also 0 or 1 for every other direction when I try, the example above was just the while loop from the horizontal search.当我尝试时,每隔一个方向它也是 0 或 1,上面的示例只是水平搜索中的 while 循环。

There may also be a problem with the function searchPuzzle and the amount it loops through. function searchPuzzle 及其循环次数也可能存在问题。 However, I think that may be just my imagination.不过,我想这可能只是我的想象。

Additional Info: The grid is 15x15 and there are 50 words in the list.附加信息:网格为 15x15,列表中有 50 个单词。

I think that you might need to declare Wordsearch again after the && -我认为您可能需要在 && 之后再次声明 Wordsearch -

while(i < wordlength && wordlength *(*(arr+(j+i))+k) == *(*(list+listPos)+i)){
            i++;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM