#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
#define max 100
/*---From Decimal To R BASE conversion---*/
void toRBase_handle_intpart(long val,char result[max],int r)
{
int i=0;
while(val>0)
{
result[i]=(val%r>9)?('A'+(val%r)%10):(val%r)+48;
val=val/r;
i++;
}
result[i]='\0';
strrev(result);
result[i]='.';
result[i+1]='\0';
}
void toRBase_handle_decpart(double val,char result[max],int r)
{
int i=strlen(result);
int intp,ctr=10;
while(val>0&&ctr>0)
{
intp=val*r;
val=(val*r)-intp;
result[i]=(intp>9)?('A'+(intp%10)):(intp)+48;
i++;
ctr--;
}
result[i]='\0';
}
void to_RBase(double value,char result[max],int r)
{
long intpart=(long)value;
double decpart=value-intpart;
toRBase_handle_intpart(intpart,result,r);
toRBase_handle_decpart(decpart,result,r);
}
/*---From Decimal To R BASE conversion---*/
/*---From R To Decimal BASE conversion---*/
long m_pow(int x,int y)
{
long val=1;
while(y>0)
{
val=val*x;
y--;
}
return val;
}
int dot_pos(char val[max])
{
int ctr=0;
while(val[ctr]!='\0'&&val[ctr]!='.')
{
ctr++;
}
return(ctr-1);
}
double toDec_handle_decpart(char val[max],int start,int r)
{
double value=0.0;
int power=-1;
start=start+2;
while(val[start]!='\0')
{
value=value+((val[start]>=48&&val[start]<=57)?((val[start]-48)*pow((double)r,(double)power)):((val[start]-55)*pow((double)r,(double)power)));
start++;
power--;
}
return value;
}
long toDec_handle_intpart(char val[max],int start,int r)
{
long value=0;
int power=0;
while(start>=0)
{
value=value+((val[start]>=48&&val[start]<=57)?((val[start]-48)*m_pow(r,power)):((val[start]-55)*m_pow(r,power)));
start--;
power++;
}
return value;
}
double to_Decimal(char val[max],int r)
{
long intpart;
double fullvalue=0.0;
int dot=dot_pos(val);
intpart=toDec_handle_intpart(val,dot,r);
if((strlen(val)-2)>dot)
fullvalue=toDec_handle_decpart(val,dot,r);
fullvalue+=intpart;
return fullvalue;
}
/*---From R To Decimal BASE conversion---*/
/*Validation Modules*/
int validate_characters(char arr[max])
{
int i=0,dot_count=1;
while(arr[i]!='\0')
{
if((arr[i]>=48&&arr[i]<=57)||(arr[i]>=65&&arr[i]<=70)||arr[i]=='.')
{
if(arr[i]=='.')
{
if(dot_count)
dot_count--;
else
{
i=-1;
break;
}
}
}
else
{
i=-1;
break;
}
i++;
}
return i;
}
int validate_number(char arr[max],int r)
{
int i=0,value;
while(arr[i]!='\0')
{
if(arr[i]!='.')
{
value=(arr[i]>=48&&arr[i]<=57)?arr[i]-48:arr[i]-55;
if(value>=r)
{
i=-1;
break;
}
}
i++;
}
return i;
}
/*Validation Modules*/
int main()
{
char ouputString[max],inputString[max];
int base,rbase,i;
double value;
printf("Enter a number in any base\n");
scanf("%s",inputString);
printf("Enter the base of the number\n");
scanf("%d",&base);
printf("Enter the resultant base of the number less than 16\n");
scanf("%d",&rbase);
for(i=0;i<strlen(inputString);i++)
inputString[i]=toupper(inputString[i]);
if(rbase<=16&&rbase>1&&base<=16&&base>1)
{
if(validate_characters(inputString)>=0)
{
if(validate_number(inputString,base)>=0)
{
value=to_Decimal(inputString,base);
to_RBase(value,ouputString,rbase);
printf("\n%s",ouputString);
}
else
printf("Input string does not adhere to the base provided\n");
}
else
printf("Invalid input string\n");
}
else
printf("\nInvalid rbase provided");
getch();
return 0;
}
0 comments:
Post a Comment