Sync
This commit is contained in:
parent
6f050a029e
commit
776bba907c
3887 changed files with 59894 additions and 7280 deletions
|
|
@ -0,0 +1,58 @@
|
|||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. Is-Numeric.
|
||||
|
||||
DATA DIVISION.
|
||||
WORKING-STORAGE SECTION.
|
||||
01 Numeric-Chars PIC X(10) VALUE "0123456789".
|
||||
|
||||
01 Success CONSTANT 0.
|
||||
*> By convention, a non-zero value is used to signify failure
|
||||
01 Failure CONSTANT 128.
|
||||
|
||||
LOCAL-STORAGE SECTION.
|
||||
01 I PIC 99.
|
||||
|
||||
01 Num-Decimal-Points PIC 99.
|
||||
01 Num-Valid-Chars PIC 99.
|
||||
|
||||
LINKAGE SECTION.
|
||||
01 Str PIC X(30).
|
||||
|
||||
PROCEDURE DIVISION USING BY VALUE Str.
|
||||
|
||||
IF Str = SPACES
|
||||
MOVE Failure TO Return-Code
|
||||
|
||||
GOBACK
|
||||
END-IF
|
||||
|
||||
MOVE FUNCTION TRIM(Str) TO Str
|
||||
|
||||
INSPECT Str TALLYING Num-Decimal-Points FOR ALL "."
|
||||
IF Num-Decimal-Points > 1
|
||||
MOVE Failure TO Return-Code
|
||||
|
||||
GOBACK
|
||||
ELSE
|
||||
ADD Num-Decimal-Points TO Num-Valid-Chars
|
||||
END-IF
|
||||
|
||||
IF Str (1:1) = "-" OR "+"
|
||||
ADD 1 TO Num-Valid-Chars
|
||||
END-IF
|
||||
|
||||
PERFORM VARYING I FROM 1 BY 1 UNTIL I > 10
|
||||
INSPECT Str TALLYING Num-Valid-Chars
|
||||
FOR ALL Numeric-Chars (I:1) BEFORE SPACE
|
||||
END-PERFORM
|
||||
|
||||
INSPECT Str TALLYING Num-Valid-Chars FOR TRAILING SPACES
|
||||
|
||||
IF Num-Valid-Chars = FUNCTION LENGTH(Str)
|
||||
MOVE Success TO Return-Code
|
||||
ELSE
|
||||
MOVE Failure TO Return-Code
|
||||
END-IF
|
||||
|
||||
GOBACK
|
||||
.
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
program-id. is-numeric.
|
||||
procedure division.
|
||||
display function test-numval-f("abc") end-display
|
||||
display function test-numval-f("-123.01E+3") end-display
|
||||
if function test-numval-f("+123.123") equal zero then
|
||||
display "is numeric" end-display
|
||||
else
|
||||
display "failed numval-f test" end-display
|
||||
end-if
|
||||
goback.
|
||||
|
|
@ -1,16 +1,16 @@
|
|||
import std.stdio, std.string, std.conv, std.array, std.exception;
|
||||
|
||||
bool isNumeric(in string s) /*pure*/ {
|
||||
const s2 = s.strip().toLower().replace("_", "").replace(",", "");
|
||||
bool isNumeric(in string s) pure {
|
||||
immutable s2 = s.strip.toLower.replace("_", "").replace(",", "");
|
||||
try {
|
||||
s2.to!real();
|
||||
s2.to!real;
|
||||
} catch (ConvException e) {
|
||||
if (s2.startsWith("0x"))
|
||||
return !to!ulong(s2[2 .. $], 16)
|
||||
.collectException!ConvException();
|
||||
return !s2[2 .. $].to!ulong(16)
|
||||
.collectException!ConvException;
|
||||
else if (s2.startsWith("0b"))
|
||||
return !to!ulong(s2[2 .. $], 2)
|
||||
.collectException!ConvException();
|
||||
return !s2[2 .. $].to!ulong(2)
|
||||
.collectException!ConvException;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
|
@ -19,9 +19,9 @@ bool isNumeric(in string s) /*pure*/ {
|
|||
}
|
||||
|
||||
void main() {
|
||||
foreach (const s; ["12", " 12\t", "hello12", "-12", "02"
|
||||
foreach (immutable s; ["12", " 12\t", "hello12", "-12", "02"
|
||||
"0-12", "+12", "1.5", "1,000", "1_000",
|
||||
"0x10", "0b10101111_11110000_11110000_00110011",
|
||||
"-0b10101", "0x10.5"])
|
||||
writefln(`isNumeric("%s"): %s`, s, isNumeric(s));
|
||||
writefln(`isNumeric("%s"): %s`, s, s.isNumeric);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,9 @@
|
|||
is-numeric s:
|
||||
true
|
||||
try:
|
||||
drop to-num s
|
||||
catch value-error:
|
||||
not
|
||||
|
||||
for v in [ "1" "0" "3.14" "hello" "12e3" "12ef" "-3" ]:
|
||||
.( v is-numeric v )
|
||||
|
|
@ -1,4 +1,7 @@
|
|||
sub is-number( $term --> Bool ) {
|
||||
$term ~~ /\d/ and +$term ~~ Numeric;
|
||||
?($term ~~ /\d/) and +$term ~~ Numeric;
|
||||
}
|
||||
say "true" if is-number( 10111 );
|
||||
|
||||
printf "%10s %s\n", "<$_>", is-number( $_ ) for
|
||||
<1 1.2 1.2.3 -6 1/2 12e B17 1.3e+12 1.3e12 -2.6e-3 zero
|
||||
0x 0xA10 0b1001 0o16 0o18 2+5i>, '1 1 1', '', ' ';
|
||||
|
|
|
|||
|
|
@ -0,0 +1,4 @@
|
|||
strings = %w(0 0.0 -123 abc 0x10 0xABC 123a -123e3 0.1E-5 50e)
|
||||
strings.each do |str|
|
||||
puts "%9p => %s" % [str, is_numeric?(str)]
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue