1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
#include "sendEnglish.h" Englishbook englishbook;
int randomNumber(int num) { srand((int)time(0)); return rand() % (num + 1); }
static void _split(const string& s, char delim, vector<string>& elems) { stringstream ss(s); string item;
while (getline(ss, item, delim)) { elems.push_back(item); } }
vector<string> split(const string& s, char delim) { vector<string> elems; _split(s, delim, elems); return elems; }
void test() { for (int i = 0; i <= englishbook.number; ++i) { cout << "-----------------------------------" << endl; cout << i << " : " << englishbook.english[i].english << " " << englishbook.english[i].chinese << endl; cout << "-----------------------------------" << endl; } }
void sendEnglish::show() { string s; int num = 0; cout << "*************************" << endl; cout << "* 请输入单词以及该单词的翻译 *" << endl; cout << "* 按回车即可保存 *" << endl; cout << "* 输入0按回车即可结束 *" << endl; cout << "*************************" << endl; while (true) { cout << "请输入您要输入的英语单词:"; cin >> s; if (s == "0") { break; } englishbook.number = num; englishbook.english[num].english = s; cout << "请输入该英语的翻译:"; cin >> englishbook.english[num].chinese; cout << "------------------------" << endl; num++; send(); } test(); cout << "以上是您录入的新单词,已保存!!" << endl; }
void sendEnglish::send() {
ofstream ofs; ofs.open("english.csv", ios::out | ios::app); if (!ofs.is_open()) { cout << "文件没有打开!" << endl; } ifstream ifs("english.csv", ios::in); if (!ifs.is_open()) { this->fileIsEmpty = true; cout << "文件不存在" << endl; ifs.close(); return; } int s = 0; string data; while (getline(ifs,data)) { s++; } ifs.close();
for (int i = 0; i <= englishbook.number; i++) { ofs << s+i << "," << englishbook.english[i].english << "," << englishbook.english[i].chinese << endl; } ofs.close();
}
void sendEnglish::rec(){ int num; ifstream ifs("english.csv", ios::in); if (!ifs.is_open()) { this->fileIsEmpty = true; cout << "文件不存在" << endl; ifs.close(); return; } string buff; int s = 0; while (getline(ifs, buff)) { vector<string> data = split(buff, ','); englishbook.english[s].english = data[1]; englishbook.english[s].chinese = data[2]; s++; } englishbook.number = s-1; cout << "如果想要退出请按0." << endl; while (true) { string chinese; num = randomNumber(englishbook.number); cout << "请写出" << englishbook.english[num].english << "的翻译:"; cin >> chinese; if (chinese == "0") { break;
} else if (englishbook.english[num].chinese == chinese) { cout << "回答正确!!" << endl; } else if (englishbook.english[num].chinese != chinese) { cout << "回答错误!!正确答案是:" << englishbook.english[num].chinese << endl; } } }
void sendEnglish::book() { ifstream ifs("english.csv", ios::in); if (!ifs.is_open()) { this->fileIsEmpty = true; cout << "文件不存在" << endl; ifs.close(); return; } string buff; int s = 0; while (getline(ifs, buff)) { vector<string> data = split(buff, ','); englishbook.english[s].english = data[1]; englishbook.english[s].chinese = data[2]; s++; } englishbook.number = s - 1; cout << "已录入单词如下:" << endl; test();
}
void sendEnglish::del_book() { int num; cout << "************************" << endl; cout << "您确定要清空单词本吗?" << endl; cout << "是,请输入1" << endl; cout << "否,请输入0" << endl; cin >> num; if (num == 0) { return; } else if(num == 1) { remove("english.csv"); } }
|