first commit!
This commit is contained in:
48
atof.c
Normal file
48
atof.c
Normal file
@ -0,0 +1,48 @@
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define MAXLINE 100
|
||||
|
||||
int getlime(char s[], int lim)
|
||||
{
|
||||
int ch, i;
|
||||
for (i = 0; i < lim - 1 && (ch = getchar()) != EOF && ch != '\n'; ++i)
|
||||
s[i] = ch;
|
||||
if (ch == '\n') {
|
||||
s[i] = ch;
|
||||
++i;
|
||||
}
|
||||
s[i] = '\0';
|
||||
return i;
|
||||
}
|
||||
|
||||
double atof(char s[]) {
|
||||
double val, power;
|
||||
int ind, sign;
|
||||
|
||||
for (ind = 0; isspace(s[ind]); ++ind) /* skip whitespace */
|
||||
;
|
||||
sign = (s[ind] == '-') ? -1 : 1;
|
||||
if (s[ind] == '+' || s[ind] == '-')
|
||||
++ind;
|
||||
|
||||
for (val = 0.0; isdigit(s[ind]); ++ind)
|
||||
val = 10 * val + (s[ind] - '0');
|
||||
if(s[ind] == '.')
|
||||
++ind;
|
||||
for (power = 1.0; isdigit(s[ind]); ++ind) {
|
||||
val = 10.0 * val + (s[ind] - '0');
|
||||
power *= 10;
|
||||
}
|
||||
return sign * val / power;
|
||||
}
|
||||
|
||||
int main() {
|
||||
double sum = 0;
|
||||
char line[MAXLINE];
|
||||
|
||||
while (getlime(line, MAXLINE) > 0)
|
||||
printf("\t%g\n", sum += atof(line));
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user