-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYeeStackConverter.cpp
More file actions
241 lines (235 loc) · 8.21 KB
/
Copy pathYeeStackConverter.cpp
File metadata and controls
241 lines (235 loc) · 8.21 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// YeeStackConverter.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <stdio.h>
#include<iostream>
#include<string>
#include <math.h>
using namespace std;
void menu(int &number, int &base, int &otherBase, int &difNumBase,string &hexRef);
//@class: Yee_Stack_Converter
// implementation of a stack, used for number based conversion.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Yee_Stack_Converter
{
public:
int numOfElements = 0;
//@method: push
//place element onto top of the "stack" following LIFO
//======================================================================================================================================
bool push(int *pointer, int array[], int element)
{
if (numOfElements == 100) //if full, cant add to stack so going to exit (essentially check_full())
{
cout << "The stack is full. Can not compute, exiting..." << endl;
exit(1);
}
else
{
*(pointer) = element; //access address to next available splot
numOfElements++;
return true;
}
return false;
}
//======================================================================================================================================
//@method: pop
//will return the top of the stack
//======================================================================================================================================
int pop(int *pointer, int array[])
{
if (numOfElements == 0) //essentially check_empty()
{
return -1; //return negative value if stack empty
}
int temp = *(pointer);
numOfElements--;
return temp;
}
//======================================================================================================================================
//#method: check_full
//return boolean value true if the stack is full.
//======================================================================================================================================
bool check_full()
{
if (numOfElements != 100)
{
return false;
}
return true;
}
//======================================================================================================================================
//@method: check_empty
//will return bool value of true if the stack is empty
//======================================================================================================================================
bool check_empty()
{
if (numOfElements == 0)
{
return true;
}
return false;
}
//======================================================================================================================================
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//@main:
//will run the desired conversions.
//===============================================================================================================================
int convertToDecimal(int& base, int &number, Yee_Stack_Converter holder,int* pointer, int array[],string hexString);
void convertFromDecimal(int &input, int &base, int remainder,int* pointer, Yee_Stack_Converter &holder,int array[]);
int main()
{
//declaration of variables needed, and their references
int input = 0, base = 0, remainder = 0, temp = 0, otherBase = 0,difNumBase=0, &inputRef = input, &baseRef = base, &otherBaseRef=otherBase,&difNumBaseRef=difNumBase;
const int max = 100;
int array[max];
string hexString = " "; //used when the user wants to enter a hex value
int *pointer = array;
Yee_Stack_Converter stack; // when converting, holds the remainders...
Yee_Stack_Converter &holder = stack;
while (true) //run menu infintely
{
menu(inputRef, baseRef, otherBaseRef, difNumBaseRef,hexString);
if (input != -2) //converting decimal to whatever base.
{
remainder = 0;
convertFromDecimal(input, base, remainder, pointer,holder,array);
cout << endl;
}
else //converting from another base to decimal, hex, octal ,or binary
{
cout << "Converting from base: " << otherBase << " to " << base << endl;
int temp = convertToDecimal(otherBase, difNumBaseRef,holder, pointer, array,hexString); //no matter what, convert to decimal first.
if (base == 10) //if want from another base to decimal, just print out result
{
cout << "The decimal conversion is: " << temp << endl;
}
else
{
convertFromDecimal(temp, base, remainder, pointer, holder, array); //otherwise use previously implemented method to convert decimal to any other base.
cout<<endl;
}
}
}
}
//===============================================================================================================================
//@function: menu
// Will print out options for the user to choose from.
//===============================================================================================================================
void menu(int &number, int &base, int &otherBase, int &difNumBaseRef,string &hexRef)
{
cout << "Please enter the base you would like to convert to, or enter -1 to quit: ";
cin >> base;
if (base == -1)
{
cout << "selected to quit: " << endl;
exit(0);
}
cout << "\nPlease enter the positive number you would like to convert to in decimal, if not in decimal please type -2: ";
cin >> number;
if (number == -2) //selecting to enter number from different base
{
cout << "Please enter the base you would like to convert FROM: 2,8, or 16: ";
cin >> otherBase;
if (otherBase == 2 || otherBase == 8 || otherBase == 16)
{
if (otherBase != 16)
{
cout << "\nPlease enter the NUMBER in the base you want to convert from: ";
cin >> difNumBaseRef;
}
else
{
cout << "Please enter the hex value you would like to convert." << endl;
cin >> hexRef;
}
}
else
{
cout << "invalid base to convert from." << endl;
return;
}
}
}
//===============================================================================================================================
//@function: convertFromDecimal
// will take a decimal number, and convert it to the specified base
//===============================================================================================================================
void convertFromDecimal(int &input, int &base, int remainder,int* pointer,Yee_Stack_Converter &holder,int array[])
{
int temp = 0;
cout << "selected to convert to base: " << base << " from the number: " << input << endl;
cout << "The answer is: " << endl;
while (input > 0)
{
remainder = input%base;
input = input / base;
holder.push(pointer, array, remainder);
pointer++;
}
while (!holder.check_empty())
{
pointer--;
temp = holder.pop(pointer, array);
if (base == 16) //if base 16, need to print out appropriate characters that represent numbers from 10-15
{
if (temp == 10)
cout << 'A';
else if (temp == 11)
{
cout << 'B';
}
else if (temp == 12)
{
cout << 'C';
}
else if (temp == 13)
{
cout << 'D';
}
else if (temp == 14)
{
cout << 'E';
}
else if (temp == 15)
{
cout << 'F';
}
else
{
cout << temp;
}
}
else
{
cout << temp;
}
}
}
//===============================================================================================================================
//@function: convertToDecimal
//will take a binary, hex, or octal number and convert it to decimal
//===============================================================================================================================
int convertToDecimal(int& base, int &number, Yee_Stack_Converter holder,int* pointer, int array[],string hexString)
{
int counter = 0; //keeps track of power
int remainder = 0;
int result = 0;
int temp = 0;
if (base != 16) //not checking for letters
{
while (number > 0)
{
remainder = number % 10;
result += remainder*(int)pow(base, counter); //equation to convert to decimal
number = number / 10;
counter++;
}
}
else
{
result = std::stoi(hexString, nullptr, 16);
}
return result;
}
//===============================================================================================================================