Advertisement
Guest User

Untitled

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