-
Notifications
You must be signed in to change notification settings - Fork 0
/
numcheck.c
131 lines (125 loc) · 3.12 KB
/
numcheck.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* =====================================================================================
*
* Filename: numcheck.c
*
* Description: check if a string is a number, and what kind of number it is.
*
* Version: 1.0
* Created: 05/06/2015 05:55:56 PM
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (),
* Organization:
*
* =====================================================================================
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
/*
* === FUNCTION ======================================================================
* Name: ifstrnum
* Description: retrun -1 if the string is not a valid number, return 0 if it is a
intergal, return 1 if it is a simple float number, return 2 if it is
a scientifc notation number.
* =====================================================================================
*/
int16_t ifstrnum(const char *str) {
int16_t i = 0;
if (str[i] == '-')
i++;
if (str[i] < '0' || str[i] > '9')
return -1;
while (str[i] >= '0' && str[i] <= '9')
i++;
if (str[i] == '\0')
return 0;
else if (str[i] == '.') {
i++;
if (str[i] == '\0')
return 1;
else if (str[i] >= '0' && str[i] <= '9') {
while (str[i] >= '0' && str[i] <= '9')
i++;
if (str[i] == '\0')
return 1;
}
if (str[i] != 'e' && str[i] != 'E')
return -1;
}
else if (str[i] != 'e' && str[i] != 'E')
return -1;
i++;
if (str[i] == '-')
i++;
if (str[i] < '0' || str[i] > '9')
return -1;
while (str[i] >= '0' && str[i] <= '9')
i++;
if (str[i] == '\0')
return 2;
else
return -1;
} /* ----- end of function ifstrnum ----- */
/*
* === FUNCTION ======================================================================
* Name: eatof
* Description: A function used for turning string into double presice float number,
which is compatible with scientific notation. If a invalid string is
inputted, the function would return 15498.
* =====================================================================================
*/
double eatof(const char *str) {
double a;
int16_t n, expo;
int8_t *cp;
int8_t buf[FILENAME_MAX];
n = ifstrnum(str);
switch (n) {
case 0:
return atof(str);
case 1:
return atof(str);
case 2:
strcpy(buf, str);
cp = strchr(buf, 'e');
if (cp == NULL)
cp = strchr(buf, 'E');
*cp = '\0';
cp++;
expo = atoi(cp);
a = atof(buf);
if (expo >= 0) {
while (expo > 0) {
a *= 10;
expo--;
}
}
else {
while (expo < 0) {
a /= 10;
expo++;
}
}
return a;
default:
return NAN;
}
} /* ----- end of function eatoi ----- */
/*
* === FUNCTION ======================================================================
* Name: main
* Description:
* =====================================================================================
*/
/*int main(int argc, char *argv[]) {
char buf[FILENAME_MAX];
while (1) {
scanf("%s", buf);
printf("%1.53lf\n", eatof(buf));
}
return EXIT_SUCCESS;
}*/ /* ---------- end of function main ---------- */