代码附上:
#include <stdio.h>
#include <string.h>
#define MAX_WORDS 100
struct Word {
char definition[200];
char term[50];
};
void addWord(struct Word words[], int* count, const char* definition, const char* term) {
if (*count >= MAX_WORDS) {
printf("已达到最大单词数量。\n");
return;
}
struct Word newWord;
strcpy(newWord.definition, definition);
strcpy(newWord.term, term);
words[*count] = newWord;
(*count)++;
printf("单词已添加。\n");
}
void testWords(struct Word words[], int count, int* score, int* currentWordIndex) {
for (int i = *currentWordIndex; i < count; i++) {
printf("\033[1m\n中文解释:%s\033[0m\n", words[i].definition);
printf("请输入对应的英文单词(输入'back'返回主界面):");
char userTerm[50];
scanf("%s", userTerm);
if (strcmp(userTerm, "back") == 0) {
*currentWordIndex = i;
return;
}
if (strcmp(userTerm, words[i].term) == 0) {
printf("\033[32m回答正确!\033[0m\n");
(*score)++;
}
else {
printf("\033[31m回答错误!正确答案是:%s\033[0m\n", words[i].term);
}
if (*score >= 10) {
printf("\n你通过了第一关测试!\n");
// 故事情节
printf("\n故事情节:\n");
printf("经过艰苦的学习,你终于掌握了网络知识的精髓。在这个过程中,你结识了一群志同道合的朋友,大家相互帮助、共同进步。");
printf("你们一起组队参加了网络知识大赛,在激烈的角逐中取得了优异的成绩,成为了网络知识领域的佼佼者!");
printf("最终,你们获得了丰厚的奖金,还受邀参加了国际性的网络技术交流会议,成为了备受瞩目的网络达人。");
return;
}
}
}
void printWords(struct Word words[], int count) {
if (count == 0) {
printf("还没有添加单词。\n");
return;
}
printf("\033[1m\n所有单词:\n\033[0m");
for (int i = 0; i < count; i++) {
printf("\033[1m%s\033[0m: %s\n", words[i].definition, words[i].term);
}
}
int main() {
struct Word words[MAX_WORDS];
int count = 0;
int choice;
int score = 0;
int currentWordIndex = 0;
int testMode = 0;
addWord(words, &count, "ADSL", "非对称数字用户线路");
// 添加更多单词...
addWord(words, &count, "ARP", "地址解析协议");
addWord(words, &count, "CDMA", "码分多址");
addWord(words, &count, "CSMA/CD", "载波监听多点接入/碰撞检测");
addWord(words, &count, "DHCP", "动态主机配置协议");
// 添加更多单词...
while (1) {
if (testMode == 0) {
printf("\n1. 添加单词\n");
printf("2. 开始测试\n");
printf("3. 查看得分\n");
printf("4. 查看所有单词\n");
printf("5. 退出程序\n");
printf("请选择操作:");
scanf("%d", &choice);
}
else {
printf("\n闯关模式 - 当前得分:%d\n", score);
printf("请输入'back'返回主界面\n");
testWords(words, count, &score, ¤tWordIndex);
if (currentWordIndex >= count) {
printf("\n你已完成所有闯关!\n");
printf("故事大结局:你通过了所有的测试,成为了网络知识的大师,赢得了众人的钦佩和尊重!");
return 0;
}
continue;
}
switch (choice) {
case 1:
{
char definition[200];
char term[50];
printf("\n请输入要添加的中文解释:");
scanf(" %[^\n]s", definition);
printf("请输入对应的英文单词:");
scanf("%s", term);
addWord(words, &count, definition, term);
}
break;
case 2:
testMode = 1;
currentWordIndex = 0;
score = 0;
break;
case 3:
printf("\n当前得分:%d\n", score);
break;
case 4:
printWords(words, count);
break;
case 5:
printf("\n程序已退出。\n");
return 0;
default:
printf("\n无效的选择,请重新输入。\n");
}
}
return 0;
}