-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumberSystemConverter.cpp
117 lines (117 loc) · 3.06 KB
/
NumberSystemConverter.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
#include<bits/stdc++.h>
using namespace std ;
//Function for separating integer and fractional parts:
pair<string,string> separate(string inp){
string p1,p2 ;
p1="" ;
p2="0." ;
int i=0 ;
while(i<inp.size()){
if(inp[i]=='.'){
break;
}
p1+=inp[i] ;
i++ ;
}
i++ ;
while(i<size(inp)){
p2+=inp[i] ;
i++ ;
}
return {p1,p2} ;
}
//Function to convert any number system to decimal number system:
pair<int,float> anytodecimal(string whole,string frac,int rad1,map<int,char>m){
int wdec=0 ;float fdec=0 ;
//Converting the integer part:
for(int i=size(whole)-1;i>=0;i--){
int j=0 ;
while(m[j]!=whole[i]){
j++ ;
}
wdec+=j*pow(rad1,size(whole)-1-i) ;
}
//Converting the fractional part:
if(frac!="0."){
for(int i=2;i<size(frac);i++){
int j=0 ;
while(m[j]!=frac[i]){
j++ ;
}
fdec+=j*pow(1.0/rad1,i-1) ;
}
}
return {wdec,fdec} ;
}
//Function to convert decimal numbers to any system.
pair<string,string> decimaltoany(int w,float f,int rad2,map<int,char>m){
//Converting integer part :
string finW="" ;
while(w!=0){
int temp=w%rad2 ;
for(int i=0;i<rad2;i++){
if(i==temp){
finW=finW+m[temp] ;
}
}
w=w/rad2 ;
}
//Converting fractional part:
string finF="" ;
for(int i=0;i<10;i++){
f=f*rad2 ;
int t=int(f) ;
finF=finF+m[t] ;
f=f-t ;
}
return {finW,finF} ;
}
int main(){
//Input from the user:
cout << "------------------------------------------------" << endl ;
int rad1 ;
cout << "Enter input Radix:" ;
cin >> rad1 ;
string inp ;
cout << "Enter number in above mentioned system:" ;
cin >> inp ;
int rad2 ;
cout << "Enter output Radix :" ;
cin >> rad2 ;
//Making a map:
map<int,char> m ;
m[0]='0' ;
m[1]='1' ;
m[2]='2' ;
m[3]='3' ;
m[4]='4' ;
m[5]='5' ;
m[6]='6' ;
m[7]='7' ;
m[8]='8' ;
m[9]='9' ;
m[10]='A' ;
m[11]='B' ;
m[12]='C' ;
m[13]='D' ;
m[14]='E' ;
m[15]='F' ;
//Using defined function for separation of the number:
string whole=separate(inp).first ;
string frac=separate(inp).second ;
//Conversion of every number regardless of the system to Decimal:
int tempW=anytodecimal(whole,frac,rad1,m).first ;
float tempF=anytodecimal(whole,frac,rad1,m).second ;
//Final conversion to the desired number system:
string finW,finF ;
if(rad2==10){
cout << "The Number is:" << tempW+tempF << endl ;
}
else{
finW=decimaltoany(tempW,tempF,rad2,m).first;
finF=decimaltoany(tempW,tempF,rad2,m).second;
reverse(finW.begin(),finW.end()) ;
cout << finW << "." << finF << endl ;
}
cout << "------------------------------------------------" ;
}