Advertisement
Josif_tepe

Untitled

Apr 4th, 2025
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.77 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. const int maxn = 1000;
  4.  
  5. struct stackk {
  6.     int niza[maxn];
  7.     int idx;
  8.    
  9.     void init() {
  10.         idx = -1;
  11.     }
  12.    
  13.     void push(int x) {
  14.         if(idx + 1 >= maxn) {
  15.             cout << "NEMA DOVOLNO KAPACITET" << endl;
  16.             return;
  17.         }
  18.         idx++;
  19.         niza[idx] = x;
  20.     }
  21.    
  22.     int pop() {
  23.         if(idx == -1) {
  24.             cout << "NEMA NITU EDEN ELEMENT VO STEKOT" << endl;
  25.             return -1;
  26.         }
  27.         int result = niza[idx];
  28.         idx--;
  29.         return result;
  30.     }
  31.    
  32.     int top() {
  33.         if(idx == -1) {
  34.             cout << "NEMA NITU EDEN ELEMENT VO STEKOT" << endl;
  35.             return -1;
  36.         }
  37.         return niza[idx];
  38.     }
  39.    
  40.     int size() {
  41.         return idx + 1;
  42.     }
  43.    
  44.     int isEmpty() {
  45.         if(idx == -1) {
  46.             return 1;
  47.         }
  48.         else {
  49.             return 0;
  50.         }
  51.     }
  52. };
  53.  
  54. void promeni(stackk & m1) {
  55.     stackk m2;
  56.     m2.init();
  57.    
  58.     while(m1.isEmpty() == false) {
  59.         m2.push(m1.pop());
  60.     }
  61.    
  62.     stackk nad;
  63.     nad.init();
  64.    
  65.     while(m2.isEmpty() == false) {
  66.         int c = m2.pop();
  67.        
  68.         if(nad.isEmpty() == false) {
  69.             if(c % nad.top() == 0) {
  70.                 m1.push(c / nad.top());
  71.             }
  72.         }
  73.         nad.push(c);
  74.        
  75.         if(m2.isEmpty() == false) {
  76.             if(c % m2.top() == 0) {
  77.                 m1.push(c / m2.top());
  78.             }
  79.         }
  80.     }
  81. }
  82. int main() {
  83.     stackk m1;
  84.  
  85.     m1.init();
  86.  
  87.     m1.push(2);
  88.     m1.push(10);
  89.     m1.push(5);
  90.     m1.push(2);
  91.     m1.push(1);
  92.  
  93.     promeni(m1);
  94.  
  95.     while (!m1.isEmpty()) {
  96.         cout << m1.pop() << endl;
  97.     }
  98.  
  99.     return 0;
  100. }
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement