Advertisement
dartwlad

theory_lab2

Apr 10th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. #include <iomanip>
  5.  
  6. using namespace std;
  7.  
  8. struct Node {
  9.     int data;
  10.     Node* next;
  11.     Node() : data(0), next(nullptr) {}
  12.     explicit Node(int data): data(data), next(nullptr) {}
  13. };
  14.  
  15. class Stack {
  16.     static const int STACK_LENGTH = 10;
  17.     Node* stack;
  18.     int top;
  19.     unsigned int size = 0;
  20.  
  21. public:
  22.     Stack();
  23.     ~Stack();
  24.     void push(int);
  25.     int pop();
  26.     unsigned int getSize() const;
  27.     bool isOrdered() const;
  28.     void pushOrder(int);
  29.     string toString() const;
  30.     Stack operator+(Stack);
  31.     void stackSort() {
  32.         for (int i = 0; i < STACK_LENGTH; i++){
  33.             for (int j = i+1; j < STACK_LENGTH; j++){
  34.                 if (stack[i].data < stack[j].data)
  35.                 {
  36.                     int tmp = stack[i].data;
  37.                     stack[i] = stack[j];
  38.                     stack[j].data = tmp;
  39.                 }
  40.             }
  41.         }
  42.     }
  43. };
  44.  
  45. Stack::Stack() : top(-1) {
  46.     stack = new Node[STACK_LENGTH];
  47. }
  48.  
  49. Stack::~Stack() {
  50.     delete stack;
  51. }
  52.  
  53. void Stack::push(int i) {
  54.     top++;
  55.     size++;
  56.     stack[top] = Node(i);
  57. }
  58.  
  59. int Stack::pop() {
  60.     if (top == -1) {
  61.         cout << "Stack is empty!" << endl;
  62.         return NULL;
  63.     }
  64.  
  65.     int data = stack[top].data;
  66.     //stack[top] = NULL;
  67.     top--;
  68.     size--;
  69.  
  70.     return data;
  71. }
  72.  
  73. unsigned Stack::getSize() const {
  74.     return size;
  75. }
  76.  
  77. bool Stack::isOrdered() const {
  78.     for (int i = 0; i < size; i++) {
  79.         if (i != 0) {
  80.             if (stack[i].data >= stack[i - 1].data) {
  81.                 return false;
  82.             }
  83.         }
  84.     }
  85.     return true;
  86. }
  87.  
  88. void Stack::pushOrder(int input) {
  89.     if(size != STACK_LENGTH) {
  90.         if(isOrdered()) {
  91.             int i=0;
  92.             while (i < size && input < stack[i].data) i++;
  93.             for (int j = size; j > i; j--) {
  94.                 stack[j] = stack[j - 1];
  95.             }
  96.             stack[i].data = input;
  97.             size++;
  98.             top++;
  99.         } else {
  100.             cout << "Stack is not ordered" << endl;
  101.         }
  102.     } else {
  103.         cout << "Stack is full" << endl;
  104.     }
  105. }
  106.  
  107. string Stack::toString() const {
  108.     stringstream ss;
  109.  
  110.     for (int i = 0; i < size; i++) {
  111.         ss << setw(2) << stack[i].data;
  112.     }
  113.  
  114.     ss << endl;
  115.  
  116.     return ss.str();
  117. }
  118.  
  119. Stack Stack::operator+(Stack stackToMerge) {
  120.     Stack resultStack;
  121.  
  122.     for (int i = 0; i < STACK_LENGTH; i++) {
  123.         if (i % 2 == 0) {
  124.             if (size > 0) {
  125.                 resultStack.push(pop());
  126.             } else if (stackToMerge.getSize() > 0) {
  127.                 resultStack.push(stackToMerge.pop());
  128.             }
  129.         } else {
  130.             if (stackToMerge.getSize() > 0) {
  131.                 resultStack.push(stackToMerge.pop());
  132.             } else if (size > 0) {
  133.                 resultStack.push(pop());
  134.             }
  135.         }
  136.     }
  137.  
  138.     return resultStack;
  139. }
  140.  
  141. int main() {
  142.     Stack firstStack;
  143.     Stack secondStack;
  144.  
  145.     firstStack.push(1);
  146.     firstStack.push(3);
  147.     firstStack.push(5);
  148.     firstStack.push(1);
  149.     firstStack.push(7);
  150.     firstStack.push(9);
  151.  
  152.     secondStack.push(2);
  153.     secondStack.push(4);
  154.     secondStack.push(6);
  155.     secondStack.push(8);
  156.  
  157.     cout << "Merge" << endl;
  158.  
  159.     cout << "First Stack:" << firstStack.toString();
  160.     cout << "Second Stack:" << secondStack.toString();
  161.  
  162.     Stack resultStack = firstStack + secondStack;
  163.  
  164.     cout << "Result Stack:" << resultStack.toString();
  165.  
  166.     cout << "Pop" << endl;
  167.  
  168.     cout << "pop two last elements" << endl;
  169.  
  170.     resultStack.pop();
  171.     resultStack.pop();
  172.  
  173.     cout << "Stack: " << resultStack.toString();
  174.  
  175.     cout << "Size" << endl;
  176.  
  177.     cout << "size: " << resultStack.getSize() << endl;
  178.  
  179.     cout << "Sort" << endl;
  180.     resultStack.stackSort();
  181.     cout << "sorted: " << resultStack.toString() << endl;
  182.  
  183.     cout << "isOrdered?" << endl;
  184.  
  185.     cout << "isOrdered: " << (resultStack.isOrdered() == 1 ? "true" : "false") << endl;
  186.  
  187.     cout << "pushOrder" << endl;
  188.  
  189.     cout << "pushOrder 10" << endl;
  190.  
  191.     resultStack.pushOrder(10);
  192.  
  193.     cout << "Stack: " << resultStack.toString();
  194.  
  195.     return 0;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement