Advertisement
Guest User

Untitled

a guest
May 30th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. //enum type { state, contractor };
  7.  
  8. struct Student
  9. {
  10. string Name;
  11. bool Type;
  12. float Scholarship;
  13. int Marks[5];
  14. Student* StudentNext;
  15. };
  16.  
  17. struct Group
  18. {
  19. string NumOfGroup;
  20. int Quantity;
  21. Student* StudentTop;
  22. Group* GroupNext;
  23. } *List = NULL;
  24.  
  25. struct Queue
  26. {
  27. Student* QueInfo;
  28. Queue* QueNext;
  29. } *Root = NULL;
  30.  
  31. void MainPage();
  32. void Menu();
  33. void Choose();
  34. Group *ConsoleInput();
  35. Group *FileInput();
  36. void GroupAdd();
  37. void GroupEdit();
  38. void GroupDelete();
  39. void ChangeGroupName();
  40. void StudentAdd();
  41. void StudentEdit();
  42. void StudentDelete();
  43. void FirstPersonalTask();
  44. void SecondPersonalTask();
  45. void ConsoleOutput();
  46. void FileOutput();
  47. void Que();
  48. void Sort();
  49. void Swap(Queue *Obj1, Queue *Obj2);
  50.  
  51. int main()
  52. {
  53. MainPage();
  54. return 0;
  55. }
  56.  
  57. void MainPage()
  58. {
  59. while(1)
  60. {
  61. Menu();
  62. Choose();
  63. }
  64. }
  65.  
  66. void Menu()
  67. {
  68. cout << "==========CourseWork==========\n";
  69. cout << "Press 1 for Console input.\n";
  70. cout << "Press 2 for File input.\n";
  71. cout << "Press 3 for edit a group.\n";
  72. cout << "Press 4 for output in lexicographical order of all contractors who have twos on exams.\n";
  73. cout << "Press 5 for output number of group then average mark on group ";
  74. cout << "and last quantity of contractor in this group.\n";
  75. cout << "Press 6 for console output.\n";
  76. cout << "Press 7 for file output.\n";
  77. cout << "Press 0 for exit.\n";
  78. cout << "Enter your choice: ";
  79. }
  80.  
  81. void Choose()
  82. {
  83. int choice;
  84. cin >> choice;
  85. switch(choice)
  86. {
  87. case 1:
  88. {
  89. List = ConsoleInput();
  90. break;
  91. }
  92. case 2:
  93. {
  94. List = FileInput();
  95. break;
  96. }
  97. case 3:
  98. {
  99. GroupEdit();
  100. break;
  101. }
  102. case 4:
  103. {
  104. FirstPersonalTask();
  105. break;
  106. }
  107. case 5:
  108. {
  109. SecondPersonalTask();
  110. break;
  111. }
  112. case 6:
  113. {
  114. ConsoleOutput();
  115. break;
  116. }
  117. case 7:
  118. {
  119. FileOutput();
  120. break;
  121. }
  122. case 0:
  123. {
  124. exit(EXIT_SUCCESS);
  125. }
  126. default:
  127. {
  128. cout << "Wrong key.\n";
  129. MainPage();
  130. }
  131. }
  132. }
  133.  
  134. Group *ConsoleInput()
  135. {
  136. string str;
  137. Group *GroupTop = List, *GroupBack = List;
  138. Group *GroupCreate = NULL;
  139. Student *StudentRoot = NULL, *StudentCreate = NULL, *StudentBack = NULL;
  140. while(1)
  141. {
  142. cout << "Enter the group number:\n";
  143. cout << "Enter exit for quit.\n";
  144. cin >> str;
  145. if(str.compare("exit"))
  146. {
  147. GroupCreate = new Group;
  148. GroupCreate->NumOfGroup = str;
  149. GroupCreate->Quantity = 0;
  150. GroupCreate->StudentTop = NULL;
  151. GroupCreate->GroupNext = NULL;
  152. StudentRoot = GroupCreate->StudentTop;
  153. if(!GroupTop)
  154. {
  155. GroupTop = GroupCreate;
  156. GroupBack = GroupCreate;
  157. }
  158. else
  159. {
  160. while(GroupBack->GroupNext) GroupBack = GroupBack->GroupNext;
  161. GroupBack->GroupNext = GroupCreate;
  162. GroupBack = GroupCreate;
  163. }
  164. while (1)
  165. {
  166. cout << "Enter NextGroup for add new group.\n";
  167. cout << "Enter exit for quit.\n";
  168. cout << "Enter the student name:\n";
  169. cin >> str;
  170. if(!str.compare("exit)"))
  171. return GroupTop;
  172. if(str.compare("NextGroup"))
  173. {
  174. GroupCreate->Quantity++;
  175. StudentCreate = new Student;
  176. StudentCreate->Name = str;
  177. cout << "Enter his marks: ";
  178. for(int i = 0; i < 5; i++) cin >> StudentCreate->Marks[i];
  179. cout << "Enter his scholarship";
  180. cin >> StudentCreate->Scholarship;
  181. cout << "Enter his type:\n";
  182. cout << "0 - statement, 1 - contractor.\n";
  183. cin >> StudentCreate->Type;
  184. if(!StudentRoot)
  185. {
  186. StudentRoot = StudentCreate;
  187. StudentBack = StudentCreate;
  188. }
  189. else
  190. {
  191. StudentBack->StudentNext = StudentCreate;
  192. StudentBack = StudentCreate;
  193. }
  194. }
  195. else
  196. {
  197. StudentBack->StudentNext = NULL;
  198. break;
  199. }
  200. }
  201. }
  202. else
  203. {
  204. GroupBack->GroupNext = NULL;
  205. break;
  206. }
  207. }
  208. return GroupTop;
  209. }
  210.  
  211. Group *FileInput()
  212. {
  213. char buff[30];
  214. cin >> buff;
  215. ifstream fin(buff);
  216. string str;
  217. Group *GroupTop = List, *GroupCreate = NULL, *GroupBack = NULL;
  218. Student *StudentRoot = NULL, *StudentCreate = NULL, *StudentBack = NULL;
  219. while(1)
  220. {
  221. cout << "Enter:\n";
  222. cout << "Group number(exit for leave)";
  223. cout << "Student name(NextGroup for add new group).\n";
  224. cout << "His marks.\n";
  225. cout << "His scholarship.\n";
  226. cout << "His status(0 - statement, 1 - contractor).\n";
  227. cin >> str;
  228. if(str.compare("exit"))
  229. {
  230. GroupCreate = new Group;
  231. GroupCreate->NumOfGroup = str;
  232. GroupCreate->Quantity = 0;
  233. GroupCreate->StudentTop = NULL;
  234. GroupCreate->GroupNext = NULL;
  235. StudentRoot = GroupCreate->StudentTop;
  236. if(!GroupTop)
  237. {
  238. GroupTop = GroupCreate;
  239. GroupBack = GroupCreate;
  240. }
  241. else
  242. {
  243. GroupBack->GroupNext = GroupCreate;
  244. GroupBack = GroupCreate;
  245. }
  246. while (1)
  247. {
  248. fin >> str;
  249. if(!str.compare("exit"))
  250. break;
  251. if(str.compare("NextGroup"))
  252. {
  253. GroupCreate->Quantity++;
  254. StudentCreate = new Student;
  255. StudentCreate->Name = str;
  256. for(int i = 0; i < 5; i++) fin >> StudentCreate->Marks[i];
  257. fin >> StudentCreate->Scholarship;
  258. fin >> StudentCreate->Type;
  259. if(!StudentRoot)
  260. {
  261. StudentRoot = StudentCreate;
  262. StudentBack = StudentCreate;
  263. }
  264. else
  265. {
  266. StudentBack->StudentNext = StudentCreate;
  267. StudentBack = StudentCreate;
  268. }
  269. }
  270. else
  271. {
  272. StudentBack->StudentNext = NULL;
  273. break;
  274. }
  275. }
  276. }
  277. else
  278. {
  279. GroupBack->GroupNext = NULL;
  280. break;
  281. }
  282. }
  283. fin.close();
  284. return GroupTop;
  285. }
  286.  
  287. void GroupEdit()
  288. {
  289. cout << "Press 1 for change group name.\n";
  290. cout << "Press 2 for add group.\n";
  291. cout << "Press 3 for delete group.\n";
  292. cout << "Press 4 for student add.\n";
  293. cout << "Press 5 for student edit.\n";
  294. cout << "Press 6 for student delete.\n";
  295. cout << "Press 0 for back";
  296. int choice;
  297. cin >> choice;
  298. switch(choice)
  299. {
  300. case 1:
  301. {
  302. ChangeGroupName();
  303. break;
  304. }
  305. case 2:
  306. {
  307. GroupAdd();
  308. break;
  309. }
  310. case 3:
  311. {
  312. GroupDelete();
  313. break;
  314. }
  315. case 4:
  316. {
  317. StudentAdd();
  318. break;
  319. }
  320. case 5:
  321. {
  322. StudentEdit();
  323. break;
  324. }
  325. case 6:
  326. {
  327. StudentDelete();
  328. break;
  329. }
  330. case 0:
  331. {
  332. return;
  333. }
  334. }
  335. }
  336.  
  337. void ChangeGroupName()
  338. {
  339. string NewGroupName, OldGroupName;
  340. cout << "Enter old group name: ";
  341. cin >> OldGroupName;
  342. cout << "Enter new group name: ";
  343. cin >> NewGroupName;
  344. Group *GroupTop, *GroupCurrent;
  345. GroupCurrent = GroupTop = List;
  346. if(!GroupTop)
  347. {
  348. cout << "This group wasn't created.\n";
  349. return;
  350. }
  351. else
  352. {
  353. while(GroupCurrent->GroupNext)
  354. {
  355. if(GroupCurrent->NumOfGroup == OldGroupName)
  356. {
  357. GroupCurrent->NumOfGroup = NewGroupName;
  358. }
  359. GroupCurrent = GroupCurrent->GroupNext;
  360. }
  361. }
  362. }
  363.  
  364. void GroupAdd()
  365. {
  366. Group *GroupTop, *GroupCreate, *GroupCurrent;
  367. GroupCurrent = GroupTop = List;
  368. Student *StudentCreate, *StudentBack;
  369. string str;
  370.  
  371. GroupCreate = new Group;
  372. cout << "Enter the group number: ";
  373. cin >> GroupCreate->NumOfGroup;
  374. GroupCreate->Quantity = 0;
  375. GroupCreate->StudentTop = NULL;
  376. GroupCreate->GroupNext = NULL;
  377. StudentCreate = StudentBack = GroupCreate->StudentTop;
  378. if(!GroupTop)
  379. {
  380. GroupTop = GroupCreate;
  381. }
  382. else
  383. {
  384. while(GroupCurrent->GroupNext)
  385. {
  386. GroupCurrent = GroupCurrent->GroupNext;
  387. }
  388. GroupCurrent->GroupNext = GroupCreate;
  389. }
  390. while (1)
  391. {
  392. cout << "Enter exit for quit.\n";
  393. cout << "Enter the student name:\n";
  394. cin >> str;
  395. if (str.compare("Stop"))
  396. {
  397. GroupCreate->Quantity++;
  398. StudentCreate = new Student;
  399. StudentCreate->Name = str;
  400. cout << "Enter his marks: ";
  401. for(int i = 0; i < 5; i++) cin >> StudentCreate->Marks[i];
  402. cout << "Enter his scholarship: ";
  403. cin >> StudentCreate->Scholarship;
  404. cout << "Enter his status: ";
  405. cin >> StudentCreate->Type;
  406. if(!GroupCreate->StudentTop)
  407. {
  408. GroupCreate->StudentTop = StudentCreate;
  409. StudentBack = StudentCreate;
  410. }
  411. else
  412. {
  413. StudentBack->StudentNext = StudentCreate;
  414. StudentBack = StudentCreate;
  415. }
  416. }
  417. else
  418. {
  419. StudentBack->StudentNext = NULL;
  420. break;
  421. }
  422. }
  423. }
  424.  
  425. void GroupDelete()
  426. {
  427. string _NumOfGroup;
  428. cout << "Enter the group number: ";
  429. cin >> _NumOfGroup;
  430. Student *StudCurrent, *StudParent;
  431. Group *GroupCurrent, *GroupParent;
  432. GroupCurrent = List;
  433. while (GroupCurrent)
  434. {
  435. GroupParent = GroupCurrent;
  436. if (GroupCurrent->NumOfGroup == _NumOfGroup)
  437. {
  438. StudParent = StudCurrent = GroupCurrent->StudentTop;
  439. while (StudCurrent)
  440. {
  441. StudCurrent = StudCurrent->StudentNext;
  442. delete StudParent;
  443. StudParent = StudCurrent;
  444. }
  445. if(GroupCurrent == List)
  446. {
  447. GroupCurrent = GroupCurrent->GroupNext;
  448. delete List;
  449. List = GroupCurrent;
  450. break;
  451. }
  452. else
  453. {
  454. GroupParent->GroupNext = GroupCurrent->GroupNext;
  455. delete GroupCurrent;
  456. GroupCurrent = GroupParent->GroupNext;
  457. break;
  458. }
  459. }
  460. GroupCurrent = GroupCurrent->GroupNext;
  461. }
  462. }
  463.  
  464. void StudentAdd()
  465. {
  466. string _NumOfGroup;
  467. cout << "Enter the group number: ";
  468. cin >> _NumOfGroup;
  469. Group *GroupCurrent = List;
  470. Student *StudentCurrent = NULL, *StudentParent = NULL, *StudentCreate = NULL;
  471. while(GroupCurrent)
  472. {
  473. if(GroupCurrent->NumOfGroup == _NumOfGroup)
  474. {
  475. StudentCreate = new Student;
  476. cout << "Enter the studet name: ";
  477. cin >> StudentCreate->Name;
  478. cout << "Enter his status: ";
  479. cin >> StudentCreate->Type;
  480. cout << "Enter his scholarship: ";
  481. cin >> StudentCreate->Scholarship;
  482. cout << "Enter his marks: ";
  483. for(int i = 0; i < 5; i++) cin >> StudentCreate->Marks[i];
  484. StudentCreate->StudentNext = NULL;
  485. StudentCurrent = GroupCurrent->StudentTop;
  486. while(StudentCurrent)
  487. {
  488. StudentParent = StudentCurrent;
  489. StudentCurrent = StudentCurrent->StudentNext;
  490. }
  491. StudentParent->StudentNext = StudentCreate;
  492. GroupCurrent->Quantity++;
  493. }
  494. }
  495. }
  496.  
  497. void StudentEdit()
  498. {
  499. string _NumOfGroup, _Name;
  500. cout << "Enter the group number: ";
  501. cin >> _NumOfGroup;
  502. cout << "Enter the student name: ";
  503. cin >> _Name;
  504. Group *GroupCurrent = List;
  505. Student *StudentCurrent;
  506. while(GroupCurrent)
  507. {
  508. if(GroupCurrent->NumOfGroup == _NumOfGroup)
  509. {
  510. while(StudentCurrent)
  511. {
  512. if(StudentCurrent->Name == _Name)
  513. {
  514. cout << "Enter his status: ";
  515. cin >> StudentCurrent->Type;
  516. cout << "Enter his scholarship: ";
  517. cin >> StudentCurrent->Scholarship;
  518. cout << "Enter his marks: ";
  519. for(int i = 0; i < 5; i++) cin >> StudentCurrent->Marks[i];
  520. }
  521. StudentCurrent = StudentCurrent->StudentNext;
  522. }
  523. }
  524. GroupCurrent = GroupCurrent->GroupNext;
  525. }
  526. }
  527.  
  528. void StudentDelete()
  529. {
  530. string _NumOfGroup, _Name;
  531. cout << "Enter the group number: ";
  532. cin >> _NumOfGroup;
  533. cout << "Enter the student name: ";
  534. cin >> _Name;
  535. Group *GroupCurrent = List;
  536. Student *StudentCurrent, *StudentParent;
  537. while(GroupCurrent)
  538. {
  539. if(GroupCurrent->NumOfGroup == _NumOfGroup)
  540. {
  541. StudentCurrent = GroupCurrent->StudentTop;
  542. while(StudentCurrent)
  543. {
  544. StudentParent = StudentCurrent;
  545. if(StudentCurrent->Name == _Name)
  546. {
  547. StudentParent->StudentNext = StudentCurrent->StudentNext;
  548. delete StudentCurrent;
  549. StudentCurrent = StudentParent->StudentNext;
  550. }
  551. StudentCurrent = StudentCurrent->StudentNext;
  552. }
  553. }
  554. GroupCurrent = GroupCurrent->GroupNext;
  555. }
  556. }
  557.  
  558. void FirstPersonalTask()
  559. {
  560. if(!Root) Que();
  561. Queue *QueCurrent = Root;
  562. while(QueCurrent)
  563. {
  564. cout << QueCurrent->QueInfo->Name << ' ';
  565. switch(QueCurrent->QueInfo->Type)
  566. {
  567. case 0://this situation can't be
  568. {
  569. cout << " State ";
  570. break;
  571. }
  572. case 1:
  573. {
  574. cout << " Contractor ";
  575. break;
  576. }
  577. }
  578. cout << QueCurrent->QueInfo->Scholarship << ' ';
  579. for(int i = 0; i < 5; i++)
  580. {
  581. cout << QueCurrent->QueInfo->Marks[i] << ' ';
  582. }
  583. cout << '\n';
  584. QueCurrent = QueCurrent->QueNext;
  585. }
  586. }
  587.  
  588. void Que()
  589. {
  590. Queue *Create, *QueCurrent;
  591. Create = new Queue;
  592. Group *GroupCurrent = NULL;
  593. Student *StudentCurrent = NULL;
  594. QueCurrent = Root = Create;
  595. GroupCurrent = List;
  596. while(GroupCurrent->GroupNext)
  597. {
  598. StudentCurrent = GroupCurrent->StudentTop;
  599. while(StudentCurrent->StudentNext)
  600. {
  601. if(StudentCurrent->Type)
  602. {
  603. for(int i = 0; i < 5; i++)
  604. {
  605. if(StudentCurrent->Marks[i] == 2)
  606. {
  607. Create->QueInfo = StudentCurrent;
  608. Create->QueNext = NULL;
  609. while(QueCurrent->QueNext)
  610. {
  611. QueCurrent = QueCurrent->QueNext;
  612. }
  613. QueCurrent->QueNext = Create;
  614. break;
  615. }
  616. }
  617. }
  618. Create = new Queue;
  619. StudentCurrent = StudentCurrent->StudentNext;
  620. }
  621. GroupCurrent = GroupCurrent->GroupNext;
  622. }
  623. Sort();
  624. }
  625.  
  626. void Sort()
  627. {
  628. Queue *QueCurrent, *QueSwap, *QueMinimum;
  629. QueCurrent = QueSwap = QueMinimum = Root;
  630. while(QueSwap->QueNext)
  631. {
  632. while(QueCurrent)
  633. {
  634. if(QueCurrent->QueNext->QueInfo->Name < QueMinimum->QueInfo->Name)
  635. {
  636. QueMinimum = QueCurrent;
  637. }
  638. }
  639. Swap(QueMinimum, QueSwap);
  640. QueSwap = QueSwap->QueNext;
  641. }
  642. }
  643.  
  644. void Swap(Queue *Obj1, Queue *Obj2)
  645. {
  646. Queue *Temp = new Queue;
  647.  
  648. Temp->QueInfo->Name = Obj1->QueInfo->Name;
  649. Obj1->QueInfo->Name = Obj2->QueInfo->Name;
  650. Obj2->QueInfo->Name = Temp->QueInfo->Name;
  651.  
  652. Temp->QueInfo->Scholarship = Obj1->QueInfo->Scholarship;
  653. Obj1->QueInfo->Scholarship = Obj2->QueInfo->Scholarship;
  654. Obj2->QueInfo->Scholarship = Temp->QueInfo->Scholarship;
  655.  
  656. Temp->QueInfo->Type = Obj1->QueInfo->Type;
  657. Obj1->QueInfo->Type = Obj2->QueInfo->Type;
  658. Obj2->QueInfo->Type = Temp->QueInfo->Type;
  659.  
  660. for(int i = 0; i < 5; i++)
  661. {
  662. Temp->QueInfo->Marks[i] = Obj1->QueInfo->Marks[i];
  663. Obj1->QueInfo->Marks[i] = Obj2->QueInfo->Marks[i];
  664. Obj2->QueInfo->Marks[i] = Temp->QueInfo->Marks[i];
  665. }
  666.  
  667. delete Temp;
  668. }
  669.  
  670. void SecondPersonalTask()
  671. {
  672. Group *GroupCurrent;
  673. Student *StudentCurrent;
  674. GroupCurrent = List;
  675. int AverageMark = 0;
  676. int QuantityContractor = 0;
  677. while(GroupCurrent)
  678. {
  679. StudentCurrent = GroupCurrent->StudentTop;
  680. cout << GroupCurrent->NumOfGroup << '\t';
  681. while(StudentCurrent)
  682. {
  683. for(int i = 0; i < 5; i++)
  684. {
  685. AverageMark += StudentCurrent->Marks[i];
  686. }
  687. if(StudentCurrent->Type)
  688. {
  689. QuantityContractor++;
  690. }
  691. StudentCurrent = StudentCurrent->StudentNext;
  692. }
  693. cout << " Average Mark " << AverageMark << '\t';
  694. cout << " Quantity of contactor " << QuantityContractor << '\n';
  695. AverageMark = 0;
  696. QuantityContractor = 0;
  697. GroupCurrent = GroupCurrent->GroupNext;
  698. }
  699. }
  700.  
  701. void ConsoleOutput()
  702. {
  703. Group *GroupCurrent;
  704. Student *StudentCurrent;
  705. GroupCurrent = List;
  706. while(GroupCurrent)
  707. {
  708. StudentCurrent = GroupCurrent->StudentTop;
  709. cout << GroupCurrent->NumOfGroup << '\n';
  710. while(StudentCurrent)
  711. {
  712. cout << StudentCurrent->Name << ' ';
  713. cout << StudentCurrent->Type << ' ';
  714. cout << StudentCurrent->Scholarship << ' ';
  715. for(int i = 0; i < 5; i++)
  716. {
  717. cout << StudentCurrent->Marks[i] << ' ';
  718. }
  719. cout << '\n';
  720. StudentCurrent = StudentCurrent->StudentNext;
  721. }
  722. GroupCurrent = GroupCurrent->GroupNext;
  723. }
  724. }
  725.  
  726. void FileOutput()
  727. {
  728. char buff[30];
  729. cin >> buff;
  730. ofstream fout(buff);
  731. Group *GroupCurrent;
  732. Student *StudentCurrent;
  733. GroupCurrent = List;
  734. while(GroupCurrent)
  735. {
  736. StudentCurrent = GroupCurrent->StudentTop;
  737. fout << GroupCurrent->NumOfGroup << '\n';
  738. while(StudentCurrent)
  739. {
  740. fout << StudentCurrent->Name << ' ';
  741. fout << StudentCurrent->Type << ' ';
  742. fout << StudentCurrent->Scholarship << ' ';
  743. for(int i = 0; i < 5; i++)
  744. {
  745. fout << StudentCurrent->Marks[i] << ' ';
  746. }
  747. fout << '\n';
  748. StudentCurrent = StudentCurrent->StudentNext;
  749. }
  750. GroupCurrent = GroupCurrent->GroupNext;
  751. }
  752. fout.close();
  753. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement