Ccrabble C程序编译但不允许用户执行该文件

时间:2017-10-24 15:34:48

标签: c compiler-errors

应该提示用户要解码几个字母,然后输入他们的单词,返回该单词的分数。

我可以让程序编译,但程序实际上并没有运行。所以我不完全确定我的代码中的问题在哪里。我在这方面相对较新。

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

#define num_letters_input 7

int main()
{
    const int alphabet_count[26] = {8, 2, 2, 4, 12, 2, 3, 2, 9, 1, 1, 4, 2, 6, 8, 2, 1, 6, 4, 6, 4, 2, 2, 1, 2, 1};
    const int alphabet_value[26] = {1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10};
    int letter_set[26];
    int size_letter_set = 26;

    void generate_letter_set(int letter_set[], int size_letter_set, int num_letters) {

        int random_a = rand() % 26;
        int random_b = rand() % (alphabet_count[random_a]);
        letter_set[random_a] = random_b;
        printf("Your letters are: ");
        for(int i=0; i < size_letter_set; i++) {
            if(letter_set[i])
            {
                int num = letter_set[i];
                while(num--)
                    printf("%c ", i+97);
            }
        }

        printf("\n");
    }

    int read_word(char word[], int max_size_word) {

        printf("Enter your word: ");
        scanf("%s", word);
        printf("%s \n", word);
        int size_word = strlen(word);
        if(size_word > max_size_word)
            read_word(word, max_size_word);
        return size_word;
    }

    bool check_word(char word[], int size_word, int letter_set[], int size_letter_set) {

        int count_array[26];
        for(int i=0; i<size_word; i++) {
            count_array[word[i]-97]++;
        }
        for(int i=0; i<size_letter_set; i++) {
            if(count_array[i] <= letter_set[i])
                continue;
            else {
                return false;
            }
            return true;
        }

        int compute_word_value(char word[], int size_word) {

            int word_value = 0;
            for(int i=0; i<size_word; i++) {
                word_value = word_value + alphabet_value[word[i]-97];
            }
            return word_value;
        }

        int main(void) {

            int max_size_word = 7;
            int size_word, word_value;
            bool validity = false;
            char word[95];
            printf("This program plays the game of scrabble.\n");
            for(int i=0; i < size_letter_set; i++) {
                letter_set[i] = 0;
            }
            generate_letter_set(letter_set, size_letter_set, num_letters_input);
            while(!validity) {
                size_word = read_word(word, max_size_word);
                validity = check_word(word, size_word, letter_set, size_letter_set);
                if(!validity)
                {
                    printf("The word is not valid. Use your letters: ");
                    for(int i=0; i < size_letter_set; i++) {
                        if(letter_set[i])
                        {
                            int num = letter_set[i];
                            while(num--) {
                                printf("%c ", i+97);
                            }
                        }
                    printf("\n");
                    }
                }
                printf("The value of your word is: ");
                word_value = compute_word_value(word, size_word);
                printf("%d", word_value);
                printf("Thank you for playing.");
            }
        }
    }
    return 0;
}

1 个答案:

答案 0 :(得分:1)

可能与您的意图有所不同,
我认为从以下修改开始是有用的。

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

#define num_letters_input 7

const int alphabet_count[26] = {8,2,2,4,12,2,3,2,9,1,1,4,2,6,8,2, 1,6,4,6,4,2,2,1,2, 1};
const int alphabet_value[26] = {1,3,3,2, 1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10};


void generate_letter_set(int letter_set[], int size_letter_set, int num_letters) {
    printf("Your letters are: ");

    for(int i = 0; i < num_letters; i++){
        int random_a = rand() % 26;
        int random_b = rand() % alphabet_count[random_a];
        letter_set[random_a] = random_b;
    }
    for(int i = 0; i < size_letter_set; ++i){
        if(letter_set[i]){
            int num = letter_set[i];

            while(num--)
                printf("%c ", 'a' + i);
        }
    }
    printf("\n");
}

int read_word(char word[], int max_size_word) {
    int size_word = 0;
    do {
        printf("Enter your word: ");
        scanf("%s", word);
        size_word = strlen(word);
    } while(size_word > max_size_word);

    return size_word;
}

bool check_word(char word[], int size_word, int letter_set[], int size_letter_set) {
    int count_array[26] = {0};

    for(int i = 0; i < size_word; i++) {
        count_array[word[i]-'a']++;
    }

    for(int i=0; i < size_letter_set; i++) {
        if(count_array[i] <= letter_set[i])
            continue;
        else
            return false;
    }
    return true;
}

int compute_word_value(char word[], int size_word) {
    int word_value = 0;

    for(int i=0; i<size_word; i++) {
        word_value += alphabet_value[word[i]-97];
    }

    return word_value;
}

int main(void){
    int letter_set[26] = {0};
    int size_letter_set = 26;
    int max_size_word = num_letters_input;
    int size_word, word_value;
    bool validity = false;
    char word[96];
    srand(time(NULL));

    printf("This program plays the game of scrabble.\n");
    generate_letter_set(letter_set, size_letter_set, num_letters_input);

    while(!validity) {
        size_word = read_word(word, max_size_word);
        validity = check_word(word, size_word, letter_set, size_letter_set);

        if(!validity){
             printf("The word is not valid.\nUse your letters: ");
        }
    }

    printf("The value of your word is: ");
    word_value = compute_word_value(word, size_word);
    printf("%d\n", word_value);
    printf("Thank you for playing.\n");
    return 0;
}