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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
| #include"TcpSocket.h" #include <fstream> #include <unordered_map> #include <vector>
void Connect(TcpSocket& client) { int ret = client.Connect_Host("127.0.0.1", 10000); if (ret == -1) perror("client connect to host"); return; }
class Login { public: std::string m_name; Login(const std::string& filename) : filename_(filename) { LoadDataFromFile(); }
bool Authenticate() { std::string username; std::cout << "please input your name: "; std::cin >> username; m_name = username; std::string password; std::cout << "please input your password: "; std::cin >> password;
if (accounts_.find(username) != accounts_.end() && accounts_[username] == password) { std::cout << "Authentication successful for " << username << std::endl; return true; } else { std::cout << "Authentication failed for " << username << std::endl; return false; } }
void AddAccount(const std::string& username, const std::string& password) { accounts_[username] = password; SaveDataToFile(); std::cout << "Account added: " << username << std::endl; }
bool DeleteAccount(const std::string& username) { auto it = accounts_.find(username); if (it != accounts_.end()) { accounts_.erase(it); SaveDataToFile(); std::cout << "Account deleted: " << username << std::endl; return true; } else { std::cout << "Account not found: " << username << std::endl; return false; } }
bool ModifyPassword(const std::string& username, const std::string& newPassword) { auto it = accounts_.find(username); if (it != accounts_.end()) { it->second = newPassword; SaveDataToFile(); std::cout << "Password for account " << username << " modified." << std::endl; return true; } else { std::cout << "Account not found: " << username << std::endl; return false; } }
private: std::string filename_; std::unordered_map<std::string, std::string> accounts_;
void LoadDataFromFile() { std::ifstream file(filename_, std::ios::binary); if (file.is_open()) { while (true) { std::string username, password; if (ReadStringFromBinaryFile(file, username) && ReadStringFromBinaryFile(file, password)) { accounts_[username] = password; } else { break; } } file.close(); } }
void SaveDataToFile() { std::ofstream file(filename_, std::ios::binary); if (file.is_open()) { for (const auto& entry : accounts_) { WriteStringToBinaryFile(file, entry.first); WriteStringToBinaryFile(file, entry.second); } file.close(); } }
bool ReadStringFromBinaryFile(std::ifstream& file, std::string& str) { uint32_t strLength = 0; if (file.read(reinterpret_cast<char*>(&strLength), sizeof(uint32_t))) { str.resize(strLength); if (file.read(&str[0], strLength)) { return true; } } return false; }
void WriteStringToBinaryFile(std::ofstream& file, const std::string& str) { uint32_t strLength = static_cast<uint32_t>(str.size()); file.write(reinterpret_cast<const char*>(&strLength), sizeof(uint32_t)); file.write(str.c_str(), strLength); } };
enum class chose { add, del, select, modify }; int Chose_Function() { std::cout << "******************************************************" << std::endl; std::cout << "************ input num to use ******************" << std::endl; std::cout << "***** 0.add ************ 1.delete ********" << std::endl; std::cout << "***** 2.select ************ 3.modify ********" << std::endl; std::cout << "******************************************************" << std::endl; int input; std::cin >> input; return input; }
void Add_Action(TcpSocket& client, Login& user) { if (user.m_name != "542213430101") { std::cout << "Apologize, you do not have the authority" << std::endl; return; } std::string sql = "INSERT INTO stu_msg (num, name, gender, profession, class, score) VALUES ("; std::string input; std::string num; std::vector<std::string> inputValues; std::cout << "Enter the following information in order (num, name, gender, profession, class, score):" << std::endl;
for (int i = 0; i < 6; i++) { std::cin >> input; inputValues.push_back(input); if (i == 0) num = input; } for (const std::string& value : inputValues) sql += "'" + value + "',";
sql = sql.substr(0, sql.length() - 1); sql += ")";
client.Send_Msg(sql);
user.AddAccount(num, num);
}
void Del_Action(TcpSocket& client, Login& user) { if (user.m_name != "542213430101") { std::cout << "Apologize, you do not have the authority" << std::endl; return; }
std::cout << "input the student number to delete: "; std::string num; falg: std::cin >> num; if (num == "542213430101") { std::cout << "you can't delete yourself, try again" << std::endl; goto falg; } std::string sql("delete from stu_msg where num = "); client.Send_Msg(sql + num);
user.DeleteAccount(num); }
void Sel_Action(TcpSocket& client, Login& user) { flag: std::cout << "select your find way" << std::endl << "1. select all" << std::endl << "2. select by more ways" << std::endl; int input; std::cin >> input; if(input != 1 && input != 2) { std::cout << "input error,try again" <<std::endl; goto flag; }
std::string sql("select * from stu_msg "); if (input == 1) { client.Send_Msg(sql); } else { std::cout<<"chose your select way by ** write ** word"<<std::endl; std::string selectway; std::cout<<"num name gender profession class score"<<std::endl; std::cin >> selectway;
std::string message; std::cout << "input select message"<<std::endl; std::cin >> message;
std::string sqlsend = sql + "where " + selectway + "= '" + message + "'"; std::cout << sqlsend<<std::endl; client.Send_Msg(sqlsend); } std::cout << client.Recv_Msg() << std::endl; return; }
void Mod_Action(TcpSocket& client, Login& user) { flag: std::cout << "which you want to modify :" << std::endl; std::cout << "account/table msg (1/0) :" << std::endl; int chose; std::cin >> chose; if(chose != 1 && chose != 2) { std::cout << "input error,try again" <<std::endl; goto flag; }
if (chose) { std::string newpassword; std::cout << "input your new password" << std::endl; std::cin >> newpassword; user.ModifyPassword(user.m_name, newpassword); } else { std::string rootchosenum; if (user.m_name == "542213430101") { std::cout << "chose num to change" << std::endl; std::cin >> rootchosenum; } std::cout << "input word to change message" << std::endl; std::cout << "num name gender profession class score" << std::endl; std::string input; std::cin >> input; std::string sql("update stu_msg set "); input += "='"; sql += input; std::cout << "input message to change" << std::endl; std::cin >> input; input += "' where num = "; if (user.m_name == "542213430101") input += rootchosenum; else input += user.m_name; sql += input; client.Send_Msg(sql); } }
|