Untitled
unknown
plain_text
2 years ago
10 kB
2
Indexable
Never
#include <iostream> #include <string> #include <fstream> using namespace std; struct Institute { Institute() { instituteID = ""; instituteName = ""; address = ""; numberOfTeachers = 0; next = NULL; prev = NULL; } string instituteID; string instituteName; string deanName; int numberOfTeachers; string address; Institute *next; Institute *prev; }; char validate() { char answer = 'a'; cout << "----------" << endl; while (answer > '9' || answer < '0') { cout << "Please enter a number: "; cin >> answer; } system("cls"); return answer; } void printMenu() { cout << "1. Show all institutes" << endl; cout << "2. Search institute by ID" << endl; cout << "3. Add new institute" << endl; cout << "4. Delete institute by ID" << endl; cout << "5. Search institute by Dean name" << endl; cout << "6. Count the number of institutes has more than 10 teachers" << endl; cout << "7. Sort institutes by decreasing the number of teachers" << endl; cout << "8. Add new institute satisfying position" << endl; cout << "9. Save to text file" << endl; cout << "0. Exit" << endl; } void printAllInstitutes(Institute *head) { Institute *current = head; while (current != NULL) { cout << "Institute ID: " << current->instituteID << endl; cout << "Institute name: " << current->instituteName << endl; cout << "Dean name: " << current->deanName << endl; cout << "Number of teachers: " << current->numberOfTeachers << endl; cout << "Address: " << current->address << endl; current = current->next; cout << "-----------------" << endl; } } Institute *findInstituteByID(Institute *head, string instituteID, bool isPrintOut = true) { Institute *current = head; while (current != NULL) { if (current->instituteID == instituteID) { if (isPrintOut) { cout << "Institute ID: " << current->instituteID << endl; cout << "Institute name: " << current->instituteName << endl; cout << "Dean name: " << current->deanName << endl; cout << "Number of teachers: " << current->numberOfTeachers << endl; cout << "Address: " << current->address << endl; } return current; } current = current->next; } if (isPrintOut) { cout << "Institute not found" << endl; } return NULL; } Institute *createInstitute() { cin.clear(); cin.ignore(); Institute *newInstitute = new Institute(); cout << "Institute ID: " << endl; getline(cin, newInstitute->instituteID); cout << "Institute name: " << endl; getline(cin, newInstitute->instituteName); cout << "Dean name: " << endl; getline(cin, newInstitute->deanName); cout << "Number of teachers: " << endl; cin >> newInstitute->numberOfTeachers; cin.clear(); cin.ignore(); cout << "Address: " << endl; getline(cin, newInstitute->address); return newInstitute; } void addInstituteStart(Institute **head, Institute *newInstitute) { // Check exist if (findInstituteByID(*head, newInstitute->instituteID, false) != NULL) { cout << "Institute already exist" << endl; } else { newInstitute->next = *head; if (*head) (*head)->prev = newInstitute; (*head) = newInstitute; } return; } void deleteInstituteByID(Institute **head, string instituteID) { Institute *institute = findInstituteByID(*head, instituteID, false); if (institute) { if (institute->prev == NULL) { (*head)->prev = NULL; (*head) = institute->next; } else if (institute->next == NULL) { institute->prev->next = NULL; } else { institute->prev->next = institute->next; institute->next->prev = institute->prev; } delete institute; } else { cout << "Institute not found" << endl; } } Institute *findInstituteByDeanName(Institute *head, string deanName, bool isPrintOut = true) { Institute *current = head; while (current != NULL) { if (current->deanName == deanName) { if (isPrintOut) { cout << "Institute ID: " << current->instituteID << endl; cout << "Institute name: " << current->instituteName << endl; cout << "Dean name: " << current->deanName << endl; cout << "Number of teachers: " << current->numberOfTeachers << endl; cout << "Address: " << current->address << endl; } return current; } current = current->next; } if (isPrintOut) { cout << "Institute not found" << endl; } return NULL; } int countInstituteHasMoreThanNTeachers(Institute *head, int n) { int count = 0; Institute *current = head; while (current != NULL) { if (current->numberOfTeachers > n) { count++; } current = current->next; } return count; } void sortInstituteByDecreasing(Institute **head) { Institute *current = *head; if (current != NULL) { Institute *next = (*head)->next; while (current != NULL) { while (next != NULL) { if (current->numberOfTeachers < next->numberOfTeachers) { Institute *temp = current; current = next; next = next->next; current->prev = temp->prev; current->next = temp; temp->prev = current; temp->next = next; if (current->prev != NULL) { current->prev->next = current; } if (next != NULL) { next->prev = current; } } else { next = next->next; } } current = current->next; next = (current) ? current->next : NULL; } } } void addInstituteAfterSorting(Institute **head, Institute *newInstitute) { Institute *current = (*head); if (current) { while (true) { if (current->numberOfTeachers < newInstitute->numberOfTeachers || current->next == NULL) { break; } current = current->next; } if (current->numberOfTeachers < newInstitute->numberOfTeachers) { if (current->prev) { current->prev->next = newInstitute; } else { (*head) = newInstitute; } newInstitute->prev = current->prev; newInstitute->next = current; current->prev = newInstitute; } else { current->next = newInstitute; newInstitute->prev = current; } } else { (*head) = newInstitute; } } void saveToFile(Institute *head) { ofstream file; file.open("institutes.txt"); Institute *current = head; while (current != NULL) { file << current->instituteID << endl; file << current->instituteName << endl; file << current->deanName << endl; file << current->numberOfTeachers << endl; file << current->address << endl; file << "--------" << endl; current = current->next; } file.close(); } int main() { // Declare variables Institute *head = NULL; string instituteID = ""; string deanName = ""; // Print menu char t = 'a'; while (t != '0') { printMenu(); t = validate(); switch (t) { case '1': printAllInstitutes(head); break; case '2': cin.clear(); cin.ignore(); getline(cin, instituteID); findInstituteByID(head, instituteID); break; case '3': addInstituteStart(&head, createInstitute()); cout << "Added new institute!" << endl; break; case '4': cin.clear(); cin.ignore(); getline(cin, instituteID); deleteInstituteByID(&head, instituteID); break; case '5': cin.clear(); cin.ignore(); getline(cin, deanName); findInstituteByDeanName(head, deanName); break; case '6': cout << "The number of institutes has more than 10 teachers: " << countInstituteHasMoreThanNTeachers(head, 10) << endl; break; case '7': sortInstituteByDecreasing(&head); cout << "Sorted institutes by decreasing the number of teachers..." << endl; break; case '8': addInstituteAfterSorting(&head, createInstitute()); cout << "Added new institute satisfying position..." << endl; break; case '9': saveToFile(head); cout << "Saved to text file..." << endl; break; case '0': exit(0); break; } if (t != '0') system("pause"); } delete head; }