-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost_decimalQueue.cpp
63 lines (53 loc) · 1.29 KB
/
post_decimalQueue.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include "post_decimalQueue.h"
using namespace std;
BinaryQueue::BinaryQueue() {
front = rear = nullptr;
}
void BinaryQueue::enqueue(int val) {
QueueNode* newNode = new QueueNode;
newNode->value = val;
newNode->next = nullptr;
if (isEmpty()) {
front = rear = newNode;
}
else {
rear->next = newNode;
rear = newNode;
}
}
int BinaryQueue::dequeue() {
if (isEmpty())
{
cout << "Fractional Part is Empty or is '0'." << endl;
return -1;
}
int value = front->value;
QueueNode* temp = front;
front = front->next;
delete temp;
if (front == nullptr) {
rear = nullptr;
}
return value;
}
bool BinaryQueue::isEmpty() {
if (front == nullptr)
if (rear == nullptr) return true;
else return false;
else return false;
}
BinaryQueue::~BinaryQueue() {
while (!isEmpty()) {
dequeue();
}
}
void BinaryQueue::afterDecimalQueue(BinaryQueue & fracQueue, double num, int decimal_places) {
if (decimal_places == 0 || num == 0.0) {
return; // Base case: stop recursion if decimal limit or fraction reaches zero
}
num *= 2;
int result = num;
fracQueue.enqueue(result); // Enqueue the integer part as the binary digit
afterDecimalQueue(fracQueue, num - result, (decimal_places - 1)); // Recursive call
}