-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_to_decimal.cpp
200 lines (165 loc) · 3.86 KB
/
binary_to_decimal.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <bits/stdc++.h>
using namespace std;
// Converting a Number from Binary(base 2) to Decimal(base 10)
// linkedStack class
struct Node
{
int info;
Node* link;
};
class linkedStack
{
private:
Node* stackTop;
public:
linkedStack(); //constructor
bool isEmpty();
bool isFull();
void push(const int & );
void pop();
int top() const;
void initializeStack();
void copyStack(const linkedStack&);
~linkedStack();
};
linkedStack::linkedStack() //constructor
{
stackTop = NULL;
}
bool linkedStack::isEmpty()
{
return (stackTop==NULL);
}//end isEmpty
bool linkedStack::isFull()
{
return false;
}//end isFull
void linkedStack::push(const int &newElement)
{
Node* newNode; //pointer to create the new node
newNode = new Node; //create the node
newNode->info = newElement; //stor newElement in the node
newNode->link = stackTop; //insert newNode before stackTop
stackTop = newNode; //set stackTop to point to the top node
}//end push
void linkedStack::pop()
{
Node* temp;//pointer to deallocate memory
if (stackTop != NULL)
{
temp = stackTop; //set temp to point to the top node
stackTop = stackTop->link; //advance stackTop to the next node
delete temp; //delete the top node
}
else
cout << "Cannot remove from an empty stack." << endl;
}//end pop
int linkedStack ::top() const
{
assert(stackTop != NULL);
return stackTop->info;
}//end top
void linkedStack::initializeStack() {
Node* temp;//pointer to delete the node
while (stackTop != NULL) //while there are elements in the stack
{
temp = stackTop; //set temp to point to the current node
stackTop = stackTop->link; //advance stackTop to the next node
delete temp; //deallocate memory occupied by temp
}
}//end initializeStack
void linkedStack::copyStack( const linkedStack&otherStack)
{
Node* newNode, * current, * last;
if (stackTop != NULL)//if stack is nonempty, make it empty
initializeStack();
if (otherStack.stackTop == NULL) // if other stack is empty
stackTop == NULL;
else // if other stack is nonempty
{
current = otherStack.stackTop;
//copy the stackTop element of the stack
stackTop = new Node;
stackTop->info = current->info;
stackTop->link = NULL;
last = stackTop;
current = current->link;
while (current != NULL)
{
newNode = new Node;
newNode->info = current->info;
newNode->link = NULL;
last->link = newNode;
last = newNode;
current = current->link;
}
}
}//end copyStack
linkedStack::~linkedStack() //destructor
{
initializeStack();
}//end destructor
//end linkedStack class
// number class
class number
{
private :
long decimal; //number in decimal system
linkedStack binary; //number in Binary system
public:
number(); //constructor
void readNumber(); //reads the number in binary format as a string then converts it to ints and pushes them to the binary stack
void toDecimal(); //converts the number from binary to decimal
void print(); //prints the number in the 2 systems
};
number::number() //constructor
{
decimal = 0;
}//end constructor
void number::readNumber()
{
cout << "Please enter the number in binary format (0s and 1s) : " << endl;
string binary_number;
cin >> binary_number;
for (int i = 0; i < binary_number.size(); i++)
{
binary.push(binary_number[i] - 48);
}
}//end readNumber
void number::toDecimal()
{
//use multiplied by 2 algorithm
linkedStack temp;
temp.copyStack(binary);
int sum = 0;
int index = 0;
while (!temp.isEmpty())
{
sum+=temp.top()* (pow(2, index++));
temp.pop();
}
decimal = sum;
}//end toDecimal
void number::print()
{
linkedStack temp;
while (!binary.isEmpty())
{
temp.push(binary.top());
binary.pop();
}
while (!temp.isEmpty())
{
cout << temp.top();
temp.pop();
}
cout << " is equivelent to " << decimal << endl;
}//end print
int main()
{
number obj;
obj.readNumber();
obj.toDecimal();
obj.print();
return 0;
}