#include <stdio.h>
#include <stdint.h>
#define NUM_ZERO ('0')
#define NUM_NINE ('9')
#define SIGN_MINUS ('-')
#define SIGN_PLUS ('+')
#define DOT ('.')
#define IS_NUMERIC(chr) ((chr) >= NUM_ZERO && (chr) <= NUM_NINE)
#define IS_SIGN_MINUS(chr) ((chr) == SIGN_MINUS)
#define IS_SIGN_PLUS(chr) ((chr) == SIGN_PLUS)
#define IS_DOT(chr) ((chr) == DOT)
#define CHAR2NUM(chr) ((chr) - NUM_ZERO)
float custom_atof(const char *str) {
// Your logic here
float sign = 1.0f;
float num_int = 0.0f;
float num_fract = 0.0f;
float num_divisor = 1.0f;
if(IS_SIGN_MINUS(*str)){
sign = -1.0f;
str++;
}
else if(IS_SIGN_PLUS(*str)){
sign = 1.0f;
str++;
}
while((*str) && IS_NUMERIC(*str))
{
num_int = num_int*10.0f + CHAR2NUM(*str);
str++;
}
if(IS_DOT(*str))
{
str++;
while((*str) && IS_NUMERIC(*str))
{
num_fract = num_fract*10.0f + CHAR2NUM(*str);
num_divisor *= 10.0f;
str++;
}
}
return sign*(num_int+num_fract/num_divisor);
}
int main() {
char str[101];
fgets(str, sizeof(str), stdin);
// Remove newline
uint8_t i = 0;
while (str[i]) {
if (str[i] == '\n') {
str[i] = '\0';
break;
}
i++;
}
float value = custom_atof(str);
printf("%.2f", value);
return 0;
}
Input
123.45
Expected Output
123.45