c++英文单词填空游戏

我在CSDN上搜了好多都要会员,气死我也,贴我自己的,比较菜,别笑啊!

#include <iostream>
#include <fstream>//改进,增加汉语提示功能,英汉互译 
#include <cstdlib>
#include <vector>
#include <random>
#include <iomanip>//问题:1.多次拼写错误就会变成乱码(已解决,随机数生成0,又规定了下标减一)                      
#include <ctime>//2.偶尔会出现超过规定范围的数 ,在较短字母长度尤其频繁 ,(怀疑是参数类型问题) 
#define display {cout<<"以下是缺失字母的位置:"<<endl;for (auto &i : v1) {cout << i << endl;	s[i - 1] = '_';	}}
using namespace std;//此段代码是较常出现的一段,本人无法拿捏是否符合规范,其作用为将vecto的部分字母变成"_"
vector<unsigned> rand_vector(size_t n, size_t m) { //用于生成随机的vector(生成几个,生成的最大值)
	static default_random_engine e(time(0));
	static uniform_int_distribution<unsigned> u(1, m);//static 只是保证在同一次程序的不同次调用中的随机数不同
	vector<unsigned> number;
	for (size_t i = 0; i < n; ++i)
		number.push_back(u(e));
	return number;
}
unsigned int rand_number(size_t m) { //用于生成随机数,m表示随机数范围的最大值
	static default_random_engine e(time(0));  //在程序第一次时种子是相同的
	static uniform_int_distribution<unsigned> u(1, m);
	return u(e);
}//将文件内容读入vector中
void  read_in(vector<string> &words, string filename) { //此处要改变vector内容,要引用
	fstream read;
	read.open(filename); //打开文件读
	string read_word;
	while (getline(read, read_word)) {
		words.push_back(read_word);//将单词库中的文件读入vector
	}
	read.close();
}// 将vector内容读入文件中
void  write_in(vector<string> &words, string filename) {
	ofstream fout(filename, ios::out);//如果加上::app清空文件夹功能会失效
	for (auto &i : words) {
		fout << i << endl;
	}
	fout.close();
}//将两个vector输出
void output(vector<string> &words, vector<string> &translation) {
	int j = 0, m = 0;
	for (auto &i : words ) { //范围for循环的嵌套???????如何并列执行两个for循环
		++j;
		m = 0;
		for (auto &k : translation ) {
			if (i.empty() || k.empty())//注意&&和||区别
				continue;
			++m;
			if (m == j)
				cout << j << " " << i << "  " << k << endl;
		}
	}
	cout  << "*****************************************" << endl;
}//计算正确率并输出相应的鼓励话语
void right_rate(double right, double sum) {
	string mode;
	mode="沙雕" ;
	double rate;
	rate = right / sum;
	cout << "你总共拼写了" << sum << "次" << endl;
	cout << "你拼写正确" << right << "次" << endl;
	cout << fixed << setprecision(2) << "你的准确率为" << rate * 100 << "%" << endl;
	if (mode == "沙雕") {
		if (rate <= 0.6) {
			cout << "我要是你就找块豆腐撞死算了!!!" << endl;
		} else if (rate <= 0.7) {
			cout << "就这水平,你英语是体育老师教的吗?" << endl;
		} else if (rate <= 0.8) {
			cout << "再不好好学,别人壁咚用的墙就是你砌的!!" << endl;
		} else if (rate <= 0.9) {
			cout << "凭这水平就想过四级,你在做梦吗?" << endl;
		} else if (rate < 1) {
			cout << "就你,配这个分儿吗?" << endl;
		} else {
			cout << "我*,这种小测验你都抄,你正式考试怎么办?不想要学位证书了是吧!" << endl;
		}
}else {
	if (rate <= 0.6) {
		cout << "成绩不是很好,要继续努力啊!" << endl;
	} else if (rate <= 0.7) {
		cout << "成绩还不够好哦,多多加油吧!" << endl;
	} else if (rate <= 0.8) {
		cout << "成绩还可以但不够好,继续加油!" << endl;
	} else if (rate <= 0.9) {
		cout << "成绩已经很好了,还可以再进一步!" << endl;
	} else if (rate < 1) {
		cout << "太厉害了,正确率很高了" << endl;
	} else {
		cout << "哇哦,这么牛逼,四级过了吗?" << endl;
	}
}
cout << "再来一次吗?(y/n)" << endl;
}


int main() {
	char m;
home_page://主页面
	system("title 英文单词填空游戏");
	ifstream page;
	page.open("界面.txt", ios::in);
	if (!page) {
		cout << "打开错误!" << endl;
		return 0;
	}
	string home_page;
	while (getline(page, home_page)) {
		cout << home_page << endl;
	}
	vector<string> words, translation;
	read_in(words, "单词库.txt"); //读入单词库
	read_in(translation, "翻译库.txt"); //读入翻译库
	int option;//选择,选项
start:
	cin >> option;
	switch (option) {//用switch结构表示三个选项
		case 1://普通模式
		case 2: {//简单模式		
			double sum = 0, right = 0;
game:
			system("cls");
			++sum;//单词意思为下标
			auto subscrip = rand_number(words.size());
			auto n = words[subscrip].size();
			string s = words[subscrip];
			cout << "单词长度为:" << n << endl;
			if (n >= 2 && n <= 4) {//根据单词长度生成对应数量的缺失字母
				vector<unsigned> v1;
				do {
					v1 = rand_vector(1, n);
				} while (v1[0] > n); //解决问题1的低级方法
				display;
			} else if (n <= 7) {
				vector<unsigned> v1;
				do {
					v1 = rand_vector(2, n);
				} while ((v1[0] == v1[1]) || (v1[0] > n || v1[1] > n)); //解决问题1的低级方法
				display;
			} else if (n <= 10) {
				vector<unsigned> v1;
				do {
					v1 = rand_vector(3, n);
				} while (v1[0] == v1[1] || v1[1] == v1[2] || v1[0] == v1[2]);
				display;
			} else {
				vector<unsigned> v1;
				do {
					v1 = rand_vector(4, n);
				} while (v1[0] == v1[1] || v1[1] == v1[2] || v1[2] == v1[3] || v1[0] == v1[2] || v1[0] || v1[3] || v1[3] == v1[4]);
				display;
			}
			cout << "***********************" << endl ;
			if (option == 2)
				cout << translation[subscrip] << endl;
			cout << s << endl;
			cout << "输入单词缺失的字母:" << endl;

			char m;
			for (auto &i : s) {
				if (i == '_') { //输入字母
					cin >> m;
					i = m;
				}
			}
			if (s == words[subscrip]) {
				cout << "回答正确!!!" << endl;
				++right;
			} else {
				cout << "拼错了" << endl;
			}
			cout << "是否继续?(y/n)" << endl;
			char p;
			cin >> p;
			if (p == 'y') {
				goto game;
			} else {
				right_rate( right, sum);
				char q;
				cin >> q;
				if (q == 'y') {
					right = 0; //初始化数据
					sum = 0;
					goto game;
				} else {
					system("cls");
					goto home_page	;
				}
			}
			break;
		}//英汉互译
		case 3: {
			double right = 0, sum = 0;
transla_game:
			system("cls");
			cout << "********************************" << endl;
			cout << "请输入该汉语所对应的英文单词:" << endl;
			size_t subscrip ;
			do {
				subscrip = rand_number(words.size());//生成缺失字母的单词,防止为空字符串
			} while (translation[subscrip].empty());
			cout << translation[subscrip] << endl;
			++sum;
			string new_word;
			cin >> new_word;
			if (new_word == words[subscrip]) {
				cout << "拼写正确!!!" << endl;
				++right;
			} else {
				cout << "拼写错误!!!" << endl;
			}
			cout << "是否继续?(y/n)" << endl;
			char p;
			cin >> p;
			if (p == 'y') {
				system("cls");
				goto transla_game;
			} else {
				right_rate( right, sum);
				char q;
				cin >> q;
				if (q == 'y') {
					right = 0; //初始化数据
					sum = 0;
					goto transla_game ;
				} else {
					system("cls");
					goto home_page	;
				}
			}
		}
		//查单词
		case 4: {
search:
			system("cls");
			cout << "请输入要查找的单词" << endl;
			string search;
			cin >> search;
			size_t find = 0;
			for (auto &i : words) {
				++find;
				if (i == search) {
					cout << "它的汉语翻译为:" << endl << translation[find - 1] << endl;
					break;
				}
			}
			if (find == words.size())
				cout << "啊!!!好可惜,单词库里还没有这个单词啊,单词库正在完善中……" << endl;
			cout << "你要继续查单词吗?(y/n)" << endl;
			char a;
			cin >> a;
			if (a == 'y') {
				system("cls");
				goto search;
			} else {
				system("cls");
				goto home_page;
			}
		}

		case 5: {	//单词库
			system("cls");
			cout << "*****单词库*********************************" << endl;
			output(words, translation);
			cout << "修改单词库(c)" << endl;
			cout << "返回主页面(r)" << endl;
			cin >> m;
			if (m == 'c') {
change:
				cout << "添加(t)" << endl;
				cout << "删除(s)" << endl;
				char o;
				cin >> o;
				if (o == 't') {
					system("cls"); //添加单词,添加是不能带空格,如果带空格,再次写入的时候会识别出空格,写到这里是就会停止 
					cout << "输入你要添加的单词:" << endl;
input:
					string new_word;
					cin >> new_word;
					for (auto &i : words) {
						if (i == new_word) {
							cout << "输入的单词重复,请重新输入一个吧!" << endl;
							goto input;
						}
					}
					words.push_back(new_word);//问题3:此处写成while循环输入出现问题!!!
					cout << "请添加相应的汉语翻译:" << endl;
					cin >> new_word;
					translation.push_back(new_word);
				} else {//删除单词
					system("cls");
					output(words, translation);
					cout << "输入你要删除的单词的序号:" << endl;
					int k;
					cin >> k;
					string space;
					words[k - 1] = space;
					translation[k - 1] = space;
				}
				system("cls");
				cout << "*********新的单词库*****************" << endl ;
				output(words, translation);
				cout << "你是否要继续?(y/n)" << endl;
				string choice;
				cin >> choice;
				if (choice == "y") {
					goto change;
				} else {
					system("cls");//此处在返回主页面之前,将单词库文件内容清空,然后将修改后的vector内容写入到文件中
					write_in(words, "单词库.txt"); //写入单词库
					write_in(translation, "翻译库.txt"); //写入翻译库
					goto home_page;
				}
			} else if (m == 'r') {
				system("cls");
				goto home_page;
			} else {
				cout << "你输入了错误命令,请再输入一遍;" << endl;
				goto change;
			}
		}
		break;//规则
		case 6: {
			system("cls");
			cout << "******游戏说明*******" << endl;
			ifstream rule;
			rule.open("规则.txt", ios::in);
			if (!rule) {
				cout << "打开错误!" << endl;
				return 0;
			}
			while (getline(rule, home_page)) {
				cout << home_page << endl;
			}
			cout << endl << "********************" << endl;
			cout << "结束游戏(b)" << endl;
			cout << "返回主页面(r)" << endl;
			cin >> m;
			if (m == 'r') {
				system("cls");
				goto home_page;
			}
		}
		break;
		case 7:
			break;
		default:
			cout << "你输错了!只能输1-7哦,再输入一遍吧!" << endl;
			goto start;
	}
	cout << "GAME OVER!!!";
	return 0;
}

以下是三个文件,自己新建粘贴一下吧,不会上传txt文件

单词库.txt

love
affection
contact
display
refuge
numerous
exclaim
content

image
surge
prone
definite
deliver
contract
student
remarkable
splash
idiotic
race
superior
wonderful
over
my
lover
where
are
you
computer
daiyinran

翻译库.txt

n.v.爱情.爱.爱人
n.喜爱.钟爱.爱慕
n.v.联络.联系.熟人
n.v.陈列.展示
n.庇护.避难
adj.众多的.许多的
v.呼喊.惊叫
n.adj.内容.目录.满意的

n.形象.画像.印象
v.n.涌动.激增
adj.有做……倾向的
adj.n.确定的.清楚地
v.递送.运送.接生
n.v.合同.合约.缩小.收缩
n.学生
adj.非凡的.不同寻常的
v.n.泼洒.泼溅声
adj.愚蠢的
n.v.赛跑.竞争.种族
adj.n.更好的.上司.上级
adj.极好的.精彩的
adv.prep.结束.落下.在……之上
det.我的
n.爱人.情人
adv.conj.在哪里;在那个地方
v.是
pron.你.你们
n.计算机
戴欣然,喜欢

规则.txt

英文单词填空游戏:
这是一款帮助学生背单词的小软件,建立单词库,可从单词库中
随机抽取单词,并随机隐去该单词中的一些字母,在屏幕上显示
带空格的单词,用户对空格处的字母进行补全,程序判断填补是
否正确,并统计正确率。

界面.txt

****************************************************
**********英文单词填空游戏**************************
***********【1】普通模式****************************
***********【2】简单模式(有汉语提示)**************
***********【3】汉译英******************************
***********【4】查单词******************************
***********【5】单词库******************************
***********【6】游戏说明****************************
***********【7】结束游戏****************************
****************************************************

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

依德子龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值