/* Assignment
Program no. EX5.c */
//Program converting numaerical s1ing into corresponding real numaer
void main()
{
int i, j, sign, m, x, y, flag;
int len,len2;
static int a[7], a2[7];
float b[7];
float p=0, q=0;
float realno;
char s1[12];
clrscr();
len=len2=i=j=m=x=y=sign=m=flag=0;
printf("\n\n Enter a numerical string: ");
gets(s1);
flushall();
for(i=0, j=0, len=1; s1[i]!='.' && s1[i]!='\0'; i++)
{
if(s1[i+1]=='.')
{
flag=1;
}
if(s1[i]>='0' && s1[i]<='9') //To check if the character is a number
{
len++;
if(s1[i]=='1')
a[j]=1;
else if(s1[i]=='2')
a[j]=2;
else if(s1[i]=='3')
a[j]=3;
else if(s1[i]=='4')
a[j]=4;
else if(s1[i]=='5')
a[j]=5;
else if(s1[i]=='6')
a[j]=6;
else if(s1[i]=='7')
a[j]=7;
else if(s1[i]=='8')
a[j]=8;
else if(s1[i]=='9')
a[j]=9;
else
a[j]=0;
}
else if(s1[i]=='+')
{
sign=0;
continue;
}
else if(s1[i]=='-')
{
sign=1;
continue;
}
else
{
printf("\n\n Error: Invalid Input number\n");
break;
}
j++;
}
len=len-1; //Length of numbers before Decimal
if(flag==1 || s1[0]=='.' || s1[1]=='.')
{
for(m=i; s1[m]!='\0'; m++)
{
if(s1[0]=='.') //To check if the number does not start with (.)
{
a[0]=0;
}
if(s1[m]=='.')
{
if(s1[m]=='.' && s1[m+1]=='\0') //If the input ends with (.)
{
b[0]=0;
break;
}
for(j=0, len2=0, m=m+1; s1[m]!='\0'; m++, j++, len2++)
{
if(s1[m]=='1')
b[j]=1;
else if(s1[m]=='2')
b[j]=2;
else if(s1[m]=='3')
b[j]=3;
else if(s1[m]=='4')
b[j]=4;
else if(s1[m]=='5')
b[j]=5;
else if(s1[m]=='6')
b[j]=6;
else if(s1[m]=='7')
b[j]=7;
else if(s1[m]=='8')
b[j]=8;
else if(s1[m]=='9')
b[j]=9;
else if(s1[m]=='0')
b[j]=0;
else
{
printf("\n\n Error: Invalid Input");
break;
}
}
}
}
}
/* Sorting the a2[i] array in reverse order & multiplying
with 10's multiples & providing place value to the array*/
for(x=1, y=len-1; y>=0; y--)
{
a2[y]=a[y]*x;
x=x*10;
}
//Dividing b[i] in 10's multiples & providing place value to the array
for(x=10, i=0;i<len2; i++)
{
b[i]=b[i]/x;
x=x*10;
}
//Add a2[i] arrays
for(p=0,i=0;i!=len;i++)
{
p=p+a2[i];
}
//Add b[i] arrays
for(q=0,i=0;i!=len2;i++)
{
q=q+b[i];
}
//Add p & q together
realno = p + q;
if(sign==1)
{
realno=-1 * realno;
}
printf("\n\n The corresponding real number is %5.3f ", realno);
printf("\n\n\n Press any key to exit....");
getch();
}

0 comments:
Post a Comment