-
Notifications
You must be signed in to change notification settings - Fork 0
/
master.cpp
372 lines (329 loc) · 10.2 KB
/
master.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
//***************************GLOBAL FUNCTIONS***********************************
void BINARYmain();
void DECIMALmain();
void OCTALmain();
void HEXADECIMALmain();
int * getBIN(int bits);
int * getOCT(int bits);
int * getHEX(int bits);
int toDECIMAL(int type);
int fromDECIMAL(int initialNUM , int type);
void ENTER2exit(int type);
void clearterm(){
system("clear");
}
//***************************GLOBAL VARIABLES***********************************
int bits,i,num;
//*******************************main()*****************************************
int main(){
int opt;
clearterm();
cout<<"\t\t\t ***************** MENU *****************";
cout<<"\n\t\t\t\t [1].CONVERT from BINARY ";
cout<<"\n\t\t\t\t [2].CONVERT from DECIMAL ";
cout<<"\n\t\t\t\t [3].CONVERT from OCTAL ";
cout<<"\n\t\t\t\t [4].CONVERT from HEXADECIMAL ";
cout<<"\n\t\t\t\t [0].EXIT";
cout<<"\n\n\t\t\t\t Enter option number : ";
cin>>opt;
switch(opt){
case 1 :BINARYmain();
break;
case 2 :DECIMALmain();
break;
case 3 :OCTALmain();
break;
case 4 :HEXADECIMALmain();
break;
case 0 :clearterm();
exit(0);
break;
default:cout<<"invalid option ! choose again";
main();
}
ENTER2exit(0);
}
//****************************BINARY MENU***************************************
void BINARYmain(){
int opt;
clearterm();
cout<<"\t\t\t *************BINARY OPERATIONS MENU *****************";
cout<<"\n\t\t\t\t [1].CONVERT BINARY to DECIMAL ";
cout<<"\n\t\t\t\t [2].CONVERT BINARY to OCTAL ";
cout<<"\n\t\t\t\t [3].CONVERT BINARY to HEXADECIMAL ";
cout<<"\n\t\t\t\t [0].EXIT to MAIN MENU";
cout<<"\n\n\t\t\t\t Enter option number : ";
cin>>opt;
switch(opt){
case 1 :num=toDECIMAL(2);
cout<<"THE CONVERTED RESULT IS "<<num<<"\n\n\n";
break;
case 2 :num=toDECIMAL(2);
fromDECIMAL(num,8);
break;
case 3 :num=toDECIMAL(2);
fromDECIMAL(num,16);
break;
case 0 :main();
break;
default:cout<<"invalid option ! try again ";
BINARYmain();
}
ENTER2exit(2);
}
//***********************DECIMAL MENU*******************************************
void DECIMALmain(){
int opt;
clearterm();
cout<<"\t\t\t *************DECIMAL OPERATIONS MENU *****************";
cout<<"\n\t\t\t\t [1].CONVERT DECIMAL to BINARY ";
cout<<"\n\t\t\t\t [2].CONVERT DECIMAL to OCTAL ";
cout<<"\n\t\t\t\t [3].CONVERT DECIMAL to HEXADECIMAL ";
cout<<"\n\t\t\t\t [0].EXIT to MAIN MENU";
cout<<"\n\n\t\t\t\t Enter option number : ";
cin>>opt;
cout<<"Enter the decimal number to be converted :";
cin>>num;
switch(opt){
case 1 :fromDECIMAL(num,2);
break;
case 2 :fromDECIMAL(num,8);
break;
case 3 :fromDECIMAL(num,16);
break;
case 0 :main();
break;
default:cout<<"invalid option ! try again ";
DECIMALmain();
}
ENTER2exit(10);
}
//************************OCTAL MENU *******************************************
void OCTALmain(){
int opt;
clearterm();
cout<<"\t\t\t *************OCTAL OPERATIONS MENU *****************";
cout<<"\n\t\t\t\t [1].CONVERT OCTAL to BINARY ";
cout<<"\n\t\t\t\t [2].CONVERT OCTAL to DECIMAL ";
cout<<"\n\t\t\t\t [3].CONVERT OCTAL to HEXADECIMAL ";
cout<<"\n\t\t\t\t [0].EXIT to MAIN MENU";
cout<<"\n\n\t\t\t\t Enter option number : ";
cin>>opt;
switch(opt){
case 1 :num=toDECIMAL(8);
fromDECIMAL(num,2);
break;
case 2 :num=toDECIMAL(8);
cout<<"THE CONVERTED RESULT IS "<<num<<"\n\n\n";
break;
case 3 :num=toDECIMAL(8);
fromDECIMAL(num,16);
case 0 :main();
break;
default:cout<<"invalid option ! try again ";
OCTALmain();
}
ENTER2exit(8);
}
//**************************HEXADECIMAL MENU************************************
void HEXADECIMALmain(){
int opt;
clearterm();
cout<<"\t\t\t *************HEXADECIMAL OPERATIONS MENU *****************";
cout<<"\n\t\t\t\t [1].CONVERT HEXADECIMAL to BINARY ";
cout<<"\n\t\t\t\t [2].CONVERT HEXADECIMAL to DECIMAL ";
cout<<"\n\t\t\t\t [3].CONVERT HEXADECIMAL to OCTAL ";
cout<<"\n\t\t\t\t [0].EXIT to MAIN MENU";
cout<<"\n\n\t\t\t\t Enter option number : ";
cin>>opt;
switch(opt){
case 1 :num=toDECIMAL(16);
fromDECIMAL(num,2);
break;
case 2 :num=toDECIMAL(16);
cout<<"THE CONVERTED RESULT IS "<<num<<"\n\n\n";
break;
case 3 :num=toDECIMAL(16);
fromDECIMAL(num,8);
case 0 :main();
break;
default:cout<<"invalid option ! try again ";
HEXADECIMALmain();
}
ENTER2exit(16);
}
//*******************************BINARY INPUT***********************************
int * getBIN(int bits){
char BINinput[25];
static int numericalBIN[25];
cout<<"Enter the binary number of "<<bits<<" bits :";
for (i=(bits-1);i>-1;i--){
cin>>BINinput[i];
if (BINinput[i] == '1')
numericalBIN[i] = 1;
else if (BINinput[i] == '0')
numericalBIN[i] = 0;
else{
cout<<BINinput[i]<<" is not a BINARY exiting !!";
exit(0);
}
}
return numericalBIN;
}
//****************************OCTAL INPUT **************************************
int * getOCT(int bits){
char OCTinput[25];
static int numericalOCT[25];
cout<<"Enter the OCTAL number of"<<bits<<" digits :";
for (i=(bits-1) ; i> -1 ;i-- ){
cin>>OCTinput[i];
switch(OCTinput[i]){
case '1' :numericalOCT[i] = 1;
case '2' :numericalOCT[i] = 2;
case '3' :numericalOCT[i] = 3;
case '4' :numericalOCT[i] = 4;
case '5' :numericalOCT[i] = 5;
case '6' :numericalOCT[i] = 6;
case '7' :numericalOCT[i] = 7;
case '8' :numericalOCT[i] = 8;
}
}
return numericalOCT;
}
//**************************HEXADECIMAL INPUT**********************************
int * getHEX(int bits){
char HEXinput[25];
static int numericalHEX[25];
cout<<"Enter the OCTAL number of"<<bits<<" digits :";
for (i=bits-1 ;i > -1 ; i--){
cin>>HEXinput[i];
switch(HEXinput[i]){
case '1' : numericalHEX[i] = 1;
break;
case '2' : numericalHEX[i] = 2;
break;
case '3' : numericalHEX[i] = 3;
break;
case '4' : numericalHEX[i] = 4;
break;
case '5' : numericalHEX[i] = 5;
break;
case '6' : numericalHEX[i] = 6;
break;
case '7' : numericalHEX[i] = 7;
break;
case '8' : numericalHEX[i] = 8;
break;
case '9' : numericalHEX[i] = 9;
break;
case '0' : numericalHEX[i] = 0;
break;
case 'a' :
case 'A' : numericalHEX[i] = 10;
break;
case 'b' :
case 'B' : numericalHEX[i] = 11;
break;
case 'c' :
case 'C' : numericalHEX[i] = 12;
break;
case 'd' :
case 'D' : numericalHEX[i] = 12;
break;
case 'e' :
case 'E' : numericalHEX[i] = 12;
break;
case 'f' :
case 'F' : numericalHEX[i] = 12;
break;
default : cout<<HEXinput<<" is not a HEXADECIMAL number";
exit(0);
}
}
return numericalHEX;
}
//****************************TO DECIMAL CONVERTION ***************************
int toDECIMAL(int type){
int *getINPUTval;
int returnedINTval[25],finalRESULT=0;
if(type == 2){
cout<<"Enter the number of bits :";
cin>>bits;
getINPUTval = getBIN(bits);
}
else if (type == 8){
cout<<"Enter the number of digits : ";
cin>>bits;
getINPUTval = getOCT(bits);
}
else if (type == 16){
cout<<"Enter the number of digits : ";
cin>>bits;
getINPUTval = getHEX(bits);
}
for (i=bits-1 ; i>-1 ; i--){
returnedINTval[i] = *(getINPUTval + i);
}
for (i=bits-1 ; i>-1 ;i--){
finalRESULT = finalRESULT + ( returnedINTval[i] * pow(type,i));
}
return finalRESULT;
}
//**************************** FROM DECIMAL CONVERTING**************************
int fromDECIMAL(int initialNUM , int type){
int result[25],reminder[25],flag;
result[0] = initialNUM ;
for (i=1 ; result[i-1] > 0 ; i++){
result[i] = result[i-1] / type ;
reminder[i] = result[i-1] % type ;
flag = i;
}
cout<<"THE CONVERTED RESULT IS :";
for (i=flag ; i > 0 ; i--){
if (reminder[i]<10){
cout<<reminder[i];
}
else{
switch(reminder[i]){
case 10 :cout<<'A';
break;
case 11 :cout<<'B';
break;
case 12 :cout<<'C';
break;
case 13 :cout<<'D';
break;
case 14 :cout<<'E';
break;
case 15 :cout<<'F';
break;
}
}
}
cout<<"\n\n";
}
//**************************** ENTER 2 EXIT ************************************
void ENTER2exit(int type){
char varEXIT;
cout<<"Press ENTER to exit ";
cin.get();
if (cin.get() == '\n'){
clearterm();
switch(type){
case 0 : main();
break;
case 2 : BINARYmain();
break;
case 8 : OCTALmain();
break;
case 10: DECIMALmain ();
break;
case 16: HEXADECIMALmain();
break;
}
}
}