Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <stdlib.h>
- #include <time.h>
- #define VARIANT 2
- #define MAX_SIZE (VARIANT * 5 + 50)
- using namespace std;
- struct QUEUE {
- int headi = 0;
- int size = 0;
- int data[MAX_SIZE];
- };
- int getRandom();
- void qstore(QUEUE* queue, int d);
- int qretrieve(QUEUE* queue);
- void print(QUEUE* queue, bool prime = false);
- bool isPrime(int d);
- int main() {
- srand(time(NULL));
- QUEUE queue, *pqueue = &queue;
- for (int i = 0; i < MAX_SIZE; i++) {
- qstore(pqueue, getRandom());
- }
- print(pqueue);
- print(pqueue, true);
- return 0;
- }
- int getRandom() {
- return rand() % 100 + 1;
- }
- void qstore(QUEUE* queue, int d) {
- if (queue->size == MAX_SIZE) {
- cout << "Queue if full!" << endl;
- return;
- }
- int taili = (queue->headi + queue->size) % MAX_SIZE;
- (queue->size)++;
- queue->data[taili] = d;
- return;
- }
- int qretrieve(QUEUE* queue) {
- if (queue->size == 0) {
- cout << "Queue is empty!" << endl;
- return NULL;
- }
- int taili = (queue->headi + queue->size) % MAX_SIZE;
- queue->size--;
- queue->headi = (queue->headi + 1) % MAX_SIZE;
- return queue->data[taili];
- }
- void print(QUEUE* queue, bool prime) {
- if (prime == false) {
- cout << "Queue: ";
- } else {
- cout << "Queue (only primes): ";
- }
- for (int i = 0; i < queue->size; i++) {
- int elemi = (queue->headi + i) % MAX_SIZE;
- if (prime == true && isPrime(queue->data[elemi]) || prime == false) {
- cout << queue->data[elemi] << " ";
- }
- }
- cout << endl << endl;
- return;
- }
- bool isPrime(int d) {
- if (d == 1) {
- return false;
- }
- for (int i = 2; i <= sqrt(d); i++) {
- if (d % i == 0) {
- return false;
- }
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment