Advertisement
Cokaric

Singly-linked list v0.3 - Data structures, exercise

Oct 6th, 2013
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <fstream>
  5. using namespace std;
  6.  
  7. struct _student {
  8.     string frist_name;
  9.     string last_name;
  10.     int grade;
  11.     _student *pNextStudent;
  12. };
  13.  
  14. void InsertAfter (_student *head);
  15. void PrintAll (_student *head);
  16. void InsertBefore (_student *head);
  17. void FindElement (_student *head);
  18. void DeleteElement (_student *head);
  19. void WriteDB_toFile (_student *head, fstream *database, string FileName);
  20. void RebuildDB_fromFile (_student *head, fstream *database, string FileName);
  21.  
  22. int main ()
  23. {
  24.     _student *head = new _student;
  25.     head->pNextStudent=NULL;
  26.     int N=0, i=0, selection=0;
  27.     fstream database;
  28.     string FileName = "database.txt";
  29.  
  30.     while (selection != 8){
  31.         cout << "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ";
  32.         cout << "\nWhat do you wish to do today my lord?" << endl << endl;
  33.         cout << "I want to:\n\n1) Add new students to existing database.\n2) I wish to print all current students from database.";
  34.         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.";
  35.         cout << "\n6) I want to write database to file.\n7) I want to rebuild database from file.\n8) exit" << endl;
  36.         cin >> selection;
  37.  
  38.         switch(selection){
  39.         case 1:
  40.             cout << "\nHow many students do you wish to append? ";
  41.             cin >> N;
  42.             i = 0;
  43.             do{
  44.                 InsertAfter(head);
  45.                 i++;
  46.             }while(i<N);
  47.             break;
  48.  
  49.         case 2:
  50.             PrintAll(head);
  51.             break;
  52.        
  53.         case 3:
  54.             InsertBefore(head);
  55.             break;
  56.        
  57.         case 4:
  58.             FindElement(head);
  59.             break;
  60.  
  61.         case 5:
  62.             DeleteElement(head);
  63.             break;
  64.  
  65.         case 6:
  66.             WriteDB_toFile(head,&database,FileName);
  67.             break;
  68.    
  69.         case 7:
  70.             RebuildDB_fromFile(head,&database,FileName);
  71.             break;
  72.  
  73.         case 8:
  74.             exit(0);
  75.             break;
  76.  
  77.         default:
  78.             cout << "Wrong selection. Please try again!" << endl;
  79.             break;
  80.         }
  81.     }
  82.  
  83.     getchar();
  84.     return 0;
  85. }
  86.  
  87. bool isNumber(string line) {
  88.     return (atoi(line.c_str()));
  89. }
  90.  
  91. void InsertAfter(_student *head){
  92.     _student *counter=head;
  93.    
  94.     while(counter->pNextStudent!=NULL)
  95.         counter=counter->pNextStudent;
  96.  
  97.     _student *newStudent = new _student;
  98.     newStudent->pNextStudent=NULL;
  99.     counter->pNextStudent=newStudent;
  100.  
  101.     cout << "\nType students first name: ";
  102.     cin >> newStudent->frist_name;
  103.  
  104.     cout << "Type students last name: ";
  105.     cin >> newStudent->last_name;
  106.  
  107.     cout << "Type students " << newStudent->frist_name << " " << newStudent->last_name << " grade: ";
  108.     cin >> newStudent->grade;
  109. }
  110.  
  111. void PrintAll (_student *head){
  112.     _student * counter;
  113.     counter=head->pNextStudent;
  114.     while(counter!=NULL){
  115.         cout << "\nStudents first name: " << counter->frist_name << endl << "Students last name: " << counter->last_name << endl;
  116.         cout << "Students grade: " << counter->grade << endl;
  117.         counter=counter->pNextStudent;
  118.     };
  119. }
  120.  
  121. void InsertBefore(_student *head)
  122. {
  123.     _student *counter = head->pNextStudent;
  124.  
  125.     string first_name,last_name;
  126.     cout << "\nEnter the first and last name of student, before you wish to add new student: ";
  127.     cin >> first_name >> last_name;
  128.  
  129.     while (counter!=NULL)
  130.     {
  131.         if ((counter->pNextStudent->frist_name==first_name)&&(counter->pNextStudent->last_name==last_name))
  132.         {
  133.             _student * newStudent = new _student;
  134.  
  135.             cout << "\nType students first name: ";
  136.             cin >> newStudent->frist_name;
  137.  
  138.             cout << "Type students last name: ";
  139.             cin >> newStudent->last_name;
  140.  
  141.             cout << "Type students " << newStudent->frist_name << " " << newStudent->last_name << " grade: ";
  142.             cin >> newStudent->grade;
  143.  
  144.            
  145.             newStudent->pNextStudent=counter->pNextStudent;
  146.             counter->pNextStudent=newStudent;
  147.             break;
  148.         }
  149.         else if (counter->pNextStudent==NULL)
  150.             cout << "Student not found!\nPlease try again" << endl;
  151.  
  152.         else
  153.             counter=counter->pNextStudent;
  154.     }
  155. }
  156.  
  157. void FindElement (_student *head){
  158.     _student *counter = head;
  159.     string filter;
  160.     vector <_student*> addresses;
  161.  
  162.     cout << "\nType students first name, last name or grade to list all students with selected filter:" << endl;
  163.     cin >> filter;
  164.  
  165.     if(isNumber(filter))
  166.         while(counter!=NULL){
  167.             if (counter->grade==atoi(filter.c_str()))
  168.                 addresses.push_back(counter);
  169.             counter=counter->pNextStudent;
  170.         }
  171.     else
  172.         while (counter!=NULL){
  173.             if((counter->frist_name==filter)||(counter->last_name==filter))
  174.                 addresses.push_back(counter);
  175.             counter=counter->pNextStudent;
  176.         }
  177.        
  178.         if(addresses.size()!=NULL)
  179.         {
  180.             cout << "\nFollowing students were found using filter: " << filter << endl << endl;
  181.             for (int i=0; i<addresses.size(); i++)
  182.             {
  183.                 cout << "Students first name: " << addresses[i]->frist_name << endl;
  184.                 cout << "Students last name: " << addresses[i]->last_name << endl;
  185.                 cout << "Students grade: " << addresses[i]->grade << endl << endl;
  186.             }
  187.         }
  188.         else
  189.             cout << "\nNo students were found in database using following filter: " << filter << endl << endl;
  190. }
  191.  
  192. void DeleteElement (_student *head){
  193.     _student * counter = head;
  194.     string first_name, last_name;
  195.  
  196.     cout << "Enter the first and last name of student you wish to remove from database: ";
  197.     cin >> first_name >> last_name;
  198.  
  199.     while(counter->pNextStudent!=NULL)
  200.     {
  201.         if((first_name==counter->pNextStudent->frist_name)&&(last_name==counter->pNextStudent->last_name))
  202.         {
  203.             _student * toRemove = counter->pNextStudent;
  204.             counter->pNextStudent=counter->pNextStudent->pNextStudent;
  205.             delete toRemove;
  206.         }
  207.         if (counter->pNextStudent!=NULL)
  208.             counter=counter->pNextStudent;
  209.     }
  210. }
  211.  
  212. void WriteDB_toFile (_student *head, fstream *database,string FileName){
  213.     _student *current = head->pNextStudent;
  214.     //remove(FileName);
  215.     (*database).open(FileName.c_str());
  216.  
  217.     (*database) << "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *" << endl;
  218.     (*database) << "*\t\tFirst name\t\t|\t\tLast name\t\t|\t\tGrade\t\t*" << endl;
  219.     (*database) << "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *" << endl;
  220.     while (current!=NULL)
  221.     {
  222.         if (current->frist_name.size() <= 3)
  223.             (*database) << "*\t" << current->frist_name << "\t\t\t\t\t|\t";
  224.         else if (current->frist_name.size() <= 7)
  225.             (*database) << "*\t" << current->frist_name << "\t\t\t\t|\t";
  226.         else if (current->frist_name.size() >= 12)
  227.             (*database) << "*\t" << current->frist_name << "\t\t|\t";
  228.         else
  229.             (*database) << "*\t" << current->frist_name << "\t\t\t|\t";
  230.  
  231.         if (current->last_name.size() <= 3)
  232.             (*database) << current->last_name << "\t\t\t\t\t|\t\t";
  233.         else if (current->last_name.size() <= 7)
  234.             (*database) << current->last_name << "\t\t\t\t|\t\t";
  235.         else if (current->last_name.size() >= 12)
  236.             (*database) << current->last_name << "\t\t|\t\t";
  237.         else
  238.             (*database) << current->last_name << "\t\t\t|\t\t";
  239.  
  240.         if (current->grade < 1000)
  241.             (*database) << current->grade << "\t\t\t*" << endl;
  242.         else if (current->grade > 9999999)
  243.             (*database) << current->grade << "\t*" << endl;
  244.         else
  245.             (*database) << current->grade << "\t\t*" << endl;
  246.         current=current->pNextStudent;
  247.     }
  248.     (*database) << "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *";
  249.     (*database).close();
  250. }
  251.  
  252. void RebuildDB_fromFile (_student *head, fstream *database, string FileName){
  253.     _student *counter=head;
  254.     (*database).open(FileName.c_str());
  255.     string temp;
  256.     string clean;
  257.  
  258.     getline((*database),temp,'\n');
  259.     getline((*database),temp,'\n');
  260.     getline((*database),temp,'\n');
  261.  
  262.     while ((*database).good())
  263.     {
  264.         _student *newStudent = new _student;
  265.        
  266.         while (counter->pNextStudent!=NULL)
  267.             counter = counter->pNextStudent;
  268.  
  269.         getline((*database),temp,'|');
  270.         for (int i=0; i<temp.size(); i++)
  271.         {
  272.             if ((temp[i]!='\t') && (temp[i]!='*'))
  273.                 clean.push_back(temp[i]);
  274.         }
  275.         newStudent->frist_name=clean;
  276.         clean.clear();
  277.  
  278.         getline((*database),temp,'|');
  279.         for (int i=0; i<temp.size(); i++)
  280.         {
  281.             if ((temp[i]!='\t') && (temp[i]!='*'))
  282.                 clean.push_back(temp[i]);
  283.         }
  284.         newStudent->last_name=clean;
  285.         clean.clear();
  286.  
  287.         getline((*database),temp,'\n');
  288.         for (int i=0; i<temp.size(); i++)
  289.         {
  290.             if ((temp[i]!='\t') && (temp[i]!='*'))
  291.                 clean.push_back(temp[i]);
  292.         }
  293.         newStudent->grade=atoi(clean.c_str());
  294.         clean.clear();
  295.  
  296.         counter->pNextStudent=newStudent;
  297.         newStudent->pNextStudent=NULL;
  298.     }
  299.     counter=head;
  300.     while(counter->pNextStudent->pNextStudent!=NULL)
  301.     {
  302.         counter=counter->pNextStudent;
  303.     }
  304.     _student * toRemove = counter->pNextStudent;
  305.     delete toRemove;
  306.     counter->pNextStudent=NULL;
  307.     (*database).close();
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement