Subversion Repositories wimsdev

Rev

Rev 5219 | Rev 5283 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5219 Rev 5227
Line 6... Line 6...
6
* can be used and distributed without restriction, including for commercial use *
6
* can be used and distributed without restriction, including for commercial use *
7
* No warrenty whatsoever                                                        *
7
* No warrenty whatsoever                                                        *
8
*********************************************************************************
8
*********************************************************************************
9
syntax number_1 number2 number_3 ... number_n
9
syntax number_1 number2 number_3 ... number_n
10
 
10
 
11
Treats a number as string array : no numrical evaluation !
11
Treats a number as string array : no numerical evaluation !
-
 
12
(pfffff...)
12
 
13
 
13
example
14
example
14
!exec sigdigits 1.23 1.230 1.2300 1.23e5 1.23*10^5 1.2300e5 01.23*10^5 1.2.3.4
15
!exec sigdigits 1.23 1.230 1.2300 1.23e5 1.23*10^5 1.2300e5 01.23*10^5 1.2.3.4
15
3,2,1,2,1,0,1
16
3,2,1,2,1,0,1
16
3,2,1,3,1,0,0
17
3,2,1,3,1,0,0
Line 36... Line 37...
36
item 5) exponent base (if not present : 1)
37
item 5) exponent base (if not present : 1)
37
 
38
 
38
item 6) exponent (if not present : 0)
39
item 6) exponent (if not present : 0)
39
 
40
 
40
item 7) indication : is the number correctly written ?  1 or 0  ( 000.1 is not correct...)
41
item 7) indication : is the number correctly written ?  1 or 0  ( 000.1 is not correct...)
-
 
42
        scientiffic: [1-9.*] *10^exp : 1.2345*10^5 is OK ... 12.345*10^4 or 0.12345*10^6 is "NOK"
-
 
43
        a*10^b  : 1 <= a <= 9
41
 
44
 
42
remarks:
45
remarks:
43
- exponent: any other base will be tolerated : 4*7^5  base=7 exponent=5 -> 1,0,1,0,7,5,1
46
- exponent: any other base will be tolerated : 4*7^5  base=7 exponent=5 -> 1,0,1,0,7,5,1
44
- if number is 'nonsense' : -1,-1,-1,-1,-1,-1 (1.23.4567  10^1.23.4 ; number will produce NaN in other math software)
47
- if number is 'nonsense' : -1,-1,-1,-1,-1,-1 (1.23.4567  10^1.23.4 ; number will produce NaN in other math software)
45
 
48
 
46
ruleset:
49
ruleset:
47
120.2           : 4 significant digits
50
120.2           : 4 significant digits
48
120.2000        : 4 significant digits
51
120.2000        : 4 significant digits
49
0120.2          : 4 significant digits
52
0120.2          : 4 significant digits
50
scientiffic notation:
53
scientiffic notation:
51
120.2*10^5      : 4 significant digits
54
1.202*10^5      : 4 significant digits
52
120.200*10^5    : 6 significant digits
55
1.20200*10^5    : 6 significant digits
-
 
56
 
53
 
57
 
54
*/
58
*/
55
 
59
 
56
#include <stdio.h>
60
#include <stdio.h>
57
#include <stdlib.h>
61
#include <stdlib.h>
Line 109... Line 113...
109
        zeros = 0; // leading or trailing zeros
113
        zeros = 0; // leading or trailing zeros
110
        points = 0; // number of points in number...
114
        points = 0; // number of points in number...
111
        exp[0]='\0';
115
        exp[0]='\0';
112
        base_start = 0;
116
        base_start = 0;
113
        base_end = 0;
117
        base_end = 0;
-
 
118
        ok = 0;
114
        for( i = length - 1 ; i >= 0 ; i--){ // walk from rightside to left through the 'number'
119
        for( i = length - 1 ; i >= 0 ; i--){ // walk from rightside to left through the 'number'
115
            switch( word[i] ){
120
            switch( word[i] ){
116
                case '^' : base_start = i;found_power++;break;
121
                case '^' : base_start = i;found_power++;break;
117
                case '*' :
122
                case '*' :
118
                    found_multiply++;
123
                    found_multiply++;
Line 174... Line 179...
174
            fprintf(stdout,"-1,-1,-1,-1,-1,-1,-1\n");
179
            fprintf(stdout,"-1,-1,-1,-1,-1,-1,-1\n");
175
        }
180
        }
176
        else
181
        else
177
        {
182
        {
178
            // extra check for handling "special cases" 
183
            // extra check for handling "special cases" 
179
            if(found_point == 0 && found_power == 0){ sig2 = length; }  // just a number 12345
184
            if(found_point == 0 && found_power == 0){ sig2 = length;ok = 1; }   // just a number 12345
-
 
185
            else
180
            if(found_point == 0 && found_multiply == 0 && found_power == 1){ sig1 = 0; dec1 = 0; sig2 = 0 ; dec2 = 0; } // 10^5
186
            if(found_point == 0 && found_multiply == 0 && found_power == 1){ sig1 = 0; dec1 = 0; sig2 = 0 ; dec2 = 0; } // 10^5
-
 
187
            else
181
            if(found_point == 1 && found_multiply == 0 && found_power == 1){ sig1 = 0; dec1 = 0; sig2 = 0 ; dec2 = 0; } // 10^5.1
188
            if(found_point == 1 && found_multiply == 0 && found_power == 1){ sig1 = 0; dec1 = 0; sig2 = 0 ; dec2 = 0; } // 10^5.1
-
 
189
            else
182
            if(found_point == 0 && found_multiply == 1 && found_power == 1){ dec1 = 0; dec2 = 0; sig2 = length - zeros - pow; } // 3*10^5
190
            if(found_point == 0 && found_multiply == 1 && found_power == 1){ dec1 = 0; dec2 = 0; sig2 = length - zeros - pow;}  // 3*10^5
183
           
191
           
184
            if( found_power == 1){
192
            if( found_power == 1){
-
 
193
                // find out if scientiffic number is correctly written ; ok=0 -> ok=1 
-
 
194
                // rule [1-9].[0-9]* x10^exp
185
                // correct writing ? 1.20000*10^5 is OK, 01.20000*10^5 is NOK
195
                if( word[0] != '0' && word[1] == '.' ){ //  0.120*10^5 => 1.20*10^4
-
 
196
                    ok = 1;
-
 
197
                }
-
 
198
                else
-
 
199
                {
-
 
200
                    if( (word[0] == '-' || word[0] == '+' ) && word[1] != '0' && word[2] == '.' ){
-
 
201
                        ok = 1; // -1.2*10^5
-
 
202
                    }
-
 
203
                    else
-
 
204
                    {
-
 
205
                        if( found_point == 0 && found_multiply == 1 ){
-
 
206
                            if( word[1] == '*' ){ //4*10^5
-
 
207
                                ok = 1;
-
 
208
                            }
-
 
209
                            else
-
 
210
                            {
186
                if( zeros != 0 ){ ok = 0; }else{ ok = 1; } // 01*10^5
211
                                if( (word[0] == '-' || word[0] == '+' ) && word[2] == '*'){ //-4*10^5
-
 
212
                                    ok = 1;
-
 
213
                                }
-
 
214
                            }
-
 
215
                        }
-
 
216
                    }
-
 
217
                }
187
                int len = strlen(exp);// reverse appended char array ... 123+ --> +321
218
                int len = strlen(exp);// reverse appended char array ... 123+ --> +321
188
                char exponent[len];
219
                char exponent[len];
189
                int w = len - 1;
220
                int w = len - 1;
190
                for(i = 0 ; i < len ; i++){
221
                for(i = 0 ; i < len ; i++){
191
                    exponent[i] = exp[w];
222
                    exponent[i] = exp[w];
Line 215... Line 246...
215
                }
246
                }
216
            }
247
            }
217
            else
248
            else
218
            {   // no exponent : base = '1' exponent = '0'
249
            {   // no exponent : base = '1' exponent = '0'
219
                //several possible correct way of writing...
250
                //several possible correct way of writing...
220
                if( ( sig1 == sig2 + dec2 ) || ( sig1 + dec1 == sig2 + dec2 ) || ( dec1 == dec2 && sig2 == 1 ) ){ ok  = 1; } else { ok = 0; }
251
                if( ok == 0 && ( ( sig1 == sig2 + dec2 ) || ( sig1 + dec1 == sig2 + dec2 ) || ( dec1 == dec2 && sig2 == 1 ) )){ ok  = 1; }
221
                fprintf(stdout,"%d,%d,%d,%d,1,0,%d\n",sig1,dec1,sig2,dec2,ok);
252
                fprintf(stdout,"%d,%d,%d,%d,1,0,%d\n",sig1,dec1,sig2,dec2,ok);
222
            }
253
            }
223
        }
254
        }
224
        cnt++;
255
        cnt++;
225
        input = argv[cnt];
256
        input = argv[cnt];