-
Notifications
You must be signed in to change notification settings - Fork 0
/
hexa.cpp
127 lines (107 loc) · 1.85 KB
/
hexa.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
/* C++ program for number conversion for hexa and octal number
* Written By: Ramchandra Shahi Thakuri
* Date:
*/
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
class num
{
public:
char hexaN[100];
int octN;
};
class hexa : public num
{
public:
int val1;
hexa()
{
val1 = 0;
}
void read()
{
cout << "\nEnter hexadecimal number: ";
cin >> hexaN;
}
int hexaToDec()
{
int term;
int len;
len = strlen(hexaN);
for(int i; hexaN[i] != '\0'; i++)
{
len--;
if(hexaN[i] >= '0' && hexaN[i] <= '9')
term = hexaN[i] - 48;
else if(hexaN[i] >= 'a' && hexaN[i] <= 'f')
term = hexaN[i] - 87;
else if(hexaN[i] >= 'A' && hexaN[i] <= 'F')
term = hexaN[i] - 55;
val1 += term * pow(16, len);
}
return val1;
}
};
class octal : public num
{
public:
int val2;
octal()
{
val2 = 0;
}
void read()
{
cout << "\nEnter octal number: ";
cin >> octN;
}
int octToDec()
{
int temp;
int i;
temp = octN;
i = 0;
while(temp != 0)
{
temp %= 10;
val2 += temp * pow(8, i++);
temp /= 10;
}
return val2;
}
}
int operator +(hexa h, octal o)
{
int k;
k = h.val1 + o.val2;
return k;
}
int operator +(octal o, int num)
{
int k;
k = o.val2 + num;
return k;
}
int main()
{
hexa h;
octal o;
int val;
system("clear");
cout << "\t\t\tNumber conversion program\n";
h.read();
val = h.hexaToDec();
cout << "\nHexa to decimal is " << val << endl;
o.read();
val = o.octToDec();
cout << "\nOctal to decimal is " << val << endl;
cout << "\n\n" << h.val1 << "+" << o.val2 << " = " << h.val1 + o.val2 << endl;
cout << "\nEnter number to add in hexa and oct number: ";
cin >> val;
cout << "\nOctal + Num:\n";
cout << "\n\n" << o.val2 << "+" << val << " = " << o.val2 + val << endl;
return 0;
}