Добавил:
vvrstcnho
Рад, если кому-то помог
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабораторные работы С++ (для ИВТ) / Готовые лабы С++ / Лаба2 / Laba 2 (6)
.cpp#include <iostream>
#include <windows.h>
using namespace std;
class DoubleQueue {
private:
double* data;
int capacity;
int front;
int rear;
int count;
public:
DoubleQueue(int maxSize) : capacity(maxSize), front(0), rear(-1), count(0) {
data = new double[capacity];
cout << "Создана очередь вместимостью " << maxSize << " элементов" << endl;
}
~DoubleQueue() {
delete[] data;
}
void put(double value) {
if (!full()) {
rear = (rear + 1) % capacity;
data[rear] = value;
count++;
cout << "Добавлен элемент: " << value << endl;
} else {
cout << "Ошибка: очередь полна!" << endl;
}
}
double get() {
if (!empty()) {
double value = data[front];
front = (front + 1) % capacity;
count--;
cout << "Извлечен элемент: " << value << endl;
return value;
}
cout << "Ошибка: очередь пуста!" << endl;
return -1.0;
}
bool empty() {
return count == 0;
}
bool full() {
return count == capacity;
}
void printStatus() {
cout << "Очередь " << (empty() ? "пуста" : "не пуста") << ", "
<< (full() ? "полна" : "есть свободное место") << endl;
}
};
int main() {
SetConsoleOutputCP(65001);
DoubleQueue queue(3);
queue.printStatus();
queue.put(1.1);
queue.put(2.2);
queue.put(3.3);
queue.put(4.4); // ошибка - очередь полна
queue.printStatus();
cout << "Извлечение элементов из очереди:" << endl;
while (!queue.empty()) {
queue.get();
}
queue.get(); // ошибка - очередь пуста
return 0;
}
Соседние файлы в папке Лаба2
