Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- #include <cstdlib>
- #include <cstdio>
- #include <fstream>
- using namespace std;
- struct Student
- {
- string Name;
- bool Type;
- float Scholarship;
- int Marks[5];
- Student* StudentNext;
- };
- struct Group
- {
- string NumOfGroup;
- int Quantity;
- int QuantityOfContractor;
- Student* StudentTop;
- Group* GroupNext;
- } *List = NULL;
- struct Queue
- {
- Student* QueInfo;
- Queue* QueNext;
- } *Root = NULL;
- struct Table
- {
- float AverageMark;
- Group *GroupInfo;
- Table *TableNext;
- } *TableTop = NULL;
- void MainPage();
- void Menu();
- void Choose();
- Group *ConsoleInput();
- Group *FileInput();
- void GroupAdd();
- void GroupEdit();
- void GroupDelete();
- void ChangeGroupName();
- void StudentAdd();
- void StudentEdit();
- void StudentDelete();
- void FirstPersonalTask();
- void PutInQue(Student *StudentContractor);
- void ShowFirstTask();
- void SecondPersonalTask();
- float Average(Group *Current);
- void ShowSecondTask();
- void ConsoleOutput();
- void FileOutput();
- int main()
- {
- MainPage();
- return 0;
- }
- void MainPage()
- {
- while(1)
- {
- Menu();
- Choose();
- }
- }
- void Menu()
- {
- cout << "==========CourseWork==========\n";
- cout << "Press 1 for Console input.\n";
- cout << "Press 2 for File input.\n";
- cout << "Press 3 for edit a group.\n";
- cout << "Press 4 for output in lexicographical order of all contractors who have twos on exams.\n";
- cout << "Press 5 for output number of group then average mark on group ";
- cout << "and last quantity of contractor in this group.\n";
- cout << "Press 6 for console output.\n";
- cout << "Press 7 for file output.\n";
- cout << "Press 0 for exit.\n";
- cout << "Enter your choice: ";
- }
- void Choose()
- {
- int choice;
- cin >> choice;
- switch(choice)
- {
- case 1:
- {
- List = ConsoleInput();
- break;
- }
- case 2:
- {
- List = FileInput();
- break;
- }
- case 3:
- {
- GroupEdit();
- break;
- }
- case 4:
- {
- FirstPersonalTask();
- break;
- }
- case 5:
- {
- SecondPersonalTask();
- break;
- }
- case 6:
- {
- ConsoleOutput();
- break;
- }
- case 7:
- {
- FileOutput();
- break;
- }
- case 0:
- {
- exit(EXIT_SUCCESS);
- }
- default:
- {
- cout << "Wrong key.\n";
- MainPage();
- }
- }
- }
- Group *ConsoleInput()
- {
- string str;
- Group *GroupTop = List, *GroupBack = List, *GroupCreate = NULL;
- Student *StudentCreate = NULL, *StudentBack = NULL;
- while(1)
- {
- cout << "Enter exit for quit.\n";
- cout << "Enter the group number: ";
- cin.ignore();
- cin.clear();
- getline(cin, str);
- if(str != "exit")
- {
- GroupCreate = new Group;
- GroupCreate->NumOfGroup = str;
- GroupCreate->Quantity = 0;
- GroupCreate->QuantityOfContractor = 0;
- GroupCreate->StudentTop = NULL;
- GroupCreate->GroupNext = NULL;
- if(!GroupTop)
- {
- GroupTop = GroupCreate;
- GroupBack = GroupCreate;
- }
- else
- {
- while(GroupBack->GroupNext) GroupBack = GroupBack->GroupNext;
- GroupBack->GroupNext = GroupCreate;
- GroupBack = GroupCreate;
- }
- while (1)
- {
- cout << "Enter NextGroup for add new group.\n";
- cout << "Enter exit for quit.\n";
- cout << "Enter the student name: ";
- cin.ignore();
- cin.clear();
- getline(cin, str);
- if(str == "exit")
- return GroupTop;
- if(str != "NextGroup")
- {
- GroupCreate->Quantity++;
- StudentCreate = new Student;
- StudentCreate->Name = str;
- cout << "Enter his marks: ";
- for(int i = 0; i < 5; i++) cin >> StudentCreate->Marks[i];
- cout << "Enter his scholarship: ";
- cin >> StudentCreate->Scholarship;
- cout << "0 - state, 1 - contractor.\n";
- cout << "Enter his type: ";
- cin >> StudentCreate->Type;
- if(StudentCreate->Type) GroupCreate->QuantityOfContractor++;
- if(!GroupCreate->StudentTop)
- {
- GroupCreate->StudentTop = StudentCreate;
- StudentBack = StudentCreate;
- }
- else
- {
- StudentBack->StudentNext = StudentCreate;
- StudentBack = StudentCreate;
- }
- }
- else
- {
- StudentBack->StudentNext = NULL;
- break;
- }
- }
- }
- else
- {
- if(GroupBack)
- GroupBack->GroupNext = NULL;
- break;
- }
- }
- return GroupTop;
- }
- //???????
- Group *FileInput()
- {
- char buff[30];
- cout << "Enter the name of file: ";
- cin >> buff;
- string str;
- string test;
- ifstream fin(buff);
- if(!fin.is_open())
- {
- cout << "File doesn't open this file.\n";
- return NULL;
- }
- Group *GroupTop = List, *GroupCreate = NULL, *GroupBack = List;
- Student *StudentRoot = NULL, *StudentCreate = NULL, *StudentBack = NULL;
- while(1)
- {
- cout << "Group number (exit for leave)";
- cout << "Student name (NextGroup for add new group).\n";
- cout << "His marks.\n";
- cout << "His scholarship.\n";
- cout << "His status(0 - state, 1 - contractor).\n";
- fin >> str;
- //getline(fin, str);
- cout << "Group: " << str << '\n';
- cin >> test;
- if(str != "exit")
- {
- GroupCreate = new Group;
- GroupCreate->NumOfGroup = str;
- GroupCreate->Quantity = 0;
- GroupCreate->QuantityOfContractor = 0;
- GroupCreate->StudentTop = NULL;
- GroupCreate->GroupNext = NULL;
- StudentRoot = GroupCreate->StudentTop;
- if(!GroupTop)
- {
- GroupTop = GroupCreate;
- GroupBack = GroupCreate;
- }
- else
- {
- while(GroupBack->GroupNext) GroupBack = GroupBack->GroupNext;
- GroupBack->GroupNext = GroupCreate;
- GroupBack = GroupCreate;
- }
- while (1)
- {
- fin >> str;
- //getline(fin, str);
- cout << "Student: " << str << '\n';
- cin >> test;
- if(str == "exit")
- {
- fin.close();
- return GroupTop;
- }
- if(str != "NextGroup")
- {
- GroupCreate->Quantity++;
- StudentCreate = new Student;
- StudentCreate->Name = str;
- for(int i = 0; i < 5; i++)
- {
- fin >> StudentCreate->Marks[i];
- cout << StudentCreate->Marks[i];
- }
- cin >> test;
- cout << endl;
- fin >> StudentCreate->Scholarship;
- cout << "Student scholarship: " << StudentCreate->Scholarship << '\n';
- cin >> test;
- fin >> StudentCreate->Type;
- cout << "Student type: " << StudentCreate->Type << '\n';
- cin >> test;
- if(StudentCreate->Type)
- GroupCreate->QuantityOfContractor++;
- if(!StudentRoot)
- {
- StudentRoot = StudentCreate;
- StudentBack = StudentCreate;
- }
- else
- {
- StudentBack->StudentNext = StudentCreate;
- StudentBack = StudentCreate;
- }
- }
- else
- {
- StudentBack->StudentNext = NULL;
- break;
- }
- }
- }
- else
- {
- if(GroupBack)
- GroupBack->GroupNext = NULL;
- break;
- }
- }
- fin.close();
- return GroupTop;
- }
- void GroupEdit()
- {
- cout << "Press 1 for change group name.\n";
- cout << "Press 2 for delete group.\n";
- cout << "Press 3 for student add.\n";
- cout << "Press 4 for student edit.\n";
- cout << "Press 5 for student delete.\n";
- cout << "Press 0 for back";
- int choice;
- cin >> choice;
- switch(choice)
- {
- case 1:
- {
- ChangeGroupName();
- break;
- }
- case 2:
- {
- GroupDelete();
- break;
- }
- case 3:
- {
- StudentAdd();
- break;
- }
- case 4:
- {
- StudentEdit();
- break;
- }
- case 5:
- {
- StudentDelete();
- break;
- }
- case 0:
- {
- return;
- }
- }
- }
- void ChangeGroupName()
- {
- string NewGroupName, OldGroupName;
- cout << "Enter old group name: ";
- cin >> OldGroupName;
- cout << "Enter new group name: ";
- cin >> NewGroupName;
- Group *GroupTop = List, *GroupCurrent = List;
- if(!GroupTop)
- {
- cout << "This group wasn't created.\n";
- return;
- }
- else
- {
- while(GroupCurrent->GroupNext)
- {
- if(GroupCurrent->NumOfGroup == OldGroupName)
- {
- GroupCurrent->NumOfGroup = NewGroupName;
- }
- GroupCurrent = GroupCurrent->GroupNext;
- }
- }
- }
- void GroupDelete()
- {
- string _NumOfGroup;
- cout << "Enter the group number: ";
- cin >> _NumOfGroup;
- Student *StudCurrent, *StudParent;
- Group *GroupCurrent, *GroupParent;
- GroupCurrent = List;
- while (GroupCurrent)
- {
- GroupParent = GroupCurrent;
- if (GroupCurrent->NumOfGroup == _NumOfGroup)
- {
- StudParent = StudCurrent = GroupCurrent->StudentTop;
- while (StudCurrent)
- {
- StudCurrent = StudCurrent->StudentNext;
- delete StudParent;
- StudParent = StudCurrent;
- }
- if(GroupCurrent == List)
- {
- GroupCurrent = GroupCurrent->GroupNext;
- delete List;
- List = GroupCurrent;
- break;
- }
- else
- {
- GroupParent->GroupNext = GroupCurrent->GroupNext;
- delete GroupCurrent;
- GroupCurrent = GroupParent->GroupNext;
- break;
- }
- }
- GroupCurrent = GroupCurrent->GroupNext;
- }
- }
- void StudentAdd()
- {
- string _NumOfGroup;
- cout << "Enter the group number: ";
- cin >> _NumOfGroup;
- Group *GroupCurrent = List;
- Student *StudentCurrent = NULL, *StudentParent = NULL, *StudentCreate = NULL;
- while(GroupCurrent)
- {
- if(GroupCurrent->NumOfGroup == _NumOfGroup)
- {
- StudentCreate = new Student;
- cout << "Enter the studet name: ";
- cin.ignore();
- cin.clear();
- getline(cin, StudentCreate->Name);
- cout << "Enter his status: ";
- cin >> StudentCreate->Type;
- cout << "Enter his scholarship: ";
- cin >> StudentCreate->Scholarship;
- cout << "Enter his marks: ";
- for(int i = 0; i < 5; i++) cin >> StudentCreate->Marks[i];
- StudentCreate->StudentNext = NULL;
- StudentCurrent = GroupCurrent->StudentTop;
- while(StudentCurrent)
- {
- StudentParent = StudentCurrent;
- StudentCurrent = StudentCurrent->StudentNext;
- }
- StudentParent->StudentNext = StudentCreate;
- GroupCurrent->Quantity++;
- if(StudentCreate->Type)
- GroupCurrent->QuantityOfContractor++;
- }
- }
- }
- void StudentEdit()
- {
- string _NumOfGroup, _Name;
- cout << "Enter the group number: ";
- cin >> _NumOfGroup;
- cout << "Enter the student name: ";
- cin.ignore();
- cin.clear();
- getline(cin, _Name);
- Group *GroupCurrent = List;
- Student *StudentCurrent;
- while(GroupCurrent)
- {
- if(GroupCurrent->NumOfGroup == _NumOfGroup)
- {
- while(StudentCurrent)
- {
- if(StudentCurrent->Name == _Name)
- {
- cout << "Enter his status: ";
- cin >> StudentCurrent->Type;
- cout << "Enter his scholarship: ";
- cin >> StudentCurrent->Scholarship;
- cout << "Enter his marks: ";
- for(int i = 0; i < 5; i++) cin >> StudentCurrent->Marks[i];
- }
- StudentCurrent = StudentCurrent->StudentNext;
- }
- }
- GroupCurrent = GroupCurrent->GroupNext;
- }
- }
- void StudentDelete()
- {
- string _NumOfGroup, _Name;
- cout << "Enter the group number: ";
- cin >> _NumOfGroup;
- cout << "Enter the student name: ";
- cin.clear();
- getline(cin, _Name);
- Group *GroupCurrent = List;
- Student *StudentCurrent, *StudentParent;
- while(GroupCurrent)
- {
- if(GroupCurrent->NumOfGroup == _NumOfGroup)
- {
- StudentCurrent = GroupCurrent->StudentTop;
- while(StudentCurrent)
- {
- StudentParent = StudentCurrent;
- if(StudentCurrent->Name == _Name)
- {
- if(StudentCurrent->Type)
- GroupCurrent->QuantityOfContractor--;
- StudentParent->StudentNext = StudentCurrent->StudentNext;
- delete StudentCurrent;
- StudentCurrent = StudentParent->StudentNext;
- }
- StudentCurrent = StudentCurrent->StudentNext;
- }
- }
- GroupCurrent = GroupCurrent->GroupNext;
- }
- }
- void FirstPersonalTask()
- {
- Group *GroupCurrent = List;
- Student *StudentCurrent = NULL;
- while(GroupCurrent)
- {
- StudentCurrent = GroupCurrent->StudentTop;
- while(StudentCurrent)
- {
- if(StudentCurrent->Type)
- {
- for(int i = 0; i < 5; i++)
- {
- if(StudentCurrent->Marks[i] == 2)
- {
- PutInQue(StudentCurrent);
- break;
- }
- }
- }
- StudentCurrent = StudentCurrent->StudentNext;
- }
- GroupCurrent = GroupCurrent->GroupNext;
- }
- ShowFirstTask();
- }
- void PutInQue(Student *StudentContractor)
- {
- Queue *Create = new Queue;
- Create->QueInfo = StudentContractor;
- Create->QueNext = NULL;
- Queue *QueCurrent;
- if(!Root)
- {
- Root = Create;
- }
- else
- {
- QueCurrent = Root;
- if(Create->QueInfo->Name < Root->QueInfo->Name)
- {
- Create->QueNext = Root;
- Root = Create;
- }
- else
- {
- while(QueCurrent)
- {
- if((!QueCurrent->QueNext) && (Create->QueInfo->Name > QueCurrent->QueInfo->Name))
- {
- QueCurrent->QueNext = Create;
- break;
- }
- if((Create->QueInfo->Name < QueCurrent->QueNext->QueInfo->Name) && (Create->QueInfo->Name > QueCurrent->QueInfo->Name))
- {
- Create->QueNext = QueCurrent->QueNext;
- QueCurrent->QueNext = Create;
- break;//?
- }
- else
- {
- QueCurrent = QueCurrent->QueNext;
- }
- }
- }
- }
- }
- void ShowFirstTask()
- {
- Queue *Current = Root;
- while(Current)
- {
- cout << Current->QueInfo->Name << '\n';
- Current = Current->QueNext;
- }
- cout << endl;
- }
- void SecondPersonalTask()
- {
- Group *GroupCurrent = List;
- Table *TableCurrent, *TableCreate;
- while(GroupCurrent)
- {
- TableCreate = new Table;
- TableCreate->GroupInfo = GroupCurrent;
- TableCreate->AverageMark = Average(GroupCurrent);
- TableCreate->TableNext = NULL;
- cout << "1\n";
- if(!TableTop)
- {
- TableTop = TableCreate;
- }
- else
- {
- TableCurrent = TableTop;
- if(TableCreate->AverageMark > TableTop->AverageMark)
- {
- TableCreate->TableNext = TableTop;
- TableTop = TableCreate;
- }
- else
- {
- while(TableCurrent)
- {
- cout << "2\n";
- if((!TableCurrent->TableNext) && (TableCreate->AverageMark < TableCurrent->AverageMark))
- {
- TableCurrent->TableNext = TableCreate;
- break;
- }
- if((TableCurrent->AverageMark > TableCreate->AverageMark) && (TableCurrent->TableNext->AverageMark < TableCreate->AverageMark))
- {
- TableCreate->TableNext = TableCurrent->TableNext;
- TableCurrent->TableNext = TableCreate;
- break;
- }
- else
- TableCurrent = TableCurrent->TableNext;
- }
- }
- }
- GroupCurrent = GroupCurrent->GroupNext;
- }
- ShowSecondTask();
- }
- float Average(Group *Current)
- {
- int Average = 0;
- Student *StudentCurrent = Current->StudentTop;
- while(StudentCurrent)
- {
- cout << "0\n";
- for(int i = 0; i < 5; i++)
- Average += StudentCurrent->Marks[i];
- StudentCurrent = StudentCurrent->StudentNext;
- }
- return Average = Average / (5 * Current->Quantity);
- }
- void ShowSecondTask()
- {
- Table *TableCurrent = TableTop;
- while(TableCurrent)
- {
- cout << "Number of group: " << TableCurrent->GroupInfo->NumOfGroup << '\t';
- cout << "Average: " << TableCurrent->AverageMark << '\t';
- cout << "Quantity of contractors: " << TableCurrent->GroupInfo->QuantityOfContractor << '\n';
- TableCurrent = TableCurrent->TableNext;
- }
- }
- void ConsoleOutput()
- {
- Group *GroupCurrent;
- Student *StudentCurrent;
- GroupCurrent = List;
- while(GroupCurrent)
- {
- StudentCurrent = GroupCurrent->StudentTop;
- cout << "Number of group: " << GroupCurrent->NumOfGroup << '\n';
- while(StudentCurrent)
- {
- cout << "Student name: " << StudentCurrent->Name << '\n';
- cout << "his type: " << StudentCurrent->Type << '\n';
- cout << "his scholarship " << StudentCurrent->Scholarship << '\n';
- cout << "his marks: ";
- for(int i = 0; i < 5; i++)
- {
- cout << StudentCurrent->Marks[i] << ", ";
- }
- cout << '\n';
- StudentCurrent = StudentCurrent->StudentNext;
- }
- GroupCurrent = GroupCurrent->GroupNext;
- }
- }
- void FileOutput()
- {
- char buff[30];
- cin >> buff;
- ofstream fout(buff);
- Group *GroupCurrent;
- Student *StudentCurrent;
- GroupCurrent = List;
- while(GroupCurrent)
- {
- StudentCurrent = GroupCurrent->StudentTop;
- fout << "Number of group: " << GroupCurrent->NumOfGroup << '\n';
- while(StudentCurrent)
- {
- fout << "Student name: " << StudentCurrent->Name << '\n';
- fout << "his type: " << StudentCurrent->Type << '\n';
- fout << "his scholarship: " << StudentCurrent->Scholarship << '\n';
- fout << "his marks: ";
- for(int i = 0; i < 5; i++)
- {
- fout << StudentCurrent->Marks[i] << ", ";
- }
- fout << '\n';
- StudentCurrent = StudentCurrent->StudentNext;
- }
- GroupCurrent = GroupCurrent->GroupNext;
- }
- fout.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement