当前位置:   article > 正文

背单词工具(C++)_单词本c++程序

单词本c++程序

功能分析

  1. 生词本管理
    • 创建生词本文件:在构造函数中创建了“生词本.txt”“背词历史.log”“历史记录.txt”三个文件。
    • 添加单词:用户可以输入单词、词性和解释,将其添加到生词本中。
    • 查询所有单词:展示生词本中所有的单词、词性和翻译。
    • 精确查词:用户可以选择按照单词、词性或中文解释进行查词,并显示查询结果。
    • 删除单词:根据用户输入删除生词本中的特定单词。
  2. 背词功能
    • 背生词:从生词本中读取单词进行背诵,背诵完成后将生词从生词本中删除,并将相关信息添加到背词历史中。
  3. 历史记录查询
    • 根据时间查历史记录:用户输入年月日,查询该日期的背词历史记录,并将其保存到历史记录文件中。
    • 查询历史背词:展示历史记录文件中的内容。
  4. 其他功能
    • 更新日志:输出新增功能的说明。
    • 总的服务界面:提供菜单界面,用户根据序号选择相应的服务。

详细代码

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <sstream>
  5. #include <iomanip> //输出控制头文件1
  6. #include <time.h>
  7. #include <windows.h>
  8. using namespace std;
  9. class Recite {
  10. fstream file;
  11. fstream file1;
  12. public:
  13. Recite(); //创建生词本文件
  14. void insert_word(); //添加单词
  15. void query_all(); //查询所有单词
  16. void query_by_time();//根据时间查历史记录
  17. void query_history();//查询历史背词
  18. void query_exact(); //精确查词
  19. void delete_word(); //删除单词
  20. int get_num(); //返回生词本中单词的数量
  21. void recite_word(); //背生词
  22. void update_log(); //更新日志
  23. void run(); //总的服务界面
  24. };
  25. Recite::Recite() {
  26. file.open("生词本.txt");
  27. file.close();
  28. file.open("背词历史.log");
  29. file.close();
  30. file.open("历史记录.txt");
  31. file.close();
  32. }
  33. void Recite::insert_word() {
  34. clock_t startTime, endTime;
  35. file.open("生词本.txt", ios::out | ios::app); //在文件末尾处写入
  36. if (file.is_open() == 1) {
  37. //打开成功的话
  38. startTime = clock();
  39. char word[20], cha[5], trans[20]; //单词 词性 解释
  40. cout << "请输入要写入错题本的单词:";
  41. cin >> word;
  42. cout << "请输入单词的词性:";
  43. cin >> cha;
  44. cout << "请输入单词的解释:";
  45. cin >> trans;
  46. file << word << " " << cha << " " << trans << endl; //1就代表没有被删除的单词
  47. file.close();
  48. endTime = clock();
  49. cout << "写入成功,总共用时" << (double) (endTime - startTime) / CLOCKS_PER_SEC << " s" << endl;
  50. } else {
  51. cout << "打开文件失败" << endl;
  52. system("pause");
  53. }
  54. }
  55. void Recite::query_all() {
  56. clock_t startTime, endTime;
  57. startTime = clock();
  58. char buffer[100];
  59. int number = 0; //记录记录的条数
  60. cout << " --------+----+---------" << endl;
  61. cout << "|" << setw(8) << "单词";
  62. cout << "|" << setw(4) << "词性";
  63. cout << "|" << setw(8) << "翻译";
  64. cout << "|" << endl;
  65. cout << " --------+----+---------" << endl;
  66. file.open("生词本.txt", ios::in | ios::app);
  67. while (!file.eof()) {
  68. file.getline(buffer, 100);
  69. istringstream is(buffer);
  70. string s1, s2, s3, s4;
  71. is >> s1 >> s2 >> s3 >> s4;
  72. if (s1 != "" && s2 != "" && s3 != "") {
  73. number++;
  74. cout << "|" << setw(8) << s1;
  75. cout << "|" << setw(4) << s2;
  76. cout << "|" << setw(8) << s3;
  77. cout << "|" << endl;
  78. cout << " --------+----+---------" << endl;
  79. }
  80. }
  81. endTime = clock();
  82. cout << "总共有" << number << "条记录,总共用时" << (double) (endTime - startTime) / CLOCKS_PER_SEC << " s" << endl;
  83. file.close();
  84. }
  85. void Recite::query_by_time() {
  86. file.open("背词历史.log", ios::in | ios::out | ios::app);
  87. if (file.is_open()) {
  88. string time;
  89. cout << "请输入要查询的历史记录的年月日,格式为年-月-日:";
  90. cin >> time;
  91. string word[100], cha[100], trans[100];
  92. int i = 0;
  93. char buffer[100];
  94. while (!file.eof()) {
  95. file.getline(buffer, 100);
  96. istringstream is(buffer);
  97. string t1, t2;
  98. is >> t1 >> t2;
  99. if (t1 == time) {
  100. while (!file.eof()) {
  101. file.getline(buffer, 100);
  102. istringstream input(buffer);
  103. string s1, s2, s3;
  104. input >> s1 >> s2 >> s3;
  105. if (s1 != "" && s2 != "" && s3 != "") {
  106. word[i] = s1;
  107. cha[i] = s2;
  108. trans[i] = s3;
  109. i++;
  110. } else {
  111. if (s1 == time)
  112. continue;
  113. else
  114. break;
  115. }
  116. }
  117. }
  118. }
  119. file.close();
  120. file.open("历史记录.txt", ios::in | ios::out | ios::trunc);
  121. for (int j = 0; j < i; j++)
  122. file << word[j] << " " << cha[j] << " " << trans[j] << endl;
  123. file.close();
  124. query_history();
  125. } else {
  126. cout << "文件打开失败" << endl;
  127. return;
  128. }
  129. }
  130. void Recite::query_history() {
  131. clock_t startTime, endTime;
  132. startTime = clock();
  133. char buffer[100];
  134. int number = 0; //记录记录的条数
  135. cout << " --------+----+---------" << endl;
  136. cout << "|" << setw(8) << "单词";
  137. cout << "|" << setw(4) << "词性";
  138. cout << "|" << setw(8) << "翻译";
  139. cout << "|" << endl;
  140. cout << " --------+----+---------" << endl;
  141. file.open("历史记录.txt", ios::in | ios::app);
  142. while (!file.eof()) {
  143. file.getline(buffer, 100);
  144. istringstream is(buffer);
  145. string s1, s2, s3, s4;
  146. is >> s1 >> s2 >> s3 >> s4;
  147. if (s1 != "" && s2 != "" && s3 != "") {
  148. number++;
  149. cout << "|" << setw(8) << s1;
  150. cout << "|" << setw(4) << s2;
  151. cout << "|" << setw(8) << s3;
  152. cout << "|" << endl;
  153. cout << " --------+----+---------" << endl;
  154. }
  155. }
  156. endTime = clock();
  157. cout << "总共有" << number << "条记录,总共用时" << (double) (endTime - startTime) / CLOCKS_PER_SEC << " s" << endl;
  158. file.close();
  159. }
  160. void Recite::query_exact() {
  161. clock_t startTime, endTime;
  162. char buffer[100];
  163. int i, number = 0;
  164. cout << "1.按照单词查词" << endl;
  165. cout << "2.按照词性查词" << endl;
  166. cout << "3.按照中文解释查词" << endl;
  167. cout << "请输入需要确定查词方式:";
  168. cin >> i;
  169. startTime = clock();
  170. string word;
  171. cout << "请输入要查的单词:";
  172. cin >> word;
  173. cout << " --------+----+---------" << endl;
  174. cout << "|" << setw(8) << "单词";
  175. cout << "|" << setw(4) << "词性";
  176. cout << "|" << setw(8) << "翻译";
  177. cout << "|" << endl;
  178. cout << " --------+----+---------" << endl;
  179. file.open("生词本.txt", ios::in);
  180. switch (i) {
  181. case 1:
  182. while (!file.eof()) {
  183. file.getline(buffer, 100);
  184. istringstream is(buffer);
  185. string s1, s2, s3;
  186. is >> s1 >> s2 >> s3;
  187. if (s1 == word) {
  188. number++;
  189. cout << "|" << setw(8) << s1;
  190. cout << "|" << setw(4) << s2;
  191. cout << "|" << setw(8) << s3;
  192. cout << "|" << endl;
  193. cout << " --------+----+---------" << endl;
  194. }
  195. }
  196. endTime = clock();
  197. cout << "查询成功,一共有" << number << "条记录,用时:" << (double) (endTime - startTime) / CLOCKS_PER_SEC
  198. << " s" << endl;
  199. file.close();
  200. break;
  201. case 2:
  202. while (!file.eof()) {
  203. file.getline(buffer, 100);
  204. istringstream is(buffer);
  205. string s1, s2, s3;
  206. is >> s1 >> s2 >> s3;
  207. if (s2 == word) {
  208. number++;
  209. cout << "|" << setw(8) << s1;
  210. cout << "|" << setw(4) << s2;
  211. cout << "|" << setw(8) << s3;
  212. cout << "|" << endl;
  213. cout << " --------+----+---------" << endl;
  214. }
  215. }
  216. endTime = clock();
  217. cout << "查询成功,一共有" << number << "条记录,用时:" << (double) (endTime - startTime) / CLOCKS_PER_SEC
  218. << " s" << endl;
  219. file.close();
  220. break;
  221. case 3:
  222. while (!file.eof()) {
  223. file.getline(buffer, 100);
  224. istringstream is(buffer);
  225. string s1, s2, s3;
  226. is >> s1 >> s2 >> s3;
  227. if (s3 == word) {
  228. number++;
  229. cout << "|" << setw(8) << s1;
  230. cout << "|" << setw(4) << s2;
  231. cout << "|" << setw(8) << s3;
  232. cout << "|" << endl;
  233. cout << " --------+----+---------" << endl;
  234. }
  235. }
  236. endTime = clock();
  237. cout << "查询成功,一共有" << number << "条记录,用时:" << (double) (endTime - startTime) / CLOCKS_PER_SEC
  238. << " s" << endl;
  239. file.close();
  240. break;
  241. default:
  242. //默认用单词查询
  243. while (!file.eof()) {
  244. file.getline(buffer, 100);
  245. istringstream is(buffer);
  246. string s1, s2, s3;
  247. is >> s1 >> s2 >> s3;
  248. if (s1 == word) {
  249. number++;
  250. cout << "|" << setw(8) << s1;
  251. cout << "|" << setw(4) << s2;
  252. cout << "|" << setw(8) << s3;
  253. cout << "|" << endl;
  254. cout << " --------+----+---------" << endl;
  255. }
  256. }
  257. endTime = clock();
  258. cout << "查询成功,一共有" << number << "条记录,用时:" << (double) (endTime - startTime) / CLOCKS_PER_SEC
  259. << " s" << endl;
  260. file.close();
  261. break;
  262. }
  263. }
  264. int Recite::get_num() {
  265. file1.open("生词本.txt", ios::in); //以只读方式打开生词本
  266. char buffer[100];
  267. int number = 0;
  268. while (!file.eof()) {
  269. file.getline(buffer, 100);
  270. istringstream is(buffer);
  271. string s1, s2, s3;
  272. is >> s1 >> s2 >> s3;
  273. if (s1 != " " && s2 != " " && s3 != " ")
  274. number++;
  275. }
  276. file1.close();
  277. return number;
  278. }
  279. void Recite::delete_word() {
  280. query_all(); //显示所有的记录
  281. string str;
  282. clock_t startTime, endTime;
  283. cout << "请输入想要删除的单词:";
  284. cin >> str;
  285. startTime = clock();
  286. file.open("生词本.txt", ios::in);
  287. char buffer[100];
  288. string str1[100], str2[100], str3[100];
  289. int i = 0;
  290. while (!file.eof()) {
  291. file.getline(buffer, 100);
  292. istringstream is(buffer);
  293. string s1, s2, s3;
  294. is >> s1 >> s2 >> s3;
  295. if (s1 != str && s1 != "" && s2 != "" && s3 != "") {
  296. str1[i] = s1;
  297. str2[i] = s2;
  298. str3[i] = s3;
  299. i++;
  300. }
  301. }
  302. file.close();
  303. file.open("生词本.txt", ios::out | ios::trunc); //以截断方式打开文件,清空所有内容
  304. for (int j = 0; j < i; j++) {
  305. file << str1[j] << " " << str2[j] << " " << str3[j] << " " << endl;
  306. }
  307. file.close();
  308. endTime = clock();
  309. cout << "删除成功,用时:" << (double) (endTime - startTime) / CLOCKS_PER_SEC << " s" << endl;
  310. }
  311. void Recite::recite_word() {
  312. file.open("生词本.txt", ios::in | ios::out);
  313. if (file.is_open() == 1) {
  314. clock_t startTime, endTime;
  315. //遍历后将单词拷贝至内存
  316. string word[100], cha[100], trans[100], str;
  317. char buffer[100];
  318. int i = 0;
  319. while (!file.eof()) {
  320. file.getline(buffer, 100);
  321. istringstream is(buffer);
  322. string s1, s2, s3;
  323. is >> s1 >> s2 >> s3;
  324. if (s1 != "" && s2 != "" && s3 != "") {
  325. word[i] = s1;
  326. cha[i] = s2;
  327. trans[i] = s3;
  328. i++;
  329. }
  330. }
  331. int number = i;
  332. cout << "本次需要复习的单词数量是:" << number << endl;
  333. system("pause");
  334. system("cls");
  335. int num_of_recite[100]; //记录需要背诵单词的次数,一开始都是1
  336. for (int k = 0; k < 100; k++)
  337. num_of_recite[k] = 1;
  338. int sucessful = 0; //判断单词是否背完了,背完了就是1,没有背完就是0
  339. if (number == 0)
  340. sucessful = 1;
  341. int num = 0;
  342. startTime = clock();
  343. while (sucessful == 0) {
  344. for (int j = 0; j < i; j++) {
  345. if (num_of_recite[j] != 0) {
  346. cout << "中文意思:" << trans[j] << " " << cha[j] << endl;
  347. cout << "请输入单词:";
  348. cin >> str;
  349. if (str == word[j]) {
  350. cout << "正确!";
  351. num_of_recite[j]--;
  352. system("pause");
  353. system("cls");
  354. num++;
  355. if (num == number)
  356. sucessful = 1;
  357. } else {
  358. cout << "错误,正确答案是:" << word[j];
  359. num_of_recite[j]++;
  360. system("pause");
  361. system("cls");
  362. }
  363. }
  364. }
  365. }
  366. endTime = clock();
  367. cout << "恭喜你背完啦~~,用时:" << (double) (endTime - startTime) / CLOCKS_PER_SEC << " s" << endl;
  368. //背完单词后清空单词表
  369. file.close();
  370. file.open("生词本.txt", ios::out | ios::trunc);
  371. file.close();
  372. //然后写入日志
  373. file.open("背词历史.log", ios::out | ios::app);
  374. SYSTEMTIME st = {0};
  375. GetLocalTime(&st);
  376. file << st.wYear << "-" << st.wMonth << "-" << st.wDay << " " << st.wHour << ":" << st.wMinute << ":"
  377. << st.wSecond << endl;
  378. for (int j = 0; j < i; j++) {
  379. file << word[j] << " " << cha[j] << " " << trans[j] << endl;
  380. }
  381. file.close();
  382. } else {
  383. cout << "生词表为空,先加入生词再背诵吧" << endl;
  384. return;
  385. }
  386. }
  387. void Recite::update_log() {
  388. cout << "新增的内容:" << endl;
  389. cout << "1.新增背词功能,在背诵完生词后生词会自动从生词表删除,并且添加到背词历史中" << endl;
  390. cout << "2.新增历史生词查询功能,可以根据当天的年与日查询背诵完的生词" << endl;
  391. }
  392. void Recite::run() {
  393. cout << "------------------------------" << endl;
  394. cout << "|欢迎使用大家一起背单词 |" << endl;
  395. cout << "|1.添加生词 |" << endl;
  396. cout << "|2.显示所有生词 |" << endl;
  397. cout << "|3.精确查词 |" << endl;
  398. cout << "|4.删除生词表中的词 |" << endl;
  399. cout << "|5.背生词 |" << endl;
  400. cout << "|6.查询背诵历史 |" << endl;
  401. cout << "|7.更新日志 |" << endl;
  402. cout << "|8.退出 |" << endl;
  403. cout << "------------------------------" << endl;
  404. cout << "请输入需要服务的序号:";
  405. int i;
  406. cin >> i;
  407. while (i != 8) {
  408. switch (i) {
  409. case 1:
  410. system("cls");
  411. insert_word();
  412. break;
  413. case 2:
  414. system("cls");
  415. query_all();
  416. break;
  417. case 3:
  418. system("cls");
  419. query_exact();
  420. break;
  421. case 4:
  422. system("cls");
  423. delete_word();
  424. break;
  425. case 5:
  426. system("cls");
  427. recite_word();
  428. break;
  429. case 6:
  430. system("cls");
  431. query_by_time();
  432. break;
  433. case 7:
  434. system("cls");
  435. update_log();
  436. break;
  437. case 8:
  438. break;
  439. default:
  440. cout << "对应数字的服务不存在,请重新输入" << endl;
  441. break;
  442. }
  443. system("pause");
  444. system("cls");
  445. cout << "------------------------------" << endl;
  446. cout << "|欢迎使用背词宝version1.1 |" << endl;
  447. cout << "|1.添加生词 |" << endl;
  448. cout << "|2.显示所有生词 |" << endl;
  449. cout << "|3.精确查词 |" << endl;
  450. cout << "|4.删除生词表中的词 |" << endl;
  451. cout << "|5.背生词 |" << endl;
  452. cout << "|6.查询背诵历史 |" << endl;
  453. cout << "|7.更新日志 |" << endl;
  454. cout << "|8.退出 |" << endl;
  455. cout << "------------------------------" << endl;
  456. cout << "请输入需要服务的序号:";
  457. cin >> i;
  458. }
  459. }
  460. int main() {
  461. Recite r;
  462. r.run();
  463. return 0;
  464. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/990750
推荐阅读
相关标签
  

闽ICP备14008679号