Quote:
You may also change/add the commented lines below,so that you could support also negative values:
The idea is simply by not touching the great short code CPallini made for positive values and just treat the negative value as positive and multiplying by -1 at the end to make it negative.
#include <stdio.h>
int main()
{
char inputstr[] = "-.45";
float result= 0.0f;
int len = sizeof(inputstr)-1;
int dotpos=0;
int n;
if (inputstr[0]=='-'||inputstr[0]=='+') n=1; else n=0;
for (; n < len; ++n) if (inputstr[n] == '.')
dotpos = len - n - 1;
else
result = result * 10.0f + (inputstr[n]-'0');
while (dotpos--)
result /= 10.0f;
if (inputstr[0]=='-') result*=(-1); printf("%f\n",result);
return 0;
}
I have changed size_t to integer,because I don't know how to use them.