Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.27 KB | None | 0 0
  1. /*
  2.  
  3. COMP2123 Mini Group Project Members List
  4.  
  5. Name StudentID
  6. Chan Tik Shun 3035536553
  7. Lui Kin Ping 3035537363
  8.  
  9. */
  10.  
  11. #include <iostream>
  12. #include <stdlib.h> // for system()
  13. #include <string> // for storing, processing and validating command
  14. #include <ctime> // for clock_t, clock()
  15.  
  16. using namespace std;
  17.  
  18. struct history { // struct for each command
  19. string command;
  20. double runTime;
  21. history *next;
  22. };
  23.  
  24. void historyInsert(history *& h, const string & c, const double & r);
  25.  
  26. void printHistory(history *h, int displayed);
  27.  
  28. void copyHistory(history *source, history *&destin);
  29.  
  30. bool isSorted(history *h);
  31.  
  32. void sortHistory(history *&h);
  33.  
  34. string normalize(const string original_string) { // delete optional space and determine whether the string fits the format
  35. string temp_string;
  36. bool potential_sbu = true;
  37.  
  38. for(int i=0; i < original_string.size(); i++) { // check every character one by one
  39. if (original_string[i] != ' ') // omit the space character
  40. temp_string += ((char)(original_string[i])); // directly input the character
  41.  
  42. if (temp_string == "history" && potential_sbu) {
  43. if( ((original_string.size() - 1 - i) >= 6) && potential_sbu ) { // check length of remaining string after "(optional space)history" (prevent array index out of bound!!!)
  44. string remain_option = original_string.substr(i+1, original_string.size()-1-i); // remaining string
  45.  
  46. if(remain_option[0] != ' ' || remain_option[remain_option.size()-1] != ' ') // omit "(optional space)history(NO space)-sbu(optional space)", "(optional space)history(optional space)-sbu(NO space)"
  47. potential_sbu = false;
  48.  
  49. string temp_substring; // for storing remaining string without spaces
  50. for(int j=0; (j < remain_option.size() && potential_sbu); j++) {
  51. if (remain_option[j] != ' ') // omit the space character
  52. temp_substring += ((char)(remain_option[j])); // directly input the character
  53. }
  54.  
  55. if(temp_substring == "-sbu" && potential_sbu) // whether remaining string after "(optional space)history" is "(one or more space)-sbu(one or more space)"
  56. return "history -sbu ";
  57. } else {
  58. potential_sbu = false; // treat "(optional space)history(more than one space)-sbu(optional space)" as a normal command
  59. }
  60. }
  61. }
  62.  
  63. if (temp_string == "history" || temp_string == "exit")
  64. return temp_string; // history or exit command
  65.  
  66. return original_string; // return the original string as command and keep all the spaces
  67. }
  68.  
  69. int main() {
  70.  
  71. string command;
  72. history *listOfHistory = NULL;
  73.  
  74. while ( (cout << "tinyshell>") && (getline(cin, command)) ) { // store the user input into command (whole line)
  75.  
  76. string new_command = normalize(command); // remove any optional space and check whether it matches the format of built-in command
  77.  
  78. clock_t begin = clock(); // start time of the command
  79.  
  80. if(new_command == "history") {
  81. // display last 5 commands, later command first
  82. printHistory(listOfHistory, 1); // print first 5 history, or until the end
  83.  
  84. } else if (new_command == "history -sbu ") {
  85. // display slowest 5 commands, slower command first
  86. history *sortedHistory = NULL;
  87. copyHistory(listOfHistory, sortedHistory);
  88. sortHistory(sortedHistory);
  89. printHistory(sortedHistory, 1);
  90. delete sortedHistory;
  91. sortedHistory = NULL;
  92. } else if (new_command == "exit") {
  93. // terminate program
  94. exit(0);
  95. } else {
  96. // pass the command to the terminal
  97. system(command.c_str());
  98.  
  99. clock_t end = clock(); // end time of the command
  100. double runTime = double(end - begin) / CLOCKS_PER_SEC; // return total execution time
  101. historyInsert(listOfHistory, command, runTime); // update history
  102. }
  103. cout << endl; // break line
  104. }
  105. }
  106.  
  107. void historyInsert(history *& h, const string & c, const double & r) {
  108. history *newH = new history;
  109. newH -> command = c;
  110. newH -> runTime = r;
  111. newH -> next = h;
  112. h = newH;
  113. }
  114.  
  115. void printHistory(history *h, int displayed) {
  116. if ( (h == NULL) || (displayed > 5) )
  117. return;
  118. else {
  119. cout << h -> command << " " << h -> runTime << "s" << endl;
  120. printHistory(h->next, displayed+1);
  121. }
  122. }
  123.  
  124. void copyHistory(history *source, history *&destin){
  125. if (source == NULL)
  126. return;
  127. else{
  128. copyHistory(source->next, destin);
  129. historyInsert(destin, source->command, source->runTime);
  130. }
  131. }
  132.  
  133. bool isSorted(history *h) {
  134. if (h == NULL || h->next == NULL) {
  135. return true;
  136. } else if (h->runTime >= h->next->runTime){
  137. return isSorted(h->next);
  138. } else {
  139. return false;
  140. }
  141. }
  142.  
  143. void sortHistory(history *&h) {
  144. if (h == NULL || h->next == NULL)
  145. return;
  146. while (!isSorted(h)){
  147. if (h->runTime >= h->next->runTime){
  148. sortHistory(h->next);
  149. } else {
  150. history *tempHistory1 = new history;
  151. history *tempHistory2 = new history;
  152. tempHistory1->command = h->command;
  153. tempHistory1->runTime = h->runTime;
  154. tempHistory1->next = h->next->next;
  155. tempHistory2 = h->next;
  156. tempHistory2->next = tempHistory1;
  157. h = tempHistory2;
  158. sortHistory(h->next);
  159. }
  160. }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement