C_simple/word/C2main.c

164 lines
5.7 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// Created by CC-star on 25-5-6.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define Words_File "../word/words.txt"
struct Words {
char English[50];
char Chinese[100];
// int ReciteIndex;//单词背诵次数【背诵正确+1错误-1】
} wordsList[500];
int wordsCount,SumRightCount = 0,SumLearnCount = 0;
int usedWords[500] = {0};
// 读取
int readWordsFromFile() {
FILE* file = fopen( Words_File, "r");
if (file == NULL) {
printf("\n⚠️无法打开单词库文件:%s⚠\n", Words_File);
return 0;
}else {
// 单词数
wordsCount = 0;
char line[100];
while (fgets(line, sizeof(line), file) != NULL ) {
sscanf(line, "%s %s",wordsList[wordsCount].English, wordsList[wordsCount].Chinese);
wordsCount++;
}
}
fclose(file);
if (wordsCount == 0) {
printf("\n\t⚠️单词库没有信息⚠️\n");
return 0;
}else {
return 1;
}
}
// 显示所有单词
void showAllWords() {
//readWordsFromFile();
printf("\n---------------------👇单词列表👇----------------------\n");
for (int i = 0; i < wordsCount; i++) {
printf("%d. %s - %s\n", i + 1, wordsList[i].English, wordsList[i].Chinese);
}
printf("-----------------------👆共计单词数:%d👆------------------------\n",wordsCount);
}
// 背单词功能
void startLearning() {
//readWordsFromFile();
int LearnCount,ret;//本次背诵数量
char ming[100];
int RightCount = 0;//正确数量单词
while (1){
printf("\n---------------------👇背单词区域👇----------------------\n");
printf("请输入要背诵的单词数量(不超过%d: ", wordsCount);
ret = scanf("%d", &LearnCount);
while(getchar() != '\n');
if (ret != 1 || LearnCount < 1 || LearnCount > wordsCount) {
do {
printf("输入无效请输入1到%d之间的数字\n", wordsCount);
ret = scanf("%d", &LearnCount);
while(getchar() != '\n');
}while (ret != 1 || LearnCount < 1 || LearnCount > wordsCount);
}
// printf("本次需要背诵:%d", LearnCount);
for (int i = 0; i < LearnCount; i++) {
int randomIndex;
do {
randomIndex = rand() % wordsCount;
} while (usedWords[randomIndex] == 1);
usedWords[randomIndex] = 1;
printf("单词:%s\n",wordsList[randomIndex].Chinese);
printf("请输入对应英文(不要有空格)\n");
scanf("%s", ming);
while (getchar() != '\n');
if (strcmp(wordsList[randomIndex].English, ming) == 0) {
RightCount++;
} else {
printf("错误,对应正确英文是:%s\n",wordsList[randomIndex].English);
}
}
printf("-----------------------👆背单词区域👆------------------------\n");
printf("\n---------------------👇单次成绩区域👇----------------------\n");
//总结提示
if (RightCount == LearnCount) {
printf("\t太棒啦,全部正确!!本次背了%d个单词分数为%d\n",LearnCount,RightCount);
}else if (RightCount > (LearnCount/2)){
printf("\t不错哦!!本次背了%d个单词分数为%d\n",LearnCount,RightCount);
}else {
printf("\t很遗憾😪,本次背了%d个单词只正确了%d个\n",LearnCount,RightCount);
}
printf("-----------------------👆单次成绩区域👆------------------------\n");
SumLearnCount += LearnCount;
SumRightCount += RightCount;
RightCount = 0;
char continueLearning;
while (1) {
printf("是否继续(Y/N)?\n");
scanf(" %c", &continueLearning);
if (continueLearning == 'Y' || continueLearning == 'y') {
break;
} else if (continueLearning == 'N' || continueLearning == 'n') {
printf("\n---------------------👇总成绩区域👇----------------------\n");
printf("\t本次共计背了%d个单词总正确数为%d\n", SumLearnCount, SumRightCount);
printf("-----------------------👆总成绩区域👆------------------------\n");
SumRightCount = 0;
SumLearnCount = 0;
return;
} else {
printf("输入有误,请重新输入\n");
}
}
}
}
int C2main() {
srand(time(NULL));
if (readWordsFromFile() == 1){
while (1) {
printf("\n---------------------👇背单词控制面板👇----------------------\n");
printf("1开始背单词\n2查看所有单词\n");
printf("0.返回主控制台\n");
printf("-----------------------👆选择对应数字👆------------------------\n");
printf("请输入选择:");
int opt;
scanf("%d", &opt);
while (getchar() != '\n');
switch (opt) {
case 1:
startLearning();
break;
case 2:
showAllWords();
break;
case 0:
printf("⚠️返回主控制台中...⚠️\n");
return 0;
default:
printf("⚠️输入有误,请重新输入⚠️\n");
break;
}
}
}else {
printf("⚠️背单词程序无法运作,已退出⚠️\n");
}
}