试图找出如何对角搜索

问题描述 投票:-2回答:1

我正在研究一个涉及单词搜索的代码,我似乎遇到了从右上到左下对角搜索的问题。您可以帮我吗?

此外,我的代码也不允许使用toUpper函数或任何辅助函数。有没有一种方法可以使char **列表中的字符暂时大写以与char ** arr进行比较,但是当它打印出来时,它应该与大写之前的字符相同。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// DO NOT INCLUDE OTHER LIBRARY!

// Declarations of the two functions you will implement
// Feel free to declare any helper functions
void printPuzzle(char** arr, int n);
void searchPuzzle(char** arr, int n, char** list, int listSize);
int horRight(char** arr, int n, char* word);
int horLeft(char** arr, int n, char* word);
int vertDown(char** arr, int n, char* word);
int diaRight(char** arr, int n, char* word);
int diaLeft(char** arr, int n, char* word);
void lowerCase(char** arr, int wordSize, int row, int col, int direction);

// Main function, DO NOT MODIFY!!!  
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*));

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

// Read puzzle block into 2D arrays
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);

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

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

// Remove newline characters from each word (except for the last word)
for(i=0; i<49; i++){
    *(*(words+i) + strlen(*(words+i))-2) = '\0';    
}

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

// Print out original puzzle grid
printf("Printing puzzle before search:\n");
printPuzzle(block, bSize);
printf("\n");

// Call searchPuzzle to find all words in the puzzle
searchPuzzle(block, bSize, words, 50);
printf("\n");

// Print out final puzzle grid with found words in lower case
printf("Printing puzzle after search:\n");
printPuzzle(block, bSize);
printf("\n");

return 0;
}

void printPuzzle(char** arr, int n){
// This function will print out the complete puzzle grid (arr). It must produce the output in the SAME format as the samples in the instructions.
// Your implementation here
int i, j;

for(i=0; i<n; i++) {
    printf("%c %c %c %c %c %c %c %c %c %c %c %c %c %c %c\n", *(*(arr+i)), *(*(arr+i)+1), *(*(arr+i)+2), *(*(arr+i)+3), *(*(arr+i)+4), *(*(arr+i)+5), *(*(arr+i)+6), *(*(arr+i)+7), *(*(arr+i)+8), *(*(arr+i)+9), *(*(arr+i)+10), *(*(arr+i)+11), *(*(arr+i)+12), *(*(arr+i)+13), *(*(arr+i)+14));

}}

void searchPuzzle(char** arr, int n, char** list, int listSize){
// This function checks if arr contains words from list. If a word appears in arr, it will print out that word and then convert that word entry in arr into lower case.
// Your implementation here
char** tempList = malloc(50 * sizeof(char**));
int i;

for(i=0; i<listSize; i++){
    if(horRight(arr, n, *(tempList+i))) {
        printf("Word found: %s\n", *(list+i));
    }
    if(horLeft(arr, n, *(list+i))){
        printf("Word found: %s\n", *(list+i));
    }
    if(vertDown(arr, n, *(list+i))){
        printf("Word found: %s\n", *(list+i));
    }
    if(diaRight(arr, n, *(list+i))){
        printf("Word found: %s\n", *(list+i));
    }
    if(diaLeft(arr, n, *(list+i))){
        printf("Word found: %s\n", *(list+i));
    }

}}

int horRight(char** arr, int n, char* word){
int size = strlen(word);
int i, j, k, index;

for(i=0; i<n; i++){
    for(j=0; j<n-size; j++){
       index = 0;
       while(index<size){
           //checks if characters are matching
           if(toupper(*(word+index)) == *(*(arr+i)+index+j)){  //change
               //if index equals the size of word, make arr lowercase
               if(index == size-1){
                   lowerCase(arr, size, i, j, 1);
                   return 1;;
               }
           }
           else{
               break;
           }
           index++;
       }
   }
}
return 0;
}

int horLeft(char** arr, int n, char* word){
int size = strlen(word);
int i, j, index;

for(i=0; i<n; i++){
    for(j=n-1; j>=size-1; j--){
        index = 0;
        while(index<size){
            if(toupper(*(word+index)) == *(*(arr+i)+j-index)){ //change
                if(index == size-1){
                    lowerCase(arr, size, i, j, 2);
                    return 1;
                }
            }
            else{
                break;
            }
            index++;
        }
    }
}
return 0;

}

int vertDown(char** arr, int n, char* word){
int size = strlen(word);
int i, j, index;

for(i=0; i<n; i++){
    for(j=0; j<n-size; j++){
        index = 0;
        while(index<size){
            if(toupper(*(word+index)) == *(*(arr+index+j)+i)){ //change
                if(index == size-1){
                    lowerCase(arr, size, j, i, 3);
                    return 1;;
                }
            }
            else{
                break;
            }
            index++;
        }
    }
}
return 0;

}

int diaRight(char** arr, int n, char* word){
int size = strlen(word);
int i, j, index;

for(i=0; i<n; i++){
    for(j=0; j<n; j++){
        index = 0;
        while(index<size && (i+index<n) && (j+index<n)){
            if(toupper(*(word+index)) == *(*(arr+i+index)+j+index)){ //change
                if(index == size-1){
                    lowerCase(arr, size, i, j, 4);
                    return 1;;
                }
            }
            else{
                break;
            }
            index++;
        }
    }
}
return 0;

}

int diaLeft(char** arr, int n, char* word){
int size = strlen(word);
int i, j, index;

for(/*i=n-1; i>0; i--*/){
    for(/*j=0;j<n;j++*/){
        index=0;
        while(/*index<size && (i+index<n) && (j+index<n)*/){
            if(/*touper(*(word+index)) == *(*(arr+i-index)+j+index))*/){
               if(index == size-1){
                    lowerCase(arr, size, i, j, 5);
                    return 1;;
                }
            }
            else{
                break;
            }
            index++;
        }
    }
}
return 0;

}

void lowerCase(char** arr, int wordSize, int row, int col, int direction){
int i,j;

switch(direction){

    case 1: //horizontal left to right
        for(j=col; j<col+wordSize; j++){
            if(*(*(arr+row)+j) >= 'A' && *(*(arr+row)+j) <= 'Z'){
                *(*(arr+row)+j) = *(*(arr+row)+j) + 32;
            }
        }
        break;
    case 2: //horizontal right to left
        for(j=col; j>col-wordSize; j--){
            if(*(*(arr+row)+j) >= 'A' && *(*(arr+row)+j) <= 'Z'){
                *(*(arr+row)+j) = *(*(arr+row)+j) + 32;
            }
        }
        break;
    case 3: //vertical top to bottom
        for(j=row; j<row+wordSize; j++){
            if(*(*(arr+j)+col) >= 'A' && *(*(arr+j)+col) <= 'Z'){
                *(*(arr+j)+col) = *(*(arr+j)+col) + 32;
            }
        }
        break;
    case 4: //diagonal top left to bottom right
        for(j=0;j<wordSize;j++){
            if(*(*(arr+row+j)+col+j) >= 'A' && *(*(arr+row+j)+col+j) <= 'Z'){
                *(*(arr+row+j)+col+j) = *(*(arr+row+j)+col+j) + 32;
            }
        }
        break;
    case 5: //diagonal top right to bottom left
        for(/*code*/){
            if(*(*(arr+row+j)+col+j) >= 'A' && *(*(arr+row+j)+col+j) <= 'Z'){
                *(*(arr+row+j)+col+j) = *(*(arr+row+j)+col+j) + 32;
            }
        }
}

}

c uppercase diagonal double-pointer
1个回答
0
投票

如果在函数声明后阅读注释

// This function checks if arr contains words from list. If a word appears
// in arr, it will print out that word and then convert that word entry in
// arr to lower case.
// Your implementation here

然后似乎您需要的是以下内容。我只将int类型替换为size_t类型的函数参数。

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

void searchPuzzle( char **arr, size_t n, char **list, size_t listSize ) 
{
    // This function checks if arr contains words from list. If a word appears
    // in arr, it will print out that word and then convert that word entry in
    // arr to lower case.
    // Your implementation here

    for ( size_t i = 0; i < n; i++ )
    {
        size_t j = 0;

        while ( j < listSize && strcmp( arr[i], list[j] ) != 0 ) ++j;

        if ( j != listSize )
        {
            puts( arr[i] );

            for ( char *p = arr[i]; *p; ++p )
            {
                *p = tolower( ( unsigned char )*p );
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.