Sunteți pe pagina 1din 4

#include<stdio.

h>

#include<stdlib.h>

#include<math.h>

float my_a2f(char *);

main()

char ch[20];

float i;

printf("\n enter the number as a string\n");

scanf("%[^\n]", ch);

//gets//

i = my_a2f(ch);

printf(" string =%s , float =%g \n", ch, i);

}
float my_a2f(char *p)

// here i took another two variables for counting the number of digits in mantissa

//

int i, num = 0, num2 = 0, pnt_seen = 0, x = 0, y = 1;

float f1, f2, f3;

for (i = 0; p[i]; i++)//iteration to find the dot//

if (p[i] == '.')

pnt_seen = i;

break;

for (i = 0; p[i]; i++)


{

if (i < pnt_seen)

num = num * 10 + (p[i] - 48);//before dot convert string into value//

else if (i == pnt_seen)

continue;//'.' found ignore it//

else

num2 = num2 * 10 + (p[i] - 48);// after dot convert string into value//

++x;

// it takes 10 if it has 1 digit ,100 if it has 2 digits in mantissa

//

for (i = 1; i <= x; i++)

y = y * (pow(10,x));
f2 = num2 / (float) y;

f3 = num + f2;

return f3;

S-ar putea să vă placă și