1 //#include "pch.h" 2 3 #include <stdio.h> 4 #include <iostream> 5 6 7 #include <string> 8 #include <cstring> 9 #define SizeMax 300 10 #define SizeMin 0 11 #define NotValid -1 12 13 enum ESubject 14 { 15 Math, //数学成绩 16 English, //英语成绩 17 Program, //程序成绩 18 Data, //数据结构成绩 19 20 }; 21 22 23 struct FNode 24 { 25 std::string Name; 26 int ID = -1; 27 float Score[4]; 28 float GetScore(ESubject Subject) 29 { 30 if (Subject > 3) 31 { 32 return -1.f; 33 } 34 return Score[Subject]; 35 } 36 void SetScore(ESubject Subject, float NewScore) 37 { 38 if (Subject > 3) 39 { 40 return; 41 } 42 Score[Subject] = NewScore; 43 } 44 }; 45 46 struct FNodeArray 47 { 48 private: 49 FNode Array[SizeMax]; 50 int Size = 0; 51 //增 52 //删 53 protected: 54 virtual bool RemoveArrayItem(int ID) 55 { 56 for (int i = 0; i < Size; i++) 57 { 58 if (Array[i].ID == ID) 59 { 60 Size = Size - 1; 61 if (Size < SizeMin) 62 { 63 return false; 64 } 65 for (; i < Size; i++) { 66 Array[i] = Array[i + 1]; 67 } 68 return true; 69 } 70 } 71 return false; 72 } 73 //改 74 virtual bool SetArrayItem(int ID, FNode NodeArray) 75 { 76 for (int i = 0; i < Size; i++) 77 { 78 if (Array[i].ID == ID) 79 { 80 Array[i] = NodeArray; 81 return true; 82 } 83 } 84 return false; 85 } 86 //查 87 virtual FNode FindArrayItem(int ID) 88 { 89 for (int i = 0; i < Size; i++) 90 { 91 if (Array[i].ID == ID) 92 { 93 return Array[i]; 94 } 95 } 96 97 return FNode(); 98 99 } 100 virtual bool AddArrayItem(FNode NodeArray) 101 { 102 if (FindArrayItem(NodeArray.ID).ID != NotValid) 103 { 104 return false; 105 } 106 107 if (Size < SizeMax) 108 { 109 Array[Size] = NodeArray; 110 Size = Size++; 111 return true; 112 } 113 return false; 114 } 115 116 117 public: 118 bool IsValid(int ID) 119 { 120 FNode Node_rn = FindArrayItem(ID); 121 122 if (Node_rn.ID == NotValid) 123 { 124 return false; 125 } 126 else 127 { 128 return true; 129 } 130 } 131 //查询成绩,修改成绩 132 float FindScore(int ID, ESubject Subject)// 查询成绩 133 { 134 if (IsValid(ID) == false) 135 { 136 return false; 137 } 138 139 FNode Node = FindStudentData(ID); 140 return Node.GetScore(Subject); 141 142 143 144 } 145 bool ChangeScore(int ID, ESubject Subject, float NewScore) 146 { 147 //修改成绩 148 149 if (IsValid(ID) == false) 150 { 151 return false; 152 } 153 154 FNode Node = FindStudentData(ID); 155 Node.SetScore(Subject, NewScore); 156 return SetArrayItem(ID, Node); 157 } 158 159 // 160 FNode FindStudentData(int ID) //查找学生信息 161 { 162 return FindArrayItem(ID); 163 } 164 bool ChangeStudentData(int ID, FNode NewData) //修改学生信息 165 { 166 return SetArrayItem(ID, NewData); 167 } 168 bool DeleteStudentData(int ID) //删除学生信息 169 { 170 return RemoveArrayItem(ID); 171 } 172 bool AddStudentData(FNode NewData) 173 { 174 return AddArrayItem(NewData); 175 } 176 int GetSize() 177 { 178 return Size; 179 } 180 181 //返回完全的一张数据表 182 //遍历器 183 FNode GetArrayNode(int Index) 184 { 185 if (Index < Size) 186 { 187 return Array[Index]; 188 } 189 else 190 { 191 return FNode(); 192 } 193 194 } 195 }; 196 //不使用单例了,涉及指针的部分我全部屏蔽掉了 197 198 199 200 201 FNodeArray NodeArray; 202 203 enum EInterfaceType 204 { 205 Main, 206 Add, 207 Del, 208 Change, 209 Find, 210 ChangeScore, 211 FindScore, 212 ShowAll, 213 Break, 214 215 }; 216 std::string CmdStr; 217 void ShowData(FNode ShowNode) 218 { 219 220 std::cout << " " << ShowNode.ID << " "; 221 std::cout << " " << ShowNode.Name << " "; 222 std::cout << " " << ShowNode.GetScore(ESubject::Math) << " "; 223 std::cout << " " << ShowNode.GetScore(ESubject::English) << " "; 224 std::cout << " " << ShowNode.GetScore(ESubject::Program) << " "; 225 std::cout << " " << ShowNode.GetScore(ESubject::Data) << " "; 226 std::cout << " " << std::endl; 227 228 229 230 } 231 232 EInterfaceType Interface(int Cmd) 233 { 234 235 236 if (Cmd == EInterfaceType::Main) //起始 237 { 238 239 std::cout << "图书管理系统" << std::endl; 240 std::cout << "------------------------------------" << std::endl; 241 std::cout << "------------------------------------" << std::endl; 242 std::cout << std::endl; 243 std::cout << "0.添加学员信息" << std::endl; 244 std::cout << "1.删除学员信息" << std::endl; 245 std::cout << "2.改变学员信息" << std::endl; 246 std::cout << "3.查询学员信息" << std::endl; 247 std::cout << "4.修改学员分数" << std::endl; 248 std::cout << "5.查询学员成绩" << std::endl; 249 std::cout << "6.显示所有信息" << std::endl; 250 251 std::cout << "------------------------------------" << std::endl; 252 std::cout << "请输入信息" << std::endl; 253 std::cin >> CmdStr; 254 int CmdBf = std::atoi(CmdStr.data()) + 1; 255 return (EInterfaceType)CmdBf; 256 } 257 else if (Cmd == EInterfaceType::Add) //添加学员信息 258 { 259 FNode NewNode; 260 std::cout << "图书管理系统 -> 学员信息管理界面" << std::endl; 261 std::cout << "------------------------------------" << std::endl; 262 std::cout << "------------------------------------" << std::endl; 263 std::cout << std::endl; 264 265 std::cout << "输入学号" << std::endl; 266 std::cin >> CmdStr; 267 NewNode.ID = std::atoi(CmdStr.data()); 268 269 std::cout << "输入学员名称" << std::endl; 270 std::cin >> CmdStr; 271 NewNode.Name = CmdStr; 272 273 std::cout << "输入学员数学成绩" << std::endl; 274 std::cin >> CmdStr; 275 NewNode.SetScore(ESubject::Math, std::atof(CmdStr.data())); 276 277 std::cout << "输入学员英语成绩" << std::endl; 278 std::cin >> CmdStr; 279 NewNode.SetScore(ESubject::English, std::atof(CmdStr.data())); 280 281 std::cout << "输入学员程序成绩" << std::endl; 282 std::cin >> CmdStr; 283 NewNode.SetScore(ESubject::Program, std::atof(CmdStr.data())); 284 285 std::cout << "输入学员数据结构成绩" << std::endl; 286 std::cin >> CmdStr; 287 NewNode.SetScore(ESubject::Data, std::atof(CmdStr.data())); 288 289 NodeArray.AddStudentData(NewNode); 290 std::cout << "录入成功,任意键结束" << std::endl; 291 std::cin >> CmdStr; 292 return EInterfaceType::Main; 293 } 294 else if (Cmd == EInterfaceType::Del) //删除学员信息 295 { 296 std::cout << "图书管理系统 -> 学员信息管理界面" << std::endl; 297 std::cout << "------------------------------------" << std::endl; 298 std::cout << "------------------------------------" << std::endl; 299 std::cout << std::endl; 300 std::cout << "输入学员学号" << std::endl; 301 std::cin >> CmdStr; 302 int CmdBf = std::atoi(CmdStr.data()); 303 304 305 if (NodeArray.IsValid(CmdBf)) 306 { 307 std::cout << "学员信息为" << std::endl; 308 FNode NewData = NodeArray.FindStudentData(CmdBf); 309 ShowData(NewData); 310 std::cout << "删除成功,任意键返回" << std::endl; 311 std::cin >> CmdStr; 312 return EInterfaceType::Main; // 返回该界面 313 } 314 else 315 { 316 std::cout << "学员不存在,任意键返回" << std::endl; 317 std::cin >> CmdStr; 318 return EInterfaceType::Del; // 返回该界面 319 } 320 } 321 else if (Cmd == EInterfaceType::Change) //改变学员信息 322 { 323 std::cout << "图书管理系统 -> 学员信息管理界面" << std::endl; 324 std::cout << "------------------------------------" << std::endl; 325 std::cout << "------------------------------------" << std::endl; 326 std::cout << std::endl; 327 std::cout << "输入学员学号" << std::endl; 328 std::cin >> CmdStr; 329 int CmdBf = std::atoi(CmdStr.data()); 330 331 332 if (NodeArray.IsValid(CmdBf)) 333 { 334 std::cout << "学员信息为" << std::endl; 335 FNode NewData = NodeArray.FindStudentData(CmdBf); 336 ShowData(NewData); 337 std::cout << "New ID" << std::endl; 338 std::cin >> CmdStr; 339 NewData.ID = std::atoi(CmdStr.data()); 340 std::cout << "New Name" << std::endl; 341 std::cin >> CmdStr; 342 NewData.Name = CmdStr; 343 std::cout << "New EnglishScore" << std::endl; 344 std::cin >> CmdStr; 345 NewData.SetScore(ESubject::English, std::atof(CmdStr.data())); 346 347 std::cout << "New MathScore" << std::endl; 348 std::cin >> CmdStr; 349 NewData.SetScore(ESubject::Math, std::atof(CmdStr.data())); 350 351 std::cout << "New ProgramScore" << std::endl; 352 std::cin >> CmdStr; 353 NewData.SetScore(ESubject::Program, std::atof(CmdStr.data())); 354 355 std::cout << "New DataScore" << std::endl; 356 std::cin >> CmdStr; 357 NewData.SetScore(ESubject::Data, std::atof(CmdStr.data())); 358 359 360 NodeArray.ChangeStudentData(CmdBf, NewData); 361 std::cout << "录入成功任意键退出" << std::endl; 362 std::cin >> CmdStr; 363 return EInterfaceType::Main; // 返回该界面 364 } 365 else 366 { 367 std::cout << "学员不存在,任意键返回" << std::endl; 368 std::cin >> CmdStr; 369 return EInterfaceType::Change; // 返回该界面 370 } 371 372 373 374 } 375 else if (Cmd == EInterfaceType::Find) //查询学员信息 376 { 377 std::cout << "图书管理系统 -> 学员信息管理界面" << std::endl; 378 std::cout << "------------------------------------" << std::endl; 379 std::cout << "------------------------------------" << std::endl; 380 std::cout << std::endl; 381 std::cout << "输入学员学号" << std::endl; 382 std::cin >> CmdStr; 383 int CmdBf = std::atoi(CmdStr.data()); 384 385 if (NodeArray.IsValid(CmdBf)) 386 { 387 std::cout << "学员信息为" << std::endl; 388 FNode NewData = NodeArray.FindStudentData(CmdBf); 389 ShowData(NewData); 390 391 std::cout << "任意键返回" << std::endl; 392 std::cin >> CmdStr; 393 return EInterfaceType::Main; // 返回该界面 394 } 395 else 396 { 397 std::cout << "学员不存在,任意键返回" << std::endl; 398 std::cin >> CmdStr; 399 return EInterfaceType::Change; // 返回该界面 400 } 401 } 402 else if (Cmd == EInterfaceType::ChangeScore) //修改学员分数 403 { 404 std::cout << "图书管理系统 -> 学员信息管理界面" << std::endl; 405 std::cout << "------------------------------------" << std::endl; 406 std::cout << "------------------------------------" << std::endl; 407 std::cout << std::endl; 408 std::cout << "输入学员学号" << std::endl; 409 std::cin >> CmdStr; 410 int CmdBf = std::atoi(CmdStr.data()); 411 412 413 if (NodeArray.IsValid(CmdBf)) 414 { 415 std::cout << "学员信息为" << std::endl; 416 FNode NewData = NodeArray.FindStudentData(CmdBf); 417 ShowData(NewData); 418 std::cout << "输入科目" << std::endl; 419 std::cout << "0,数学" << std::endl; 420 std::cout << "1,英语" << std::endl; 421 std::cout << "2,程序" << std::endl; 422 std::cout << "3,数据结构" << std::endl; 423 int ID = Cmd; 424 std::cin >> CmdStr; 425 CmdBf = std::atoi(CmdStr.data()); 426 427 std::cout << "输入新的分数" << std::endl; 428 std::cin >> CmdStr; 429 NodeArray.ChangeScore(ID, (ESubject)CmdBf, std::atof(CmdStr.data())); 430 431 std::cout << "修改成功,任意键返回" << std::endl; 432 std::cin >> CmdStr; 433 434 return EInterfaceType::Main; // 返回该界面 435 } 436 else 437 { 438 std::cout << "学员不存在,任意键返回" << std::endl; 439 std::cin >> CmdStr; 440 return EInterfaceType::ChangeScore; // 返回该界面 441 } 442 443 } 444 else if (Cmd == EInterfaceType::FindScore) //查询学员成绩 445 { 446 std::cout << "图书管理系统 -> 学员信息管理界面" << std::endl; 447 std::cout << "------------------------------------" << std::endl; 448 std::cout << "------------------------------------" << std::endl; 449 std::cout << std::endl; 450 std::cout << "输入学员学号" << std::endl; 451 std::cin >> CmdStr; 452 int CmdBf = std::atoi(CmdStr.data()); 453 454 455 if (NodeArray.IsValid(CmdBf)) 456 { 457 std::cout << "学员信息为" << std::endl; 458 FNode NewData = NodeArray.FindStudentData(CmdBf); 459 ShowData(NewData); 460 std::cout << "输入科目" << std::endl; 461 std::cout << "0,数学" << std::endl; 462 std::cout << "1,英语" << std::endl; 463 std::cout << "2,程序" << std::endl; 464 std::cout << "3,数据结构" << std::endl; 465 std::cin >> CmdStr; 466 CmdBf = std::atoi(CmdStr.data()); 467 468 469 std::cout << "当前成绩为,任意键返回" << std::endl; 470 std::cout << NewData.GetScore((ESubject)CmdBf) << std::endl; 471 std::cin >> CmdStr; 472 return EInterfaceType::Main; // 返回该界面 473 } 474 } 475 else if (Cmd == EInterfaceType::ShowAll) 476 { 477 std::cout << "图书管理系统 -> 数据集合" << std::endl; 478 std::cout << "------------------------------------" << std::endl; 479 std::cout << "------------------------------------" << std::endl; 480 std::cout << std::endl; 481 482 std::cout << " " << "Math" << " "; 483 std::cout << " " << "English" << " "; 484 std::cout << " " << "Program" << " "; 485 std::cout << " " << "Data" << " "; 486 std::cout << " " << std::endl; 487 for (int i = 0; i < NodeArray.GetSize(); i++) 488 { 489 FNode FindNode = NodeArray.GetArrayNode(i); 490 ShowData(FindNode); 491 } 492 std::cout << "任意键返回" << std::endl; 493 std::cin >> CmdStr; 494 return EInterfaceType::Main; 495 496 } 497 498 return EInterfaceType::Break; 499 } 500 501 502 int main() 503 { 504 int Cmd = 0; 505 while (true) 506 { 507 if (Cmd == EInterfaceType::Break) 508 { 509 return 0; 510 } 511 system("cls"); 512 Cmd = Interface(Cmd); 513 } 514 getchar(); 515 return 0; 516 } 517 518 519 // 测试 v0.0.1 520 // //添加信息 521 // FNode Node; 522 // Node.ID = 0; 523 // Node.Name = std::string("zhangshan"); 524 // Node.SetScore(ESubject::Data, 65.f); 525 // Node.SetScore(ESubject::English, 59.f); 526 // Node.SetScore(ESubject::Math, 63.f); 527 // Node.SetScore(ESubject::Program, 68.f); 528 // NodeArray.AddStudentData(Node); 529 // 530 // 531 // FNode Node2; 532 // Node2.ID = 1; 533 // Node2.Name = std::string("wangwu"); 534 // Node2.SetScore(ESubject::Data, 61.f); 535 // Node2.SetScore(ESubject::English, 55.f); 536 // Node2.SetScore(ESubject::Math, 77.f); 537 // Node2.SetScore(ESubject::Program, 12.f); 538 // 539 // NodeArray.AddStudentData(Node2); 540 // 541 // 542 // //修改分数 543 // NodeArray.ChangeScore(0, ESubject::English, 75.f); //修改一个人的分数 544 // NodeArray.ChangeScore(1, ESubject::Data, 77.f); //修改一个人的分数 545 // 546 // 547 ////修改整体信息 548 // FNode NewNode; 549 // NewNode.ID = 6; 550 // NewNode.Name = std::string("laowang"); 551 // NewNode.SetScore(ESubject::Data, 61.f); 552 // NewNode.SetScore(ESubject::English, 55.f); 553 // NewNode.SetScore(ESubject::Math, 77.f); 554 // NewNode.SetScore(ESubject::Program, 12.f); 555 // 556 // 557 // NodeArray.ChangeStudentData(1, NewNode); 558 // 559 // //删除学生信息 560 // NodeArray.DeleteStudentData(6); 561 // 562 // 563 // //查询成绩 564 // float EnglishScore = NodeArray.FindScore(0, ESubject::English); 565 // //查找完整的学生数据 566 // FNode FindNode = NodeArray.FindStudentData(1); 567 // //判断学生是否存在 568 // NodeArray.IsValid(0); 569 // 570 // 571 // //遍历完整表格 572 // for (int i = 0; i < NodeArray.GetSize(); i++) 573 // { 574 // FNode FindNode = NodeArray.GetArrayNode(i); 575 // } 576 // 577 //