Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <fstream>
- using namespace std;
- struct _student {
- string frist_name;
- string last_name;
- int grade;
- _student *pNextStudent;
- };
- void InsertAfter (_student *head);
- void PrintAll (_student *head);
- void InsertBefore (_student *head);
- void FindElement (_student *head);
- void DeleteElement (_student *head);
- void WriteDB_toFile (_student *head, fstream *database, string FileName);
- void RebuildDB_fromFile (_student *head, fstream *database, string FileName);
- int main ()
- {
- _student *head = new _student;
- head->pNextStudent=NULL;
- int N=0, i=0, selection=0;
- fstream database;
- string FileName = "database.txt";
- while (selection != 8){
- cout << "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ";
- cout << "\nWhat do you wish to do today my lord?" << endl << endl;
- cout << "I want to:\n\n1) Add new students to existing database.\n2) I wish to print all current students from database.";
- cout << "\n3) I wish to Insert new student at specific place.\n4) I want to check if student exists in database.\n5) I want to delete student from database.";
- cout << "\n6) I want to write database to file.\n7) I want to rebuild database from file.\n8) exit" << endl;
- cin >> selection;
- switch(selection){
- case 1:
- cout << "\nHow many students do you wish to append? ";
- cin >> N;
- i = 0;
- do{
- InsertAfter(head);
- i++;
- }while(i<N);
- break;
- case 2:
- PrintAll(head);
- break;
- case 3:
- InsertBefore(head);
- break;
- case 4:
- FindElement(head);
- break;
- case 5:
- DeleteElement(head);
- break;
- case 6:
- WriteDB_toFile(head,&database,FileName);
- break;
- case 7:
- RebuildDB_fromFile(head,&database,FileName);
- break;
- case 8:
- exit(0);
- break;
- default:
- cout << "Wrong selection. Please try again!" << endl;
- break;
- }
- }
- getchar();
- return 0;
- }
- bool isNumber(string line) {
- return (atoi(line.c_str()));
- }
- void InsertAfter(_student *head){
- _student *counter=head;
- while(counter->pNextStudent!=NULL)
- counter=counter->pNextStudent;
- _student *newStudent = new _student;
- newStudent->pNextStudent=NULL;
- counter->pNextStudent=newStudent;
- cout << "\nType students first name: ";
- cin >> newStudent->frist_name;
- cout << "Type students last name: ";
- cin >> newStudent->last_name;
- cout << "Type students " << newStudent->frist_name << " " << newStudent->last_name << " grade: ";
- cin >> newStudent->grade;
- }
- void PrintAll (_student *head){
- _student * counter;
- counter=head->pNextStudent;
- while(counter!=NULL){
- cout << "\nStudents first name: " << counter->frist_name << endl << "Students last name: " << counter->last_name << endl;
- cout << "Students grade: " << counter->grade << endl;
- counter=counter->pNextStudent;
- };
- }
- void InsertBefore(_student *head)
- {
- _student *counter = head->pNextStudent;
- string first_name,last_name;
- cout << "\nEnter the first and last name of student, before you wish to add new student: ";
- cin >> first_name >> last_name;
- while (counter!=NULL)
- {
- if ((counter->pNextStudent->frist_name==first_name)&&(counter->pNextStudent->last_name==last_name))
- {
- _student * newStudent = new _student;
- cout << "\nType students first name: ";
- cin >> newStudent->frist_name;
- cout << "Type students last name: ";
- cin >> newStudent->last_name;
- cout << "Type students " << newStudent->frist_name << " " << newStudent->last_name << " grade: ";
- cin >> newStudent->grade;
- newStudent->pNextStudent=counter->pNextStudent;
- counter->pNextStudent=newStudent;
- break;
- }
- else if (counter->pNextStudent==NULL)
- cout << "Student not found!\nPlease try again" << endl;
- else
- counter=counter->pNextStudent;
- }
- }
- void FindElement (_student *head){
- _student *counter = head;
- string filter;
- vector <_student*> addresses;
- cout << "\nType students first name, last name or grade to list all students with selected filter:" << endl;
- cin >> filter;
- if(isNumber(filter))
- while(counter!=NULL){
- if (counter->grade==atoi(filter.c_str()))
- addresses.push_back(counter);
- counter=counter->pNextStudent;
- }
- else
- while (counter!=NULL){
- if((counter->frist_name==filter)||(counter->last_name==filter))
- addresses.push_back(counter);
- counter=counter->pNextStudent;
- }
- if(addresses.size()!=NULL)
- {
- cout << "\nFollowing students were found using filter: " << filter << endl << endl;
- for (int i=0; i<addresses.size(); i++)
- {
- cout << "Students first name: " << addresses[i]->frist_name << endl;
- cout << "Students last name: " << addresses[i]->last_name << endl;
- cout << "Students grade: " << addresses[i]->grade << endl << endl;
- }
- }
- else
- cout << "\nNo students were found in database using following filter: " << filter << endl << endl;
- }
- void DeleteElement (_student *head){
- _student * counter = head;
- string first_name, last_name;
- cout << "Enter the first and last name of student you wish to remove from database: ";
- cin >> first_name >> last_name;
- while(counter->pNextStudent!=NULL)
- {
- if((first_name==counter->pNextStudent->frist_name)&&(last_name==counter->pNextStudent->last_name))
- {
- _student * toRemove = counter->pNextStudent;
- counter->pNextStudent=counter->pNextStudent->pNextStudent;
- delete toRemove;
- }
- if (counter->pNextStudent!=NULL)
- counter=counter->pNextStudent;
- }
- }
- void WriteDB_toFile (_student *head, fstream *database,string FileName){
- _student *current = head->pNextStudent;
- //remove(FileName);
- (*database).open(FileName.c_str());
- (*database) << "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *" << endl;
- (*database) << "*\t\tFirst name\t\t|\t\tLast name\t\t|\t\tGrade\t\t*" << endl;
- (*database) << "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *" << endl;
- while (current!=NULL)
- {
- if (current->frist_name.size() <= 3)
- (*database) << "*\t" << current->frist_name << "\t\t\t\t\t|\t";
- else if (current->frist_name.size() <= 7)
- (*database) << "*\t" << current->frist_name << "\t\t\t\t|\t";
- else if (current->frist_name.size() >= 12)
- (*database) << "*\t" << current->frist_name << "\t\t|\t";
- else
- (*database) << "*\t" << current->frist_name << "\t\t\t|\t";
- if (current->last_name.size() <= 3)
- (*database) << current->last_name << "\t\t\t\t\t|\t\t";
- else if (current->last_name.size() <= 7)
- (*database) << current->last_name << "\t\t\t\t|\t\t";
- else if (current->last_name.size() >= 12)
- (*database) << current->last_name << "\t\t|\t\t";
- else
- (*database) << current->last_name << "\t\t\t|\t\t";
- if (current->grade < 1000)
- (*database) << current->grade << "\t\t\t*" << endl;
- else if (current->grade > 9999999)
- (*database) << current->grade << "\t*" << endl;
- else
- (*database) << current->grade << "\t\t*" << endl;
- current=current->pNextStudent;
- }
- (*database) << "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *";
- (*database).close();
- }
- void RebuildDB_fromFile (_student *head, fstream *database, string FileName){
- _student *counter=head;
- (*database).open(FileName.c_str());
- string temp;
- string clean;
- getline((*database),temp,'\n');
- getline((*database),temp,'\n');
- getline((*database),temp,'\n');
- while ((*database).good())
- {
- _student *newStudent = new _student;
- while (counter->pNextStudent!=NULL)
- counter = counter->pNextStudent;
- getline((*database),temp,'|');
- for (int i=0; i<temp.size(); i++)
- {
- if ((temp[i]!='\t') && (temp[i]!='*'))
- clean.push_back(temp[i]);
- }
- newStudent->frist_name=clean;
- clean.clear();
- getline((*database),temp,'|');
- for (int i=0; i<temp.size(); i++)
- {
- if ((temp[i]!='\t') && (temp[i]!='*'))
- clean.push_back(temp[i]);
- }
- newStudent->last_name=clean;
- clean.clear();
- getline((*database),temp,'\n');
- for (int i=0; i<temp.size(); i++)
- {
- if ((temp[i]!='\t') && (temp[i]!='*'))
- clean.push_back(temp[i]);
- }
- newStudent->grade=atoi(clean.c_str());
- clean.clear();
- counter->pNextStudent=newStudent;
- newStudent->pNextStudent=NULL;
- }
- counter=head;
- while(counter->pNextStudent->pNextStudent!=NULL)
- {
- counter=counter->pNextStudent;
- }
- _student * toRemove = counter->pNextStudent;
- delete toRemove;
- counter->pNextStudent=NULL;
- (*database).close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement