Atof with scientific notation
Good afternoon. I'm trying to code a program which converts a string in a double and in a string again but with scientific notation.
The exercise is from KnR:
Extend atof to handle scientific notation of the form 123.45e-6 where a floating-point number may be followed by e or E and an optionally signed exponent.
What I want to do, despite of what the exercise mentioned, is convert something like
450 to 4.50e+2 and 0.05 to 5.0e-2
that's why I thought I had to convert the double into a string again. To put the "e-2" as a string concatenated with the value.
In addiction to that, I wonder if I can write char* notation like I wrote in the code. Because I don't want each char separated by index.
conversion.h
Code:
void itob(int, char*, int);
char* Atof(char*);
atof.c
Code:
/* atof: convert string s in scientific notation double */
#include <ctype.h>
#include <string.h>
#include "../Headers/conversion.h"
char* Atof(char* s)
{
double val, power;
int i, sign, npow, dotlocation;
char* notation, strval, strsci, strnpow;
for(i = 0; isspace(s[i]); i++)
;
sign = (s[i] == '-') ? -1 : 1;
if(s[i] == '+' || s[i] == '-')
i++;
for(val = 0.0; isdigit(s[i]); i++)
val = 10.0 * val + (s[i] - '0');
if(s[i] == '.')
{
dotlocation = i;
i++;
}
for(power = 1.0, npow = 0; isdigit(s[i]); i++)
{
val = 10.0 * val + (s[i] - '0');
power *= 10.0;
npow++;
}
val = sign * val / power;
strval = val + '0';
strnpow = npow + '0';
if(npow > 0 && s[dotlocation - 1] != '0')
strsci = "e+" + strnpow + '0';
if(npow > 0 && s[dotlocation - 1] == '0')
strsci = "e-" + strnpow + '0';
notation = strcat(strval, strsci);
++*notation = '\0';
return notation;
}
mainatof.c
Code:
#include <stdio.h>
#include <stdlib.h>
#include "Headers/conversion.h"
#include "Conversion/atof.c"
#include "Validation/isanumber.c"
#ifndef MAXSIZE
#define MAXSIZE 1000
#endif
#ifndef SCIENTIFIC
#define SCIENTIFIC 4
#endif
int main(void)
{
char strnum[MAXSIZE];
char* notation;
double d;
do {
printf("Please enter a double: ");
fgets(strnum, sizeof(strnum), stdin);
}while(!isanumber(strnum));
notation = Atof(strnum);
printf("The double value in scientific notation is %s\n", notation);
return 0;
}
I have some warnings and that error saying the value wasn't assigned to a variable.
Code:
In file included from mainatof.c:5:0:
Conversion/atof.c: In function ‘Atof’:
Conversion/atof.c:39:13: warning: assignment makes integer from pointer without a cast [enabled by default]
Conversion/atof.c:42:13: warning: assignment makes integer from pointer without a cast [enabled by default]
Conversion/atof.c:44:4: warning: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [enabled by default]
/usr/include/string.h:136:14: note: expected ‘char * __restrict__’ but argument is of type ‘char’
Conversion/atof.c:44:4: warning: passing argument 2 of ‘strcat’ makes pointer from integer without a cast [enabled by default]
/usr/include/string.h:136:14: note: expected ‘const char * __restrict__’ but argument is of type ‘char’
Conversion/atof.c:45:18: error: lvalue required as left operand of assignment