飞花令C语言代码
时间: 2023-07-09 07:21:08 浏览: 85
以下是一个简单的飞花令 C 语言代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_WORD_LEN 20
#define MAX_SENT_LEN 100
char* get_random_word(char* word_list[], int list_len) {
int rand_index = rand() % list_len;
return word_list[rand_index];
}
int main() {
char* word_list[] = {"秋", "夜", "月", "霜", "枫", "雁", "江", "柳", "花", "桥"};
int list_len = sizeof(word_list) / sizeof(char*);
srand(time(NULL));
char input[MAX_SENT_LEN];
printf("请输入一句话:");
fgets(input, MAX_SENT_LEN, stdin);
// 分割输入的句子
char* tokens[MAX_SENT_LEN];
char* token = strtok(input, " \t\n");
int num_tokens = 0;
while (token != NULL && num_tokens < MAX_SENT_LEN) {
tokens[num_tokens++] = token;
token = strtok(NULL, " \t\n");
}
// 随机替换词语
for (int i = 0; i < num_tokens; i++) {
if (strlen(tokens[i]) <= MAX_WORD_LEN) {
char* new_word = get_random_word(word_list, list_len);
printf("%s ", new_word);
} else {
printf("%s ", tokens[i]);
}
}
printf("\n");
return 0;
}
```
这个程序会等待用户输入一句话,然后将其中的词语随机替换为预定义的词语列表中的词语。注意,为了简化代码,这个程序没有考虑一些复杂的情况,比如标点符号和大小写等等。