如何在C语言中将用户输入的类别名作为数组名使用?
解决词汇学习游戏中按类别调用对应词汇数组的问题
开发一款词汇学习类游戏,流程是先询问用户想要学习的词汇类别,将类别名存入全局字符数组
category,再调用对应类别的词汇数组。现有代码片段如下,需要实现当用户选择Animals这类类别后,在question函数中利用存储的类别名调用对应数组,完成用户输入与词汇的对比判断:
char Animals[10][11] = {"Eagle", "Cat", "Dog", "Rabbit", "Whale", "Snake", "Butterfly", "Dolphin", "Elephant", "Turtle"}; void askCategory() { printf("\nAnimals Nature\n"); printf("Clothes Food\n"); printf(" Fruit Jobs\n"); printf("\nWhich category of vocabulary would you like to learn? "); scanf("%s", category); } void question(){ char userInput[15] = ""; printf("\n\n%d.What does this picture represent? ",row + 1); scanf("%s", userInput); if(strcmp(userInput, ???)==0) printf("Correct!\n"); }
核心思路
通过类别名-词汇数组映射的方式,把每个类别名称和对应的词汇数组关联起来,这样就能根据category的值直接找到对应的数组,再进行后续的对比操作。
具体实现步骤
- 补充所有类别的词汇数组,保持和
Animals一致的二维字符数组格式; - 定义结构体存储类别名称和对应词汇数组的指针,建立映射关系;
- 在
question函数中遍历映射结构,匹配category找到对应词汇数组; - 结合当前题目索引
row,完成用户输入与目标词汇的对比。
完整代码示例
#include <stdio.h> #include <string.h> // 全局变量:存储用户选择的类别 char category[20]; // 当前题目索引(标记当前是第几个词汇) int row = 0; // 定义所有类别的词汇数组 char Animals[10][11] = {"Eagle", "Cat", "Dog", "Rabbit", "Whale", "Snake", "Butterfly", "Dolphin", "Elephant", "Turtle"}; char Nature[10][11] = {"Mountain", "River", "Forest", "Cloud", "Rain", "Sun", "Moon", "Star", "Wind", "Snow"}; char Clothes[10][11] = {"Shirt", "Pants", "Shoes", "Hat", "Socks", "Jacket", "Skirt", "Dress", "Scarf", "Gloves"}; char Food[10][11] = {"Apple", "Banana", "Bread", "Milk", "Rice", "Meat", "Fish", "Egg", "Cake", "Juice"}; char Fruit[10][11] = {"Orange", "Grape", "Peach", "Pear", "Watermelon", "Strawberry", "Cherry", "Mango", "Pineapple", "Kiwi"}; char Jobs[10][11] = {"Teacher", "Doctor", "Engineer", "Farmer", "Nurse", "Artist", "Writer", "Chef", "Police", "Firefighter"}; // 类别-词汇数组映射结构体 typedef struct { const char* name; char (*vocab)[11]; // 指向二维字符数组的指针 int count; // 该类别词汇总数 } CategoryMap; // 初始化映射数组 CategoryMap categoryMaps[] = { {"Animals", Animals, 10}, {"Nature", Nature, 10}, {"Clothes", Clothes, 10}, {"Food", Food, 10}, {"Fruit", Fruit, 10}, {"Jobs", Jobs, 10} }; // 映射数组长度 int mapCount = sizeof(categoryMaps) / sizeof(CategoryMap); void askCategory() { printf("\nAnimals Nature\n"); printf("Clothes Food\n"); printf(" Fruit Jobs\n"); printf("\nWhich category of vocabulary would you like to learn? "); scanf("%s", category); } void question(){ char userInput[15] = ""; printf("\n\n%d.What does this picture represent? ", row + 1); scanf("%s", userInput); // 查找当前类别对应的词汇数组 char (*currentVocab)[11] = NULL; for(int i = 0; i < mapCount; i++){ if(strcmp(category, categoryMaps[i].name) == 0){ currentVocab = categoryMaps[i].vocab; break; } } // 对比用户输入并输出结果 if(currentVocab != NULL && strcmp(userInput, currentVocab[row]) == 0){ printf("Correct!\n"); } else { printf("Wrong! The correct answer is: %s\n", currentVocab ? currentVocab[row] : "Unknown category"); } } // 测试主函数 int main(){ askCategory(); // 循环出完该类别所有词汇题 for(row = 0; row < 10; row++){ question(); } return 0; }
关键细节说明
- 结构体指针:
char (*vocab)[11]是指向长度为11的字符数组的指针,完美匹配我们定义的词汇数组类型(每个词汇最多10个字符+1个结束符); - 类别匹配:通过遍历
categoryMaps数组,用strcmp对比类别名称,快速定位到对应的词汇数组; - 异常处理:加入
currentVocab != NULL的判断,避免用户输入不存在的类别导致程序崩溃。
内容的提问来源于stack exchange,提问作者luna
相关产品推荐
相关产品推荐

