用python自制背单词程序_c++自制背单词应用
文件結構:
背詞歷史.log 用來存放背過的單詞,存放的格式是
年-月-日 時:分:秒
單詞 詞性 中文解釋
生詞本.txt 用來存放當下要背誦的單詞列表,格式是
單詞 詞性 中文解釋
歷史記錄.txt 用來當做按照時間查詢生詞的緩存,記錄最后一個詞查詢的結果
其格式與 生詞本.txt 一樣
主要功能:
更新日志:2018-9-4 背詞寶version1.0 誕生2018-9-4 背詞寶version1.1 誕生新增的內容:1.新增背詞功能,在背誦完生詞后生詞會自動從生詞表刪除,并且添加到背詞歷史中2.新增歷史生詞查詢功能,可以根據當天的年與日查詢背誦完的生詞
代碼:#include "stdafx.h"#include #include #include #include #include ? ? //輸出控制頭文件#include #include 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<> 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 << "條記錄,總共用時"<> 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] << " "<> 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:查詢操作顯示的時間比較正常
————————————————版權聲明:本文為CSDN博主「haohulala」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。原文鏈接:https://blog.csdn.net/haohulala/article/details/82418187
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的用python自制背单词程序_c++自制背单词应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 极米Z4X投影仪怎么退出音响模式?
- 下一篇: “衰恨满秋容”下一句是什么