Last active
January 17, 2019 10:45
-
-
Save asd227695/7f66dc82dec64194c18e55c64af6b696 to your computer and use it in GitHub Desktop.
Conversion of other base system to decimal(floating and normal).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<string.h> | |
#include<math.h> | |
#include<cctype> | |
using namespace std; | |
int bd[30],ad[30],l_bd,l_ad,flag; | |
void change(char a[30],int n,int base) | |
{ | |
int i=0,j=0; | |
while(a[i]!='.'&&i<n) | |
{ | |
if(base==16) | |
{ | |
if(isalpha(a[i])) | |
{ | |
bd[j]=((int)tolower(a[i])-87); | |
i++; | |
} | |
else | |
{ | |
bd[j]=((int)a[i]-48); | |
i++; | |
} | |
} | |
else | |
{ | |
bd[i]=((int)a[i]-48); | |
i++; | |
} | |
} | |
int k=0;flag=1; | |
if(base==8||base==2) | |
{ | |
while(k<n) | |
{ | |
if((int)(bd[k])>=base) | |
{ | |
flag=0; | |
k++; | |
} | |
else{k++;} | |
} | |
} | |
else | |
{ | |
while(k<n) | |
{ | |
if((int)((bd[k]))>=base) | |
{ | |
flag=0; | |
k++; | |
} | |
else{k++;} | |
} | |
} | |
if(flag==0){cout<<"Enter Valid Input"<<endl;} | |
else | |
{ | |
for(int j=0;j<i;j++) | |
{ | |
cout<<bd[j]<<"\t"; | |
} | |
l_bd=i; | |
while(i+1<n) | |
{ | |
if(base==16) | |
{ | |
if(isalpha(a[i+1])) | |
ad[j]=((int)tolower(a[i+1])-87); | |
else | |
{ | |
ad[j]=((int)a[i+1]-48); | |
} | |
i++; | |
j++; | |
} | |
else | |
{ | |
ad[j]=((int)a[i+1]-48); | |
i++; | |
j++; | |
} | |
} | |
l_ad=j; | |
cout<<"."; | |
for(int i=0;i<j;i++) | |
{ | |
cout<<ad[i]; | |
} | |
} | |
} | |
void convert(int bd[30],int ad[30],int base) | |
{ | |
int s=0; | |
float s_t=0; | |
for(int i=0,j=l_bd-1;i<l_bd,j>=0;i++,j--) | |
{ | |
s=s+(pow(base,i)*(bd[j])); | |
} | |
for(int i=1;i<l_ad+1;i++) | |
{ | |
s_t=s_t+(pow(base,-i)*(ad[i-1])); | |
} | |
s_t=s+s_t; | |
if(!flag==0) | |
{ | |
cout<<"\nThe decimal number will be : "<<s_t<<endl; | |
} | |
} | |
int main() | |
{ | |
char a[30],ch;int n,base; | |
do | |
{ | |
cout<<"Enter base to which you want to convert : "; | |
cin>>base; | |
cout<<"Enter number "<<endl; | |
cin>>a; | |
n=strlen(a); | |
change(a,n,base); | |
convert(bd,ad,base); | |
cout<<"\nTo continue press y"<<endl; | |
cin>>ch; | |
}while(ch=='y'); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment