Rev 5360 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 5360 | Rev 12133 | ||
---|---|---|---|
Line 10... | Line 10... | ||
10 | 10 | ||
11 | Treats a number as string array : no numerical evaluation ! |
11 | Treats a number as string array : no numerical evaluation ! |
12 | (pfffff...) |
12 | (pfffff...) |
13 | 13 | ||
14 | example |
14 | example |
- | 15 | shell : ./sigdigits "1.23" "1.230" "1.2300" "1.23e5" "1.23*10^5' "1.2300e5" ".12340000" "01.23*10^5" "1.2.3.4" |
|
15 | !exec sigdigits |
16 | wims : !exec sigdigits 1.23 1.230 1.2300 1.23e5 1.23*10^5 1.2300e5 .12340000 01.23*10^5 1.2.3.4 |
16 | 3,1,2,1,0,1 |
17 | 3,1,2,1,0,1 |
17 | 4,1,3,1,0,1 |
18 | 4,1,3,1,0,1 |
18 | 5,1,4,1,0,1 |
19 | 5,1,4,1,0,1 |
19 | 3,1,2,10,5,1 |
20 | 3,1,2,10,5,1 |
20 | 3,1,2,10,5,1 |
21 | 3,1,2,10,5,1 |
21 | 5,1,4,10,5,1 |
22 | 5,1,4,10,5,1 |
- | 23 | 8,1,8,1,0,1 |
|
22 | 3,2,2,10,5,0 |
24 | 3,2,2,10,5,0 |
23 | -1,-1,-1,-1,-1,-1 |
25 | -1,-1,-1,-1,-1,-1 |
24 | 26 | ||
25 | result is |
27 | result is |
26 | 1 line per input number |
28 | 1 line per input number |
Line 46... | Line 48... | ||
46 | 48 | ||
47 | ruleset: |
49 | ruleset: |
48 | 120.2 : 4 significant digits |
50 | 120.2 : 4 significant digits |
49 | 120.2000 : 7 significant digits |
51 | 120.2000 : 7 significant digits |
50 | 0.0000120 : 3 significant digits |
52 | 0.0000120 : 3 significant digits |
- | 53 | .0000120 : 3 significant digits |
|
51 | scientiffic notation: |
54 | scientiffic notation: |
52 | 1.202*10^5 : 4 significant digits |
55 | 1.202*10^5 : 4 significant digits |
53 | 1.20200*10^5 : 6 significant digits |
56 | 1.20200*10^5 : 6 significant digits |
54 | 57 | ||
55 | 58 | ||
Line 137... | Line 140... | ||
137 | } |
140 | } |
138 | break; |
141 | break; |
139 | case 'e' : if(found_point == 1){points--;found_point = 0;} found_power++;pow = length - i;sig1 = 0;sig2 = 0;dec1 = 0;found_digit = 0;zeros = 0;found_multiply++;break; |
142 | case 'e' : if(found_point == 1){points--;found_point = 0;} found_power++;pow = length - i;sig1 = 0;sig2 = 0;dec1 = 0;found_digit = 0;zeros = 0;found_multiply++;break; |
140 | case 'E' : if(found_point == 1){points--;found_point = 0;} found_power++;pow = length - i;sig1 = 0;sig2 = 0;dec1 = 0;found_digit = 0;zeros = 0;found_multiply++;break; |
143 | case 'E' : if(found_point == 1){points--;found_point = 0;} found_power++;pow = length - i;sig1 = 0;sig2 = 0;dec1 = 0;found_digit = 0;zeros = 0;found_multiply++;break; |
141 | case '0' : |
144 | case '0' : |
142 | if(i == 0){ |
145 | if(i == 0){/* last char */ |
143 | sig1 = sig1 - zeros; |
146 | sig1 = sig1 - zeros; |
144 | sig2++; |
147 | sig2++; |
145 | if(found_power == 1){zeros++;} |
148 | if(found_power == 1){zeros++;} |
146 | } |
149 | } |
147 | else |
150 | else |
Line 156... | Line 159... | ||
156 | if( found_digit == 1 ){ sig1++; } |
159 | if( found_digit == 1 ){ sig1++; } |
157 | } |
160 | } |
158 | zeros++; |
161 | zeros++; |
159 | } |
162 | } |
160 | break; |
163 | break; |
- | 164 | case '.' : if( i == 0 ){ |
|
- | 165 | sig1 = sig1 - zeros;sig2++; |
|
- | 166 | if(found_power == 1){ zeros++; } |
|
- | 167 | } /* . is last: so assume 0.xxxxx */ |
|
161 |
|
168 | dec1 = length - i - pow - 1; |
- | 169 | found_point = 1;points++; |
|
- | 170 | break; |
|
162 | case '1' : sig1++;if(found_point == 1){sig2++;} found_digit = 1;zeros = 0; break; |
171 | case '1' : sig1++;if(found_point == 1){sig2++;} found_digit = 1;zeros = 0; break; |
163 | case '2' : sig1++;if(found_point == 1){sig2++;} found_digit = 1;zeros = 0; break; |
172 | case '2' : sig1++;if(found_point == 1){sig2++;} found_digit = 1;zeros = 0; break; |
164 | case '3' : sig1++;if(found_point == 1){sig2++;} found_digit = 1;zeros = 0; break; |
173 | case '3' : sig1++;if(found_point == 1){sig2++;} found_digit = 1;zeros = 0; break; |
165 | case '4' : sig1++;if(found_point == 1){sig2++;} found_digit = 1;zeros = 0; break; |
174 | case '4' : sig1++;if(found_point == 1){sig2++;} found_digit = 1;zeros = 0; break; |
166 | case '5' : sig1++;if(found_point == 1){sig2++;} found_digit = 1;zeros = 0; break; |
175 | case '5' : sig1++;if(found_point == 1){sig2++;} found_digit = 1;zeros = 0; break; |
Line 183... | Line 192... | ||
183 | fprintf(stdout,"-1,-1,-1,-1,-1,-1\n"); |
192 | fprintf(stdout,"-1,-1,-1,-1,-1,-1\n"); |
184 | } |
193 | } |
185 | else |
194 | else |
186 | { |
195 | { |
187 | // extra check for handling "special cases" |
196 | // extra check for handling "special cases" |
188 | if(found_point == 1 && found_power == 0 && found_multiply == 0 && word[ |
197 | if(found_point == 1 && found_power == 0 && found_multiply == 0 && ( word[1] == '.' || word[0] == '.' ) ){ok = 1;}// just a decimal number 0.1233 |
189 | else |
198 | else |
190 | if(found_point == 0 && found_power == 0){ sig2 = length;ok = 1; } // just a number 12345 |
199 | if(found_point == 0 && found_power == 0){ sig2 = length;ok = 1; } // just a number 12345 |
191 | else |
200 | else |
192 | if(found_point == 0 && found_multiply == 0 && found_power == 1){ sig1 = 0; sig2 = 0 ; dec1 = 0; } // 10^5 |
201 | if(found_point == 0 && found_multiply == 0 && found_power == 1){ sig1 = 0; sig2 = 0 ; dec1 = 0; } // 10^5 |
193 | else |
202 | else |
Line 196... | Line 205... | ||
196 | if(found_point == 0 && found_multiply == 1 && found_power == 1){ dec1 = 0; sig2 = length - zeros - pow;} // 3*10^5 |
205 | if(found_point == 0 && found_multiply == 1 && found_power == 1){ dec1 = 0; sig2 = length - zeros - pow;} // 3*10^5 |
197 | 206 | ||
198 | if( found_power == 1){ |
207 | if( found_power == 1){ |
199 | // find out if scientiffic number is correctly written ; ok=0 -> ok=1 |
208 | // find out if scientiffic number is correctly written ; ok=0 -> ok=1 |
200 | // rule [1-9].[0-9]* x10^exp |
209 | // rule [1-9].[0-9]* x10^exp |
201 | if( word[0] != '0' && word[1] == '.' ){ // 0.120*10^5 => 1.20*10^4 |
210 | if( word[0] != '0' && word[1] == '.' ){ // 0.120*10^5 => 1.20*10^4 |
202 | ok = 1; |
211 | ok = 1; |
203 | } |
212 | } |
204 | else |
213 | else |
205 | { |
214 | { |
206 | if( (word[0] == '-' || word[0] == '+' ) && word[1] != '0' && word[2] == '.' ){ |
215 | if( (word[0] == '-' || word[0] == '+' ) && word[1] != '0' && word[2] == '.' ){ |