Advertisement
topkedi

Untitled

Dec 14th, 2023
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.71 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. const int MAXSIZE = 1048576, SIZE = 10;
  4.  
  5. class MemoryCell {
  6. public:
  7.     MemoryCell() = default;
  8.  
  9.     MemoryCell(int size, int start, int end) {
  10.         if (size > MAXSIZE) {
  11.             barbieSize = MAXSIZE;
  12.         } else {
  13.             barbieSize = size;
  14.         }
  15.         Full = true;
  16.         cell = new char[barbieSize];
  17.         startBlock = start, endBlock = end;
  18.     }
  19.  
  20.     char *cell{};
  21.     int startBlock{}, endBlock{}, barbieSize{};
  22.     bool Full = false;
  23. };
  24.  
  25. class MemoryManager {
  26. public:
  27.     MemoryCell *array;
  28.     int StackTop;
  29.  
  30.     MemoryManager(int size = SIZE) {
  31.         array = new MemoryCell[size]();
  32.         StackTop = 0;
  33.         std::cout << "STACK CREATED" << std::endl;
  34.     }
  35.  
  36.     ~MemoryManager() = default;
  37.  
  38.     void Push(int size) {
  39.         UpdateTop();
  40.         int CellQuantity;
  41.         if (size % MAXSIZE == 0) {
  42.             CellQuantity = size / MAXSIZE;
  43.         } else {
  44.             CellQuantity = size / MAXSIZE + 1;
  45.         }
  46.  
  47.         if (StackTop + CellQuantity > SIZE) {
  48.             std::cout
  49.                 << "SORRY, STACK IS FULL. CLEAR OR DEFRAG IT TO PUSH SOMETHING"
  50.                 << std::endl;
  51.             return;
  52.         }
  53.         int startBlock = StackTop, endBlock = StackTop + CellQuantity - 1;
  54.         for (int i = startBlock; i <= endBlock; i = i + 1) {
  55.             array[i] = MemoryCell(size, startBlock, endBlock);
  56.             size = size - MAXSIZE;
  57.         }
  58.         StackTop = endBlock + 1;
  59.         std::cout << "INFORMATION ADDED" << std::endl << std::endl;
  60.     }
  61.  
  62.     void Print() {
  63.         for (int i = 0; i < StackTop; i = i + 1) {
  64.             std::cout << "Cell Number '" << i << "' Memory Volume '"
  65.                       << array[i].barbieSize << "' Begin Block '"
  66.                       << array[i].startBlock << "' End Block '"
  67.                       << array[i].endBlock << "' Fullness '" << array[i].Full
  68.                       << "'" << std::endl;
  69.         }
  70.         std::cout << std::endl;
  71.     }
  72.  
  73.     void UpdateTop () {
  74.         for (int i = StackTop-1; i >= 0; i = i - 1){
  75.             if (array[i].Full == 1) {
  76.                 StackTop = i+1;
  77.                 break;
  78.             }
  79.         }
  80.     }
  81.  
  82.     void DeleteMemory(int num) {
  83.         for (int i = 0; i <= StackTop; i = i + 1) {
  84.             if (array[i].startBlock <= num && array[i].endBlock >= num) {
  85.                 array[i].Full = false;
  86.                 array[i].barbieSize = 0;
  87.                 delete array[i].cell;
  88.             }
  89.         }
  90.     }
  91.  
  92.     void Defragmentation() {
  93.         for (int i = 0; i < StackTop; i = i + 1) {
  94.             if (!array[i].Full) {
  95.                 for (int j = i; j <= StackTop; j = j + 1) {
  96.                     array[j] = array[j + 1];
  97.                     array[j].startBlock = array[j].startBlock - 1;
  98.                     array[j].endBlock = array[j].endBlock - 1;
  99.                 }
  100.                 StackTop = StackTop - 1;
  101.                 i = i - 1;
  102.             }
  103.         }
  104.     }
  105. };
  106.  
  107. int main() {
  108.     auto *Memorymanager = new MemoryManager();
  109.     std::cout << "Hello, weclome to the best memory manager in the world))"
  110.               << std::endl;
  111.     while (true) {
  112.         std::cout << "Please, choose any option" << std::endl;
  113.         std::cout << "1)Add Information" << std::endl;
  114.         std::cout << "2)Print Information" << std::endl;
  115.         std::cout << "3)Delete Information" << std::endl;
  116.         std::cout << "4)Defragmentation" << std::endl;
  117.         std::cout << "Enter any other number to exit" << std::endl;
  118.         std::cout << " Number of your option: ";
  119.         int n;
  120.         std::cin >> n;
  121.         std::cout << std::endl;
  122.         switch (n) {
  123.             case 1:
  124.                 int mem;
  125.                 std::cout << "Choose how many bites you want to apply: ";
  126.                 std::cin >> mem;
  127.                 Memorymanager->Push(mem);
  128.                 break;
  129.             case 2:
  130.                 std::cout << "CURRENT STACK" << std::endl;
  131.                 Memorymanager->Print();
  132.                 std::cout << std::endl;
  133.                 break;
  134.             case 3:
  135.                 int cell;
  136.                 std::cout << "Choose cell to delete: ";
  137.                 std::cin >> cell;
  138.                 Memorymanager->DeleteMemory(cell);
  139.                 std::cout << "INFORMATION DELETED" << std::endl << std::endl;
  140.                 break;
  141.             case 4:
  142.                 Memorymanager->Defragmentation();
  143.                 std::cout << "DEFRAGMENTATION ENDED" << std::endl << std::endl;
  144.                 break;
  145.             default:
  146.                 delete Memorymanager;
  147.                 std::cout << "Memory is clear. See you later!" << std::endl;
  148.                 return 0;
  149.         }
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement