C程序编译但不运行

标签 c

我正在用 C 编写一个单词搜索程序。该程序应该将包含单词搜索的文件(data1、data2、data3)作为输入,并输出仅包含已解决单词的搜索(包含在解决方案1、解决方案2中) ,解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] = ' ';
        }
    }    
}

最佳答案

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

get_Array 方法中,在第二个 for 循环中,现在它期望这样的输入:

A space B space C space \n

注意到最后一个空格了吗?我不确定这就是你的意图。只需删除最后一个 getchar(); 就可以了。

还需要注意的是,fgets() 还将包含字符串末尾的 NewLine,因此,如果您要分隔诸如 'word\word\n' 之类的单词 这可能是一个问题。

关于C程序编译但不运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33027885/

相关文章:

c - IPMI:如何使用 ipmimonitoring-sensors.c 查找阈值

c - 字符串常量和字符常量

c - 为什么在一个表达式中同时使用左移和右移会有所不同?

c - 如何在 OpenCV 中逐帧读取视频文件?

c - 原型(prototype) "const int* foo(int)"是什么意思,特别是与 "int* foo(int)"相比?我只理解第二个

c - setuid() 返回 0 但没有效果

c - 输出怎么会是 1 而不是 j 的地址

objective-c - Core Graphics 点彩化 CGImage 效果

C 链表 - 将指针写入文件

c - 在链表中出队