Advertisement
Josif_tepe

Untitled

Mar 9th, 2025
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.13 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. const int max_size = 1000;
  4. struct stek {
  5.     int niza[max_size];
  6.     int idx;
  7.    
  8.     void init() {
  9.         idx = -1;
  10.     }
  11.     bool isEmpty() {
  12.         if(idx == -1) {
  13.             return true;
  14.         }
  15.         else {
  16.             return false;
  17.         }
  18.     }
  19.    
  20.     bool isFull() {
  21.         if(idx == max_size - 1) {
  22.             return true;
  23.         }
  24.         else {
  25.             return false;
  26.         }
  27.     }
  28.    
  29.     void push(int x) {
  30.         if(isFull() == true) {
  31.             cout << "nema poveke prostor vo stekot" << endl;
  32.             exit(-1);
  33.         }
  34.         idx++;
  35.         niza[idx] = x;
  36.     }
  37.    
  38.     int peek() {
  39.         if(isEmpty() == true) {
  40.             cout << "nema element vo stekot" << endl;
  41.             exit(-1);
  42.         }
  43.         return niza[idx];
  44.     }
  45.    
  46.     int pop() {
  47.         if(isEmpty() == true) {
  48.             cout << "nema element vo stekot" << endl;
  49.             exit(-1);
  50.         }
  51.         int result = niza[idx];
  52.         idx--;
  53.         return result;
  54.     }
  55.     int size() {
  56.         return idx + 1;
  57.     }
  58.    
  59. };
  60.  
  61. void func(stek &m)
  62. {
  63.     stek s1, s2;
  64.     s1.init();
  65.     s2.init();
  66.    
  67.     int sz = m.size() / 2;
  68.     while(m.isEmpty() == false) {
  69.         if(m.size() > sz) {
  70.             s1.push(m.pop());
  71.         }
  72.         else {
  73.             s2.push(m.pop());
  74.         }
  75.     }
  76.    
  77.     while(s1.isEmpty() == false) {
  78.         m.push(s1.pop());
  79.     }
  80.    
  81.    
  82.     int broj = 0;
  83.     while(m.isEmpty() == false) {
  84.         int c1 = m.pop();
  85.         int c2 = s2.pop();
  86.        
  87.         int zbir = c1 + c2;
  88.         if(zbir > 9) {
  89.             zbir = 9;
  90.         }
  91.         broj = (broj * 10) + zbir;
  92.         s1.push(zbir);
  93.        
  94.     }
  95.    
  96.     while(s1.isEmpty() == false ){
  97.         broj = (broj * 10) + s1.pop();
  98.     }
  99.     cout << broj << endl;
  100.    
  101. }
  102. int main()
  103. {
  104.     stek s;
  105.     int no, element, c;
  106.     s.init();
  107.     cout << "Vnesete koj broj da se proveri:";
  108.     cin >> no;
  109.     while(no != 0){
  110.         element = no % 10;
  111.         no /= 10;
  112.         s.push(element);
  113.     }
  114.     func(s);
  115.    
  116. }
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement