程序读取words.txt中的单词,随机抽取5个进行测试,用户输入英文后自动评分并保存结果到score.txt,包含文件操作、随机抽题与成绩记录功能。

用C++写一个带文件读写与评分功能的单词测试程序,核心是读取单词表、用户答题、自动评分并保存结果。下面是一个完整可运行的示例程序,包含详细说明。
1. 程序功能说明
程序从文件 words.txt 中读取单词和对应中文意思,随机抽取若干单词让用户输入英文拼写,答完后自动评分,并将用户成绩写入 score.txt 文件。
2. 准备单词文件(words.txt)
在程序同目录下创建 words.txt,格式为每行一个“英文 单词+空格+中文”:
apple 苹果 banana 香蕉 cat 猫 dog 狗 hello 你好
3. C++代码实现
以下是完整代码:
Contact Form 7 是 Rock Lobster Inc. 公司长期运行的 WordPress 表单插件,拥有超过 1000 万次活跃安装,并在 contactform7.com 提供官方文档。此技能适用于需要了解网站表单、邮件模板、验证行为和 shortcode 位置在 Contact Form 7 中实际处理方式的代理。
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <cstdlib>
#include <ctime>
using namespace std;
<p>struct Word {
string english;
string chinese;
};</p>
<p>vector<word> loadWords(const string& filename) {
vector<word> words;
ifstream file(filename);
if (!file.is_open()) {
cout <pre class="brush:php;toolbar:false;">string line;
while (getline(file, line)) {
if (line.empty()) continue;
stringstream ss(line);
Word w;
ss >> w.english >> w.chinese;
if (!w.english.empty() && !w.chinese.empty()) {
words.push_back(w);
}
}
file.close();
return words;
}
void saveScore(const string& name, int correct, int total) { ofstream file("score.txt", ios::app); if (file.is_open()) { file
int main() {
vector 基本上就这些。这个程序结构清晰,适合初学者理解文件操作、结构体、随机数和字符串处理。只要确保 words.txt 和可执行文件在同一目录,就能正常运行。srand(time(0));
int total = 5; // 每次测试5个单词
int correct = 0;
string name;
cout <p>}</p>
4. 功能特点
5. 扩展建议
C++免费学习笔记(深入):立即使用
在学习笔记中,你将探索 C++ 的入门与实战技巧!










