Advertisement
dartwlad

Untitled

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