gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:07:23 25 4
gpt4 key购买 nike

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

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

#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;
}

最佳答案

这可能与你的意图有些不同,
我认为从以下修改开始会很有用。

#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;
}

关于Ccrabble C 程序编译但不允许用户执行该文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46914591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com