-
Notifications
You must be signed in to change notification settings - Fork 130
/
NumberSystems.cpp
204 lines (127 loc) · 4.24 KB
/
NumberSystems.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
#include <iostream>
#include <string>
#include <cmath>
#include <sstream>
std::string base10toR(std::string number, int r)
{
std::string answer = "";
if(number.find(".")<number.size())
{
std::string IntegerPart = number.substr(0,number.find("."));
std::string decimalPart = number.substr(number.find(".")+1,number.size()-1);
decimalPart = "0."+decimalPart;
std::string answerIntegerPart = base10toR(IntegerPart,r);
std::string answerDecimalPart = " ";
double deci = std::stod(decimalPart);
int x;
std::string str;
for (int i = 0; i < 6; i++)
{
deci = deci * r;
const std::string Hex = "0123456789ABCDEF";
x = (int)deci;
answerDecimalPart.insert(answerDecimalPart.length() - 1, std::string(1,Hex[x]));
deci = deci - (double)x;
}
answerDecimalPart.insert(0, ".");
answer = answerIntegerPart + answerDecimalPart;
}
else
{ int n=0;
std::stringstream ss(number);
ss>>n;
std::string str;
while(n!=0)
{ if(n%r <10)
{
str = std::to_string(n%r);
answer = str + answer;
}
else
{
char ch = 'A' + (n%r) -10;
std::string s(1,ch);
answer.insert(0,s);
}
n = n/r;
}
}
return answer;
}
std::string baseRto10(std::string number,int r)
{
std::string answer = "";
if(number.find(".")<number.size())
{
std::string IntegerPart = number.substr(0, number.find("."));
std::string decimalPart = number.substr(number.find(".") + 1, number.size() - 1);
std::string answerIntegerPart = baseRto10(IntegerPart, r);
std::string answerDecimalPart = "";
double deci = 0;
int x;
for(int i = 0 ; i <decimalPart.size();i++)
{
if (decimalPart[i] >= '0' && decimalPart[i] <= '9')
{
x = std::stoi(std::string(1, decimalPart[i]));
}
if (decimalPart[i] >= 'A' && decimalPart[i] <= 'F')
{
if (decimalPart[i] == 'A')
x = 10;
else
{
x = 10 + decimalPart[i] - 'A';
}
}
deci += x*pow(r,-1-i);
}
std::string str = std::to_string(deci);
answerDecimalPart = str.substr(1,str.size());
answer = answerIntegerPart+ answerDecimalPart;
}
else
{
int x = 0;
int n = 0;
for(int i = 0;i<number.size();i++)
{ if(number[i]>='0' && number[i]<='9')
{
x = std::stoi(std::string(1, number[i]));
}
if(number[i]>='A' && number[i]<='F')
{
if(number[i]=='A')
x = 10;
else
{
x = 10 + number[i]-'A';
}
}
n += x*std::pow(r,number.size()-i-1);
}
answer = std::to_string(n);
}
return answer;
}
std::string basextoy(std::string number,int x,int y)
{
std::string temp = baseRto10(number,x);
std::string answer = base10toR(temp,y);
return answer;
}
int main()
{ std::string n;
int x,y;
std::cout<<"This program can convert number of any base less than or equal to 16 to any other base less than or equal to 16"<<std::endl;
std::cout<<"Enter a number : "<<std::flush;
std::cin>>n;
std::cout<<"\nEnter the base of number you entered: "<<std::flush;
std::cin>>x;
std::cout<<"\nEnter the base in which you want to convert your number: "<<std::flush;
std::cin>>y;
std::string temp = baseRto10(n,x);
std::string answer = base10toR(temp,y);
std::cout<<"\nYour number in base "<<y<<" is "<<answer<<std::endl;
return 0;
}