Jenifa

windows_recent_files_feature.cpp

Mar 10th, 2022
1,386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.83 KB | None | 0 0
  1.  
  2. /**
  3. ********************
  4. *   Windows Recent Files feature is an implementation of a Stack but with an added functionality
  5. *   You push items to the stack (strings in this implementation) and it operates on a FILO or LIFO
  6. *   basis for the pop operation
  7. *   As an added feature, when an item that already exists is pushed into the stack,
  8. *   it becomes the most recent item (last item) and the stack is rearranged accordingly.
  9. *   The stack has a size that can be manipulated by the user and it implements these functions
  10. *
  11. *   getMostRecentItem();
  12. *   pushItem();
  13. *
  14. *   For this implementation, I will use std::deque
  15. **********************************
  16. */
  17.  
  18.  
  19. #include <iostream>
  20. #include <deque>
  21. #include <string_view>
  22.  
  23. class RecentStack {
  24.     int count;
  25.     public:
  26.         std::deque<std::string> recentStack;
  27.         int size;
  28.        
  29.        
  30.         //Default Constructor
  31.         RecentStack() {
  32.             size = 5;
  33.             count = size;
  34.         }
  35.  
  36.         //Parameterized constructor
  37.         RecentStack(int size) : size(size), count(size) {}
  38.  
  39.  
  40.         //Returns the most recent element in the Recent Stack
  41.         std::string getMostRecentItem() const {
  42.             return recentStack.back();
  43.         }
  44.  
  45.  
  46.         //Adds element to the Recent Stack
  47.         void pushItem( std::string item) {
  48.             //search for match
  49.             for( auto it = recentStack.begin(); it != recentStack.end(); ++it) {
  50.                 if(*it == item) {
  51.                     std::cout << "Item Exists! It is added to the Front\n";
  52.                     recentStack.push_back(item);
  53.                     recentStack.erase(it);
  54.  
  55.                     return;
  56.                 }
  57.             }
  58.  
  59.             //No match
  60.             if(count == -1) { //RecentStack is full
  61.                 recentStack.push_back(item);
  62.                 std::cout << "Oops " << recentStack.front() << " fell off\n";
  63.                 recentStack.pop_front();
  64.  
  65.             } else {                            
  66.                 //push as normal and decrement counter
  67.                 recentStack.push_back(item);
  68.                 --count;
  69.             }
  70.            
  71.             return;      
  72.         }
  73.  
  74.  
  75.         //Utility Function to print Recent Stack
  76.         void printRecentList() {
  77.             for(auto i = recentStack.rbegin(); i != recentStack.rend(); ++i ) {
  78.                 std::cout << *i << "\n";
  79.             }
  80.         }
  81.  
  82. };
  83.  
  84.  
  85. int main() {
  86.     int option;
  87.     RecentStack rStack(5);
  88.    
  89.     do {
  90.         std::cout << "What operation do you want to perform? \n";
  91.         std::cout << "0. Exit \n";
  92.         std::cout << "1. Add items to the stack \n";  
  93.         std::cout << "2. View most Recent Item \n";
  94.         std::cout << "3. Print Recent List \n";  
  95.         std::cout << "4. Clear the Screen \n" <<std::endl;  
  96.  
  97.  
  98.         std::cout << "Option: ";
  99.         std::cin >> option;
  100.  
  101.         std::string str;
  102.  
  103.         switch (option){
  104.             case 0:
  105.                 break;
  106.            
  107.             case 1:
  108.                 std::cout << "Input String to Add to stack: ";
  109.                 std::cin >> str;
  110.  
  111.                 rStack.pushItem(str);
  112.                 std::cout << "ADDED!\n";
  113.  
  114.                 break;
  115.  
  116.             case 2:
  117.                 std::cout << "The Most Recent Item is: " << rStack.getMostRecentItem();
  118.                 std::cout << std::endl;
  119.  
  120.                 break;
  121.  
  122.             case 3:
  123.                 std::cout << "The Recent Stack is Composed of: " << std::endl;
  124.                 rStack.printRecentList();
  125.  
  126.                 std::cout << "\n\n\n";
  127.  
  128.                 break;
  129.  
  130.             case 4:
  131.                 system("cls");
  132.                 break;
  133.  
  134.             default:
  135.                 std::cout << "Enter proper option number" << std::endl;
  136.                 break;
  137.  
  138.         }  
  139.  
  140.     } while (option != 0);
  141.  
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment