C程序编译但不运行

时间:2015-10-09 00:04:37

标签: c

我正在使用C语言搜索程序。程序应该将包含单词搜索的文件(data1,data2,data3)作为输入,并仅使用已解决的单词(包含在solution1,solution2中)输出搜索,解决方案3)。该程序编译良好,但当我尝试运行它时,没有任何反应。我的代码如下。任何帮助表示赞赏。

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

#define MAX 50

/*Function Prototypes*/
void get_Array(char word_Puzzle[MAX][MAX], int *size);
void get_Words(char words[MAX][MAX], int *num_words);
void print_Array(char word_Puzzle[MAX][MAX], int size);
void print_Words(char words[MAX][MAX]);
int word_Search(char word_Puzzle[MAX][MAX], char current_word[MAX], int dir, int pos, int row, int col, char solved[MAX][MAX], int size);
void solve_puzzle(char word_Puzzle[MAX][MAX], char solved[MAX][MAX], char words[MAX][MAX], int size, int num_words);
void initialize_Array(char array[MAX][MAX], int size);

int main()
{
    char word_Puzzle[MAX][MAX];
    char solved[MAX][MAX];
    char words[MAX][MAX];

    int size = 0;
    int num_words = 0;
    int a;

    /*Get the input from stdin*/
    get_Array(word_Puzzle, &size);
    get_Words(words, &num_words);
    initialize_Array(solved, size);

    /*Solve the puzzle*/
    solve_puzzle(word_Puzzle, solved, words, size, num_words);

    /*Finally, Print it!*/
    print_Array(solved, size);

    stdin = fopen("/dev/tty", "r");

    printf("Enter something:\n");
    scanf("%d", &a);
    printf("%d\n",a);

    return 0;
}

void get_Array(char word_Puzzle[MAX][MAX], int *size)
{
    char current_char = 'a';
    int row, col, temp = 0;

    /*Get the first line, setting the size of the array.*/
    while(current_char != '\n')
    {
        current_char = getchar();
        if(current_char != '\n' && current_char != ' ')
        {
            word_Puzzle[0][temp] = current_char;
            temp++;
        }
    }

    /* Assign size the value of the temp variable */
    *size = temp;

    /* temp is now the length of the array, lets fill the rest in. */
    for(row = 1; row < temp; row++)
    {
        for(col = 0; col < temp; col++)
        {
            word_Puzzle[row][col] = getchar();
            /*Do another getchar to skip the space*/
            getchar();
        }
        /*Do a getchar to skip the newline character*/
        getchar();
    }
}

void get_Words(char words[MAX][MAX], int *num_words)
{
    char current_word[MAX + 1];
    char *scanned_word = current_word;
    int temp = 0, pos = 0;

    do {
        /*Take in the word*/
        scanned_word = fgets(current_word, 50, stdin);

        if(scanned_word == NULL)
            break;

        strcpy(words[pos], scanned_word);
        pos++;
        temp++;
    }while(current_word != NULL);

    *num_words = temp;
}

void print_Array(char word_Puzzle[MAX][MAX], int size)
{
    int row, col;

    for(row = 0; row < size; row++)
    {
        for(col=0; col < size; col++)
        {
            printf("%c ", word_Puzzle[row][col]);
        }
        printf("\n");
    }
}

void print_Words(char words[MAX][MAX])
{
    int row;

    for(row = 0; row < MAX + 1; row++)
    {
        printf("%s", words[row]);
    }
}

int word_Search(char word_Puzzle[MAX][MAX], char current_word[MAX], int dir,    int pos, int row, int col, char solved[MAX][MAX], int size)
{
    /*End of word, return 1!*/
    if (current_word[pos] == '\n' || current_word[pos] == '\0')
        return 1;

    /*If its out of bounds, return 0.*/
    if (row < 0 || col < 0 || row >= size || col >= size)
        return 0;


    /*Switch the direction of the search, that way it only goes the direction
    its looking.*/
    switch(dir)
    {
        /*Search L2R, row+0, col+1*/
        case 0:
            if(current_word[pos] == word_Puzzle[row][col])
            {
                if(word_Search(word_Puzzle, current_word, dir, pos+1, row, col+1, solved, size))
                {
                    solved[row][col] = word_Puzzle[row][col];
                    return 1;
                }
            }
            break;

        /*Search R2L, row+0, col-1*/
        case 1:
            if(current_word[pos] == word_Puzzle[row][col])
            {
                if(word_Search(word_Puzzle, current_word, dir, pos+1, row, col-1, solved, size))
                {
                    solved[row][col] = word_Puzzle[row][col];
                    return 1;
                }
            }
            break;

        /*Search B2T, row-1, col+0*/
        case 2:
            if(current_word[pos] == word_Puzzle[row][col])
            {
                if(word_Search(word_Puzzle, current_word, dir, pos+1, row-1, col, solved, size))
                {
                    solved[row][col] = word_Puzzle[row][col];
                    return 1;
                }
            }
            break;

        /*Search T2B, row+1, col+0*/
        case 3:
            if(current_word[pos] == word_Puzzle[row][col])
            {
                if(word_Search(word_Puzzle, current_word, dir, pos+1, row+1, col, solved, size))
                {
                    solved[row][col] = word_Puzzle[row][col];
                    return 1;
                }
            }
            break;

        /*Search ULD, row-1, col-1*/
        case 4:
            if(current_word[pos] == word_Puzzle[row][col])
            {
                if(word_Search(word_Puzzle, current_word, dir, pos+1, row-1, col-1, solved, size))
                {
                    solved[row][col] = word_Puzzle[row][col];
                    return 1;
                }
            }
            break;

        /*Search URD, row-1, col+1*/
        case 5:
            if(current_word[pos] == word_Puzzle[row][col])
            {
                if(word_Search(word_Puzzle, current_word, dir, pos+1, row-1, col+1, solved, size))
                {
                    solved[row][col] = word_Puzzle[row][col];
                    return 1;
                }
            }
            break;

        /*Search BLD, row+1, col-1*/
        case 6:
            if(current_word[pos] == word_Puzzle[row][col])
            {
                if(word_Search(word_Puzzle, current_word, dir, pos+1, row+1, col-1, solved, size))
                {
                    solved[row][col] = word_Puzzle[row][col];
                    return 1;
                }
            }
            break;

        /*Search BRD, row+1, col+1*/
        case 7:
            if(current_word[pos] == word_Puzzle[row][col])
            {
                if(word_Search(word_Puzzle, current_word, dir, pos+1, row+1, col+1, solved, size))
                {
                    solved[row][col] = word_Puzzle[row][col];
                    return 1;
                }
            }
            break;
    }

    return 0;
}

void solve_puzzle(char word_Puzzle[MAX][MAX],char solved[MAX][MAX], char words[MAX][MAX], int size, int num_words)
{
    char current_word[MAX + 1];
    int found = 0;
    int word, r, c, d;

    /*Go through each word in the word array...*/
    for(word = 0; word < num_words; word++)
    {
        strcpy(current_word, words[word]);
        found = 0;

        /*...and search each row...*/
        for(r = 0; r < size; r++)
        {
            /*...each column...*/
            for(c = 0; c < size; c++)
            {
                /*...and each direction till its found, and if it is, BREAK!*/
                for(d = 0; d < 8; d++)
                {
                    if(word_Search(word_Puzzle, current_word, d, 0, r, c, solved, size))
                    {
                        found = 1;
                        break;
                    }
                }
                if(found)
                    break;
            }
            if(found)
                break;
        }
    }
}

/*Set all the cells in the solved array for cleanliness.*/
void initialize_Array(char array[MAX][MAX], int size)
{
    int row, col;

    for(row = 0; row < size; row++)
    {
        for(col = 0; col < size; col++)
        {
            array[row][col] = ' ';
        }
    }    
}

1 个答案:

答案 0 :(得分:0)

我认为可能会破坏您的代码的一些问题:

get_Array方法中,在第二个for循环中,它现在的方式是它需要这样的输入:

A space B space C space \n

请注意最后一个空格?我不确定那是你的意图。只需删除最后一个getchar();即可。

同样重要的是要注意fgets()还会包含NewLine吃掉字符串的结尾,所以如果要分隔像'word\word\n'这样的单词,这可能是一个问题。