Advertisement
Guest User

Untitled

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