文件结构:

背词历史.log 用来存放背过的单词,存放的格式是

年-月-日 时:分:秒

单词 词性 中文解释

生词本.txt 用来存放当下要背诵的单词列表,格式是

单词 词性 中文解释

历史记录.txt 用来当做按照时间查询生词的缓存,记录最后一个词查询的结果

其格式与 生词本.txt 一样

主要功能:

更新日志:

2018-9-4 背词宝version1.0 诞生
2018-9-4 背词宝version1.1 诞生
新增的内容:
1.新增背词功能,在背诵完生词后生词会自动从生词表删除,并且添加到背词历史中
2.新增历史生词查询功能,可以根据当天的年与日查询背诵完的生词

代码:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <iomanip>    //输出控制头文件
#include <time.h>
#include <windows.h>
using namespace std;class Recite{fstream file;fstream file1;
public:Recite();            //创建生词本文件void insert_word();  //添加单词void query_all();    //查询所有单词void query_by_time();//根据时间查历史记录void query_history();//查询历史背词void query_exact();  //精确查词void delete_word();  //删除单词int get_num();       //返回生词本中单词的数量void recite_word();  //背生词void update_log();   //更新日志void run();          //总的服务界面
};
Recite::Recite() {file.open("生词本.txt");file.close();file.open("背词历史.log");file.close();file.open("历史记录.txt");file.close();
}
void Recite::insert_word() {clock_t startTime, endTime;file.open("生词本.txt", ios::out | ios::app);   //在文件末尾处写入if (file.is_open() == 1) {//打开成功的话startTime = clock();char word[20], cha[5], trans[20];   //单词 词性 解释cout << "请输入要写入错题本的单词:";cin >> word;cout << "请输入单词的词性:";cin >> cha;cout << "请输入单词的解释:";cin >> trans;file << word << " " << cha << " " << trans<<endl;  //1就代表没有被删除的单词file.close();endTime = clock();cout << "写入成功,总共用时" << (double)(endTime - startTime) / CLOCKS_PER_SEC << " s" << endl;}else {cout << "打开文件失败" << endl;system("pause");}}
void Recite::query_all() {clock_t startTime, endTime;startTime = clock();char buffer[100];int number = 0;  //记录记录的条数cout << " --------+----+---------" << endl;cout << "|" << setw(8) << "单词";cout << "|" << setw(4) << "词性";cout << "|" << setw(8) << "翻译";cout << "|" << endl;cout << " --------+----+---------" << endl;file.open("生词本.txt", ios::in | ios::app);while (!file.eof()) {file.getline(buffer, 100);istringstream is(buffer);string s1, s2, s3,s4;is >> s1 >> s2 >> s3>>s4;if (s1 != ""&&s2 != ""&&s3 != "") {number++;cout << "|" << setw(8) << s1;cout << "|" << setw(4) << s2;cout << "|" << setw(8) << s3;cout << "|" << endl;cout << " --------+----+---------" << endl;}}endTime = clock();cout << "总共有" << number << "条记录,总共用时"<<(double)(endTime - startTime)/CLOCKS_PER_SEC<<" s"<<endl;file.close();
}
void Recite::query_by_time() {file.open("背词历史.log", ios::in | ios::out | ios::app);if (file.is_open()) {string time;cout << "请输入要查询的历史记录的年月日,格式为年-月-日:";cin >> time;string word[100], cha[100], trans[100];int i = 0;char buffer[100];while (!file.eof()) {file.getline(buffer, 100);istringstream is(buffer);string t1, t2;is >> t1 >> t2;if (t1 == time) {while (!file.eof()) {file.getline(buffer, 100);istringstream input(buffer);string s1, s2, s3;input >> s1 >> s2 >> s3;if (s1 != ""&&s2 != ""&&s3 != "") {word[i] = s1;cha[i] = s2;trans[i] = s3;i++;}else {if (s1 == time)continue;elsebreak;}}}}file.close();file.open("历史记录.txt", ios::in | ios::out | ios::trunc);for (int j = 0; j < i; j++)file << word[j] << " " << cha[j] << " "<<trans[j] << endl;file.close();query_history();}else {cout << "文件打开失败" << endl;return;}
}
void Recite::query_history() {clock_t startTime, endTime;startTime = clock();char buffer[100];int number = 0;  //记录记录的条数cout << " --------+----+---------" << endl;cout << "|" << setw(8) << "单词";cout << "|" << setw(4) << "词性";cout << "|" << setw(8) << "翻译";cout << "|" << endl;cout << " --------+----+---------" << endl;file.open("历史记录.txt", ios::in | ios::app);while (!file.eof()) {file.getline(buffer, 100);istringstream is(buffer);string s1, s2, s3, s4;is >> s1 >> s2 >> s3 >> s4;if (s1 != ""&&s2 != ""&&s3 != "") {number++;cout << "|" << setw(8) << s1;cout << "|" << setw(4) << s2;cout << "|" << setw(8) << s3;cout << "|" << endl;cout << " --------+----+---------" << endl;}}endTime = clock();cout << "总共有" << number << "条记录,总共用时" << (double)(endTime - startTime) / CLOCKS_PER_SEC << " s" << endl;file.close();
}
void Recite::query_exact() {clock_t startTime, endTime;char buffer[100];int i,number=0;cout << "1.按照单词查词" << endl;cout << "2.按照词性查词" << endl;cout << "3.按照中文解释查词" << endl;cout << "请输入需要确定查词方式:";cin >> i;startTime = clock();string word;cout << "请输入要查的单词:";cin >> word;cout << " --------+----+---------" << endl;cout << "|" << setw(8) << "单词";cout << "|" << setw(4) << "词性";cout << "|" << setw(8) << "翻译";cout << "|" << endl;cout << " --------+----+---------" << endl;file.open("生词本.txt", ios::in);switch (i) {case 1:while (!file.eof()) {file.getline(buffer, 100);istringstream is(buffer);string s1, s2, s3;is >> s1 >> s2 >> s3;if (s1 == word) {number++;cout << "|" << setw(8) << s1;cout << "|" << setw(4) << s2;cout << "|" << setw(8) << s3;cout << "|" << endl;cout << " --------+----+---------" << endl;}}endTime = clock();cout << "查询成功,一共有" << number << "条记录,用时:" << (double)(endTime - startTime) / CLOCKS_PER_SEC << " s" << endl;file.close();break;case 2:while (!file.eof()) {file.getline(buffer, 100);istringstream is(buffer);string s1, s2, s3;is >> s1 >> s2 >> s3;if (s2 == word) {number++;cout << "|" << setw(8) << s1;cout << "|" << setw(4) << s2;cout << "|" << setw(8) << s3;cout << "|" << endl;cout << " --------+----+---------" << endl;}}endTime = clock();cout << "查询成功,一共有" << number << "条记录,用时:" << (double)(endTime - startTime) / CLOCKS_PER_SEC << " s" << endl;file.close();break;case 3:while (!file.eof()) {file.getline(buffer, 100);istringstream is(buffer);string s1, s2, s3;is >> s1 >> s2 >> s3;if (s3 == word) {number++;cout << "|" << setw(8) << s1;cout << "|" << setw(4) << s2;cout << "|" << setw(8) << s3;cout << "|" << endl;cout << " --------+----+---------" << endl;}}endTime = clock();cout << "查询成功,一共有" << number << "条记录,用时:" << (double)(endTime - startTime) / CLOCKS_PER_SEC << " s" << endl;file.close();break;default://默认用单词查询while (!file.eof()) {file.getline(buffer, 100);istringstream is(buffer);string s1, s2, s3;is >> s1 >> s2 >> s3;if (s1 == word) {number++;cout << "|" << setw(8) << s1;cout << "|" << setw(4) << s2;cout << "|" << setw(8) << s3;cout << "|" << endl;cout << " --------+----+---------" << endl;}}endTime = clock();cout << "查询成功,一共有" << number << "条记录,用时:" << (double)(endTime - startTime) / CLOCKS_PER_SEC << " s" << endl;file.close();break;}
}
int Recite::get_num() {file1.open("生词本.txt", ios::in);   //以只读方式打开生词本char buffer[100];int number = 0;while (!file.eof()) {file.getline(buffer, 100);istringstream is(buffer);string s1, s2, s3;is >> s1 >> s2 >> s3;if (s1 != " "&&s2 != " "&&s3 != " ")number++;}file1.close();return number;
}
void Recite::delete_word() {query_all();   //显示所有的记录string str;clock_t startTime, endTime;cout << "请输入想要删除的单词:";cin >> str;startTime = clock();file.open("生词本.txt",ios::in);char buffer[100];string str1[100],str2[100],str3[100];int i = 0;while (!file.eof()) {file.getline(buffer, 100);istringstream is(buffer);string s1, s2, s3;is >> s1 >> s2 >> s3;if (s1 != str && s1 != "" && s2 != "" && s3!="" ) {str1[i] = s1;str2[i] = s2;str3[i] = s3;i++;}}file.close();file.open("生词本.txt", ios::out|ios::trunc);  //以截断方式打开文件,清空所有内容for (int j = 0; j < i; j++) {file << str1[j] << " " << str2[j] << " " << str3[j] << " " << endl;}file.close();endTime = clock();cout << "删除成功,用时:" << (double)(endTime - startTime) / CLOCKS_PER_SEC << " s" << endl;
}
void Recite::recite_word() {file.open("生词本.txt", ios::in|ios::out);  if (file.is_open() == 1) {clock_t startTime, endTime;//遍历后将单词拷贝至内存string word[100], cha[100], trans[100], str;char buffer[100];int i = 0;while (!file.eof()) {file.getline(buffer, 100);istringstream is(buffer);string s1, s2, s3;is >> s1 >> s2 >> s3;if (s1 != ""&&s2 != ""&&s3 != "") {word[i] = s1;cha[i] = s2;trans[i] = s3;i++;}}int number = i;cout << "本次需要复习的单词数量是:" << number << endl;system("pause");system("cls");int num_of_recite[100];   //记录需要背诵单词的次数,一开始都是1for (int k = 0; k < 100; k++)num_of_recite[k] = 1;int sucessful = 0;            //判断单词是否背完了,背完了就是1,没有背完就是0if (number == 0)sucessful = 1;int num = 0;startTime = clock();while (sucessful == 0) {for (int j = 0; j < i; j++) {if (num_of_recite[j] != 0) {cout << "中文意思:" << trans[j] << " " << cha[j] << endl;cout << "请输入单词:";cin >> str;if (str == word[j]) {cout << "正确!";num_of_recite[j]--;system("pause");system("cls");num++;if (num == number)sucessful = 1;}else {cout << "错误,正确答案是:" << word[j];num_of_recite[j]++;system("pause");system("cls");}}}}endTime = clock();cout << "恭喜你背完啦~~,用时:" << (double)(endTime - startTime) / CLOCKS_PER_SEC << " s" << endl;//背完单词后清空单词表file.close();file.open("生词本.txt", ios::out | ios::trunc);file.close();//然后写入日志file.open("背词历史.log", ios::out|ios::app);SYSTEMTIME st = { 0 };GetLocalTime(&st);file << st.wYear << "-" << st.wMonth << "-" << st.wDay << " " << st.wHour << ":" << st.wMinute << ":" << st.wSecond << endl;for (int j = 0; j < i; j++) {file << word[j] << " " << cha[j] << " " << trans[j] << endl;}file.close();}else {cout << "生词表为空,先加入生词再背诵吧" << endl;return;}
}
void Recite::update_log() {cout << "2018-9-4 背词宝version1.0 诞生" << endl;cout << "2018-9-4 背词宝version1.1 诞生" << endl;cout << "新增的内容:" << endl;cout << "1.新增背词功能,在背诵完生词后生词会自动从生词表删除,并且添加到背词历史中" << endl;cout << "2.新增历史生词查询功能,可以根据当天的年与日查询背诵完的生词" << endl;
}
void Recite::run() {cout << "------------------------------" << endl;cout << "|欢迎使用背词宝version1.1    |" << endl;cout << "|1.添加生词                  |" << endl;cout << "|2.显示所有生词              |" << endl;cout << "|3.精确查词                  |" << endl;cout << "|4.删除生词表中的词          |" << endl;cout << "|5.背生词                    |" << endl;cout << "|6.查询背诵历史              |" << endl;cout << "|7.更新日志                  |" << endl;cout << "|8.退出                      |" << endl;cout << "------------------------------" << endl;cout << "请输入需要服务的序号:";int i;cin >> i;while (i != 8) {switch (i) {case 1:system("cls");insert_word();break;case 2:system("cls");query_all();break;case 3:system("cls");query_exact();break;case 4:system("cls");delete_word();break;case 5:system("cls");recite_word();break;case 6:system("cls");query_by_time();break;case 7:system("cls");update_log();break;case 8:break;default:cout << "对应数字的服务不存在,请重新输入" << endl;break;}system("pause");system("cls");cout << "------------------------------" << endl;cout << "|欢迎使用背词宝version1.1    |" << endl;cout << "|1.添加生词                  |" << endl;cout << "|2.显示所有生词              |" << endl;cout << "|3.精确查词                  |" << endl;cout << "|4.删除生词表中的词          |" << endl;cout << "|5.背生词                    |" << endl;cout << "|6.查询背诵历史              |" << endl;cout << "|7.更新日志                  |" << endl;cout << "|8.退出                      |" << endl;cout << "------------------------------" << endl;cout << "请输入需要服务的序号:";cin >> i;}
}int main()
{Recite r;r.run();return 0;
}

主要应用界面:

查词界面:

按照时间查询历史记录:

背单词算法描述:

1.将所有的 生词本.txt 中的单词都存放如内存中

2.初始化一个数组记录这个单词应该被拼写多少次,初始次数都是1,也就是一开始就会的单词没有必要再重复背诵了

3.当拼写对了的时候次数减一,每次循环的时候如果拼写次数为1则跳过这个单词的拼写

4.拼写错了则拼写次数加一

5.每当拼写对一个单词,就将记录拼写正确单词数量的变量加一,如果拼写正确单词的数量和生词本中生词数量一样的话就退出循环,否则继续循环,直到所有单词都拼写对并且需要拼写的次数为0

6.所有单词背完后要清空 生词本.txt 中的内容,然后在 背词历史.log 中写入当前时间,并将内存中的生词记录全部写入 背词历史.log 中

按照时间查询历史记录算法描述:

读取 背词历史.log 文件,如果查询到了时间,就开始读取后面记录的生词,知道下一次读取到时间;读取到时间之后比对是否和输入的时间相同,如果相同的话,那么就要继续读取时间后面的生词。

全部读取完之后把内存中的生词存入 历史记录.txt ,然后查询所有 历史记录.txt 中的单词

这次项目做完后的收获:

1.了解了 fstream 类对文件的基本操作

2.了解到了一个很好用的类 istringstream ,可以将string中的单词一个一个提取出来,非常实用

3.了解了通过windows api 获取时间的方法

4.第一次用c++做出了稍微实用一点的应用,增加了信心

仍然存在的问题:

在进行插入等有些操作的时候记录的时间往往不准,经常没有1s就出来了,但是它显示的却是远大于1s的数。

PS:查询操作显示的时间比较正常

c++自制背单词应用相关推荐

  1. 用python自制背单词程序_c++自制背单词应用

    文件结构: 背词历史.log 用来存放背过的单词,存放的格式是 年-月-日 时:分:秒 单词 词性 中文解释 生词本.txt 用来存放当下要背诵的单词列表,格式是 单词 词性 中文解释 历史记录.tx ...

  2. 自制的简易EXCEL背单词模版

    自制的简易EXCEL背单词模版 由于感受到了英语应用压力,最近我又重新开始背单词.看到网上Excel背单词法,我觉得很有趣,但是用起来很麻烦.于是我突(bu)发(wu)奇(zheng)想(ye)地准备 ...

  3. 自制安卓背单词小软件(1)

    一直喜欢看些英语方面的东西,提高词汇量自然是必不可少的.但是每天捧着单词书实在是不大方便,倒不如直接在手机上装个背单词的小软件,有空的时候就背几个,并且手机软件功能又多,又有艾宾浩斯什么这那的东西,相 ...

  4. 用python自制一款背单词程序

    需求分析 在使用电脑上网冲浪,打游戏,看电影时,经常会在各个地方夹杂着一些看不懂的英文单词,为了在日常玩电脑的时候,也能利用碎片时间积累英文单词,因此自制了一款简易的背单词程序.功能如下: 1.平时见 ...

  5. 那些相见恨晚的背单词方法,快来收藏学习!

    考研英语词汇背诵是打下英语坚实的基础的第一步,也是考研英语获得高分的重要保障,针对大家英语单词背了又忘这个问题,小编给大家整理了一些考研英语词汇记忆的方法,希望能帮助大家. 1.大量但快速地背 考研要 ...

  6. 【青少年编程】黄羽恒:我要背单词

    「青少年编程竞赛交流群」已成立(适合6至18周岁的青少年),公众号后台回复[Scratch]或[Python],即可进入.如果加入了之前的社群不需要重复加入. 微信后台回复"资料下载&quo ...

  7. 不用“背”单词,一个方法从普通二本到哥伦比亚大学:我是如何做到的?

    "学英语太难.太费劲了."我听过无数人这么说. 然而,我的学员们,却都只用了短短3-6个月时间,就以惊人速度提高了英语: 小磊:勉强踩着2本线上了大学,四级考了3次没过,毕业时却拿 ...

  8. 别光顾着背单词了,每天花18分钟做这件事,英语水平暴增!

    在知乎上看过一个问题:在当今社会,英语还重要吗? 点赞第一的回答是-- 英语可以差,但你的口语一定要好! 你记住了1万个单词.将语法书倒背如流.英语成绩名列前茅.英语证书一大摞. 但你的口语差,看见老 ...

  9. 少壮不努力,老大背单词

    少壮不努力,老大背单词 你必须非常努力,才可以看起来毫不费力 uncouth (adj.) 粗鲁的,不文明的 I am embarrassed by your uncouth manners epit ...

最新文章

  1. Dockerfile中npm中Error: could not get uid/gid问题的解决方法
  2. adb install 和adb uninstall
  3. 一劳永逸:域名支持通配符,ASP.NET Core中配置CORS
  4. Day03『NLP打卡营』实践课3:使用预训练模型实现快递单信息抽取
  5. Map3D中获取地图中心及Zoom到新的中心点
  6. C++语言编程软件推荐及下载教程
  7. php模板多图上传插件,PHP百度diyUpload多图上传插件实例
  8. 【秋招总结】分享我的社招java岗经验,以及我的大厂offer比较(京东网易滴滴,天津农行软开)
  9. SMB/CIFS--NetBOIS/Browser/NBNS 协议
  10. dom影像图形成数字地形图_基于MapMatrix的数字正射影像图制作
  11. Linux下MinDoc安装使用
  12. JavaSE重点之集合、IO、多线程
  13. Centos7环境启动mongod报polkit服务启动失败
  14. 185电缆的接法图解_185平方的电缆火线,零线,接地线多少平方的
  15. Android Studio 生成二维码、生成带logo的二维码
  16. HMACSHA1 加密算法
  17. 长度单位换算python_长度单位换算表-在线长度单位转换器
  18. Lync 2013兼容性
  19. keil (v5.24.2.0)、protues8.9、STM32F103R6,点灯试验仿真protues报错unclocked peripheral at 0x40010800
  20. 《Turbo iso: towards ultrafast and robust subgraph isomorphism search in large graph databases》读后笔记

热门文章

  1. swagger 接口参数顺序_Swagger2接口排序
  2. 32位Windows 7开启PAE可以识别4GB以上内存
  3. 乐1s 乐视X500_官方线刷包_救砖包_解账户锁
  4. TextView中ellipsize属性
  5. SP1026 FAVDICE - Favorite Dice[期望DP]
  6. 一天一个小知识:KT高阶函数
  7. 关于IAR Unable to open file错误的方法
  8. WWN,WWNN,WWPN三者的区别
  9. html中看板娘添加,使用插件添加看板娘
  10. Python:mechanize模拟浏览器行为