Vladislav_Bezruk

Untitled

Oct 12th, 2021
760
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. #define VARIANT 2
  7. #define MAX_SIZE (VARIANT * 5 + 50)
  8.  
  9. using namespace std;
  10.  
  11. struct QUEUE {
  12.     int headi = 0;
  13.     int size = 0;
  14.     int data[MAX_SIZE];
  15. };
  16.  
  17. int getRandom();
  18.  
  19. void qstore(QUEUE* queue, int d);
  20.  
  21. int qretrieve(QUEUE* queue);
  22.  
  23. void print(QUEUE* queue, bool prime = false);
  24.  
  25. bool isPrime(int d);
  26.  
  27. int main() {
  28.    
  29.     srand(time(NULL));
  30.  
  31.     QUEUE queue, *pqueue = &queue;
  32.    
  33.     for (int i = 0; i < MAX_SIZE; i++) {
  34.         qstore(pqueue, getRandom());
  35.     }
  36.    
  37.     print(pqueue);
  38.    
  39.     print(pqueue, true);
  40.    
  41.     return 0;
  42. }
  43.  
  44. int getRandom() {
  45.     return rand() % 100 + 1;
  46. }
  47.  
  48. void qstore(QUEUE* queue, int d) {
  49.     if (queue->size == MAX_SIZE) {
  50.         cout << "Queue if full!" << endl;
  51.        
  52.         return;
  53.     }
  54.    
  55.     int taili = (queue->headi + queue->size) % MAX_SIZE;
  56.    
  57.     (queue->size)++;
  58.    
  59.     queue->data[taili] = d;
  60.    
  61.     return;
  62. }
  63.  
  64. int qretrieve(QUEUE* queue) {
  65.     if (queue->size == 0) {
  66.         cout << "Queue is empty!" << endl;
  67.        
  68.         return NULL;
  69.     }
  70.        
  71.     int taili = (queue->headi + queue->size) % MAX_SIZE;
  72.    
  73.     queue->size--;
  74.    
  75.     queue->headi = (queue->headi + 1) % MAX_SIZE;
  76.    
  77.     return queue->data[taili];
  78. }
  79.  
  80. void print(QUEUE* queue, bool prime) {
  81.    
  82.     if (prime == false) {
  83.         cout << "Queue: ";
  84.     } else {
  85.         cout << "Queue (only primes): ";
  86.     }
  87.    
  88.     for (int i = 0; i < queue->size; i++) {
  89.         int elemi = (queue->headi + i) % MAX_SIZE;
  90.        
  91.         if (prime == true && isPrime(queue->data[elemi]) || prime == false) {
  92.             cout << queue->data[elemi] << " ";
  93.         }  
  94.     }
  95.    
  96.     cout << endl << endl;
  97.    
  98.     return;
  99. }
  100.  
  101. bool isPrime(int d) {
  102.     if (d == 1) {
  103.         return false;
  104.     }
  105.    
  106.     for (int i = 2; i <= sqrt(d); i++) {
  107.         if (d % i == 0) {
  108.             return false;
  109.         }
  110.     }
  111.    
  112.     return true;
  113. }
  114.  
Advertisement
Add Comment
Please, Sign In to add comment