Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
5
Task/Determine-if-a-string-is-numeric/00-META.yaml
Normal file
5
Task/Determine-if-a-string-is-numeric/00-META.yaml
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
category:
|
||||
- Simple
|
||||
from: http://rosettacode.org/wiki/Determine_if_a_string_is_numeric
|
||||
note: Text processing
|
||||
6
Task/Determine-if-a-string-is-numeric/00-TASK.txt
Normal file
6
Task/Determine-if-a-string-is-numeric/00-TASK.txt
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
;Task:
|
||||
Create a boolean function which takes in a string and tells whether it is a numeric string (floating point and negative numbers included) in the syntax the language uses for numeric literals or numbers converted from strings.
|
||||
|
||||
|
||||
{{Template:Strings}}
|
||||
<br><br>
|
||||
|
|
@ -0,0 +1,127 @@
|
|||
*=$0801
|
||||
db $0E,$08,$0A,$00,$9E,$20,$28,$32,$30,$36,$34,$29,$00,$00,$00 ;required init code on commodore 64 floppy disks
|
||||
*=$0810
|
||||
|
||||
lda #$0e
|
||||
jsr chrout ;required for my printing routine to work.
|
||||
|
||||
z_HL equ $02
|
||||
z_L equ $02
|
||||
z_H equ $03
|
||||
z_B equ $04
|
||||
|
||||
|
||||
|
||||
loadpair z_HL,TestString0
|
||||
jsr isStringNumeric
|
||||
|
||||
loadpair z_HL,TestString1
|
||||
jsr isStringNumeric
|
||||
|
||||
loadpair z_HL,TestString2
|
||||
jsr isStringNumeric
|
||||
|
||||
loadpair z_HL,TestString3
|
||||
jsr isStringNumeric
|
||||
|
||||
loadpair z_HL,TestString4
|
||||
jsr isStringNumeric
|
||||
|
||||
loadpair z_HL,TestString5
|
||||
jsr isStringNumeric
|
||||
|
||||
loadpair z_HL,TestString6
|
||||
jsr isStringNumeric
|
||||
|
||||
loadpair z_HL,TestString7
|
||||
jsr isStringNumeric
|
||||
|
||||
loadpair z_HL,TestString8
|
||||
jsr isStringNumeric
|
||||
rts ;return to basic
|
||||
|
||||
isStringNumeric:
|
||||
; input: z_HL = source address
|
||||
pushY
|
||||
ldy #0
|
||||
sty z_B ;our tally for decimal points
|
||||
|
||||
checkFirstChar:
|
||||
lda (z_HL),y
|
||||
beq notNumeric ;a null string is not a valid number!
|
||||
cmp #'-'
|
||||
beq isNegative_OK
|
||||
cmp #'.'
|
||||
beq isFloat_OK
|
||||
and #$30
|
||||
cmp #$30
|
||||
beq isNumeral_OK
|
||||
;else, is not numeric
|
||||
notNumeric:
|
||||
popY
|
||||
jsr PrintString_TextScreen ;prints what's already in z_HL
|
||||
jsr NewLine
|
||||
loadpair z_HL,isStringNumeric_Fail
|
||||
jsr PrintString_TextScreen
|
||||
jsr NewLine
|
||||
jmp NewLine
|
||||
;rts
|
||||
isNegative_OK:
|
||||
isNumeral_OK:
|
||||
iny
|
||||
jmp loop_isStringNumeric
|
||||
isFloat_OK:
|
||||
iny
|
||||
inc z_B
|
||||
loop_isStringNumeric:
|
||||
lda (z_HL),y
|
||||
beq Terminated_isStringNumeric
|
||||
cmp #'.'
|
||||
beq CheckIfDecimalAlreadyOccurred
|
||||
and #$30
|
||||
cmp #$30
|
||||
bne notNumeric
|
||||
loop_overhead_isStringNumeric:
|
||||
iny
|
||||
jmp loop_isStringNumeric
|
||||
|
||||
CheckIfDecimalAlreadyOccurred:
|
||||
lda z_B
|
||||
bne notNumeric
|
||||
inc z_B
|
||||
jmp loop_overhead_isStringNumeric
|
||||
|
||||
Terminated_isStringNumeric:
|
||||
;if we got this far the string is numeric.
|
||||
popY
|
||||
jsr PrintString_TextScreen ;prints what's already in z_HL
|
||||
jsr NewLine
|
||||
loadpair z_HL,isStringNumeric_Pass
|
||||
jsr PrintString_TextScreen
|
||||
jsr NewLine
|
||||
jmp NewLine
|
||||
;rts
|
||||
|
||||
isStringNumeric_Pass:
|
||||
db "IS NUMERIC",0
|
||||
isStringNumeric_Fail:
|
||||
db "IS NOT NUMERIC",0
|
||||
|
||||
TestString0:
|
||||
db 0
|
||||
TestString1:
|
||||
db "123",0
|
||||
TestString2:
|
||||
db "-30",0
|
||||
TestString3:
|
||||
db "123.45",0
|
||||
TestString4:
|
||||
db "-123.45",0
|
||||
TestString5:
|
||||
db "ABCDE",0
|
||||
TestString6:
|
||||
db "-34-5",0
|
||||
TestString7:
|
||||
db "1.000.000",0
|
||||
TestString8:
|
||||
db ".23456",0
|
||||
|
|
@ -0,0 +1 @@
|
|||
: number? >n >kind ns:n n:= ;
|
||||
|
|
@ -0,0 +1,203 @@
|
|||
/* ARM assembly Raspberry PI */
|
||||
/* program strNumber.s */
|
||||
|
||||
|
||||
/*******************************************/
|
||||
/* Constantes file */
|
||||
/*******************************************/
|
||||
/* for this file see task include a file in language AArch64 assembly*/
|
||||
.include "../includeConstantesARM64.inc"
|
||||
|
||||
.equ BUFFERSIZE, 100
|
||||
|
||||
|
||||
/* Initialized data */
|
||||
.data
|
||||
szMessNum: .asciz "Enter number : \n"
|
||||
|
||||
szMessError: .asciz "String is not a number !!!\n"
|
||||
szMessInteger: .asciz "String is a integer.\n"
|
||||
szMessFloat: .asciz "String is a float.\n"
|
||||
szMessFloatExp: .asciz "String is a float with exposant.\n"
|
||||
szCarriageReturn: .asciz "\n"
|
||||
|
||||
/* UnInitialized data */
|
||||
.bss
|
||||
sBuffer: .skip BUFFERSIZE
|
||||
|
||||
/* code section */
|
||||
.text
|
||||
.global main
|
||||
main:
|
||||
|
||||
loop:
|
||||
ldr x0,qAdrszMessNum
|
||||
bl affichageMess
|
||||
mov x0,#STDIN // Linux input console
|
||||
ldr x1,qAdrsBuffer // buffer address
|
||||
mov x2,#BUFFERSIZE // buffer size
|
||||
mov x8, #READ // request to read datas
|
||||
svc 0 // call system
|
||||
ldr x1,qAdrsBuffer // buffer address
|
||||
mov x2,#0 // end of string
|
||||
sub x0,x0,#1 // replace character 0xA
|
||||
strb w2,[x1,x0] // store byte at the end of input string (x0 contains number of characters)
|
||||
ldr x0,qAdrsBuffer
|
||||
bl controlNumber // call routine
|
||||
cmp x0,#0
|
||||
bne 1f
|
||||
ldr x0,qAdrszMessError // not a number
|
||||
bl affichageMess
|
||||
b 5f
|
||||
1:
|
||||
cmp x0,#1
|
||||
bne 2f
|
||||
ldr x0,qAdrszMessInteger // integer
|
||||
bl affichageMess
|
||||
b 5f
|
||||
2:
|
||||
cmp x0,#2
|
||||
bne 3f
|
||||
ldr x0,qAdrszMessFloat // float
|
||||
bl affichageMess
|
||||
b 5f
|
||||
3:
|
||||
cmp x0,#3
|
||||
bne 5f
|
||||
ldr x0,qAdrszMessFloatExp // float with exposant
|
||||
bl affichageMess
|
||||
5:
|
||||
b loop
|
||||
|
||||
100: // standard end of the program
|
||||
mov x0, #0 // return code
|
||||
mov x8, #EXIT // request to exit program
|
||||
svc 0 // perform system call
|
||||
qAdrszMessNum: .quad szMessNum
|
||||
qAdrszMessError: .quad szMessError
|
||||
qAdrszMessInteger: .quad szMessInteger
|
||||
qAdrszMessFloat: .quad szMessFloat
|
||||
qAdrszMessFloatExp: .quad szMessFloatExp
|
||||
qAdrszCarriageReturn: .quad szCarriageReturn
|
||||
qAdrsBuffer: .quad sBuffer
|
||||
/******************************************************************/
|
||||
/* control if string is number */
|
||||
/******************************************************************/
|
||||
/* x0 contains the address of the string */
|
||||
/* x0 return 0 if not a number */
|
||||
/* x0 return 1 if integer eq 12345 or -12345 */
|
||||
/* x0 return 2 if float eq 123.45 or 123,45 or -123,45 */
|
||||
/* x0 return 3 if float with exposant eq 123.45E30 or -123,45E-30 */
|
||||
controlNumber:
|
||||
stp x1,lr,[sp,-16]! // save registers
|
||||
stp x2,x3,[sp,-16]! // save registers
|
||||
stp x4,x5,[sp,-16]! // save registers
|
||||
mov x1,#0
|
||||
mov x3,#0 // point counter
|
||||
1:
|
||||
ldrb w2,[x0,x1]
|
||||
cmp x2,#0 // end string ?
|
||||
beq 7f
|
||||
cmp x2,#' ' // space ?
|
||||
bne 3f
|
||||
add x1,x1,#1
|
||||
b 1b // loop
|
||||
3:
|
||||
cmp x2,#'-' // negative ?
|
||||
bne 4f
|
||||
add x1,x1,#1
|
||||
b 5f
|
||||
4:
|
||||
cmp x2,#'+' // positive ?
|
||||
bne 5f
|
||||
add x1,x1,#1
|
||||
5:
|
||||
ldrb w2,[x0,x1] // control space
|
||||
cmp x2,#0 // end ?
|
||||
beq 7f
|
||||
cmp x2,#' ' // space ?
|
||||
bne 6f
|
||||
add x1,x1,#1
|
||||
b 5b // loop
|
||||
6:
|
||||
ldrb w2,[x0,x1]
|
||||
cmp x2,#0 // end ?
|
||||
beq 14f
|
||||
cmp x2,#'E' // exposant ?
|
||||
beq 9f
|
||||
cmp x2,#'e' // exposant ?
|
||||
beq 9f
|
||||
cmp x2,#'.' // point ?
|
||||
bne 7f
|
||||
add x3,x3,#1 // yes increment counter
|
||||
add x1,x1,#1
|
||||
b 6b // and loop
|
||||
7:
|
||||
cmp x2,#',' // comma ?
|
||||
bne 8f
|
||||
add x3,x3,#1 // yes increment counter
|
||||
add x1,x1,#1
|
||||
b 6b // and loop
|
||||
8:
|
||||
cmp x2,#'0' // control digit < 0
|
||||
blt 99f
|
||||
cmp x2,#'9' // control digit > 0
|
||||
bgt 99f
|
||||
add x1,x1,#1 // no error loop digit
|
||||
b 6b
|
||||
|
||||
9: // float with exposant
|
||||
add x1,x1,#1
|
||||
ldrb w2,[x0,x1]
|
||||
cmp x2,#0 // end ?
|
||||
beq 99f
|
||||
|
||||
cmp x2,#'-' // negative exposant ?
|
||||
bne 10f
|
||||
add x1,x1,#1
|
||||
10:
|
||||
mov x4,#0 // nombre de chiffres
|
||||
11:
|
||||
ldrb w2,[x0,x1]
|
||||
cmp x2,#0 // end ?
|
||||
beq 13f
|
||||
cmp x2,#'0' // control digit < 0
|
||||
blt 99f
|
||||
cmp x2,#'9' // control digit > 9
|
||||
bgt 99f
|
||||
add x1,x1,#1
|
||||
add x4,x4,#1 // counter digit
|
||||
b 11b // and loop
|
||||
|
||||
13:
|
||||
cmp x4,#0 // number digit exposant = 0 -> error
|
||||
beq 99f // error
|
||||
cmp x4,#2 // number digit exposant > 2 -> error
|
||||
bgt 99f // error
|
||||
|
||||
mov x0,#3 // valid float with exposant
|
||||
b 100f
|
||||
14:
|
||||
cmp x3,#0
|
||||
bne 15f
|
||||
mov x0,#1 // valid integer
|
||||
b 100f
|
||||
15:
|
||||
cmp x3,#1 // number of point or comma = 1 ?
|
||||
blt 100f
|
||||
bgt 99f // error
|
||||
mov x0,#2 // valid float
|
||||
b 100f
|
||||
99:
|
||||
mov x0,#0 // error
|
||||
100:
|
||||
ldp x4,x5,[sp],16 // restaur 2 registres
|
||||
ldp x2,x3,[sp],16 // restaur 2 registres
|
||||
ldp x1,lr,[sp],16 // restaur 2 registres
|
||||
ret
|
||||
|
||||
/********************************************************/
|
||||
/* File Include fonctions */
|
||||
/********************************************************/
|
||||
/* for this file see task include a file in language AArch64 assembly */
|
||||
.include "../includeARM64.inc"
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
PROC is numeric = (REF STRING string) BOOL: (
|
||||
BOOL out := TRUE;
|
||||
PROC call back false = (REF FILE f)BOOL: (out:= FALSE; TRUE);
|
||||
|
||||
FILE memory;
|
||||
associate(memory, string);
|
||||
on value error(memory, call back false);
|
||||
on logical file end(memory, call back false);
|
||||
|
||||
UNION (INT, REAL, COMPL) numeric:=0.0;
|
||||
# use a FORMAT pattern instead of a regular expression #
|
||||
getf(memory, ($gl$, numeric));
|
||||
out
|
||||
);
|
||||
|
||||
test:(
|
||||
STRING
|
||||
s1 := "152",
|
||||
s2 := "-3.1415926",
|
||||
s3 := "Foo123";
|
||||
print((
|
||||
s1, " results in ", is numeric(s1), new line,
|
||||
s2, " results in ", is numeric(s2), new line,
|
||||
s3, " results in ", is numeric(s3), new line
|
||||
))
|
||||
)
|
||||
|
|
@ -0,0 +1,135 @@
|
|||
begin
|
||||
|
||||
% determnines whether the string contains an integer, real or imaginary %
|
||||
% number. Returns true if it does, false otherwise %
|
||||
logical procedure isNumeric( string(32) value text ) ;
|
||||
begin
|
||||
|
||||
logical ok;
|
||||
% the "number" cannot be blank %
|
||||
ok := ( text not = " " );
|
||||
if ok then begin
|
||||
|
||||
% there is at least one non-blank character %
|
||||
% must have either an integer or real/immaginary number %
|
||||
% integer: [+|-]digit-sequence %
|
||||
% real: [+|-][digit-sequence].digit-sequence['integer][L] %
|
||||
% or: [+|-]digit-sequence[.[digit-sequence]]'integer[L] %
|
||||
% imaginary: %
|
||||
% [+|-][digit-sequence].digit-sequence['integer][L]I%
|
||||
% or: [+|-]digit-sequence[.[digit-sequence]]'integer[L]I%
|
||||
% The "I" at the end of an imaginary number can appear %
|
||||
% before or after the "L" (which indicates a long number) %
|
||||
% the "I" and "L" can be in either case %
|
||||
|
||||
procedure nextChar ; charPos := charPos + 1;
|
||||
logical procedure have( string(1) value ch ) ;
|
||||
( charPos <= maxChar and text(charPos//1) = ch ) ;
|
||||
|
||||
logical procedure haveDigit ;
|
||||
( charPos <= maxChar and text(charPos//1) >= "0" and text(charPos//1) <= "9" ) ;
|
||||
|
||||
|
||||
integer charPos, maxChar;
|
||||
logical hadDigits, isReal;
|
||||
charPos := 0;
|
||||
maxChar := 31;
|
||||
hadDigits := false;
|
||||
isReal := false;
|
||||
|
||||
% skip trailing spaces %
|
||||
while maxChar > 0 and text(maxChar//1) = " " do maxChar := maxChar - 1;
|
||||
% skip leading spacesx %
|
||||
while have( " " ) do nextChar;
|
||||
|
||||
% skip optional sign %
|
||||
if have( "+" ) or have( "-" ) then nextChar;
|
||||
|
||||
if haveDigit then begin
|
||||
% have a digit sequence %
|
||||
hadDigits := true;
|
||||
while haveDigit do nextChar
|
||||
end if_have_sign ;
|
||||
|
||||
if have( "." ) then begin
|
||||
% real or imaginary number %
|
||||
nextChar;
|
||||
isReal := true;
|
||||
hadDigits := hadDigits or haveDigit;
|
||||
while haveDigit do nextChar
|
||||
end if_have_point ;
|
||||
|
||||
% should have had some digits %
|
||||
ok := hadDigits;
|
||||
|
||||
if ok and have( "'" ) then begin
|
||||
% the number has an exponent %
|
||||
isReal := true;
|
||||
nextChar;
|
||||
% skip optional sign %
|
||||
if have( "+" ) or have( "-" ) then nextChar;
|
||||
% must have a digit sequence %
|
||||
ok := haveDigit;
|
||||
while haveDigit do nextChar;
|
||||
end if_ok_and_have_exponent ;
|
||||
|
||||
% if it is a real number, there could be L/I suffixes %
|
||||
if ok and isReal then begin
|
||||
integer LCount, ICount;
|
||||
LCount := 0;
|
||||
ICount := 0;
|
||||
while have( "L" ) or have( "l" ) or have( "I" ) or have( "i" ) do begin
|
||||
if have( "L" ) or have( "l" )
|
||||
then LCount := LCount + 1
|
||||
else ICount := ICount + 1;
|
||||
nextChar
|
||||
end while_have_L_or_I ;
|
||||
% there can be at most one L and at most 1 I %
|
||||
ok := ( LCount < 2 and ICount < 2 )
|
||||
end if_ok_and_isReal ;
|
||||
|
||||
% must now be at the end if the number %
|
||||
ok := ok and charPos >= maxChar
|
||||
|
||||
end if_ok ;
|
||||
|
||||
ok
|
||||
end isNumeric ;
|
||||
|
||||
|
||||
% test the isNumeric procedure %
|
||||
procedure testIsNumeric( string(32) value n
|
||||
; logical value expectedResult
|
||||
) ;
|
||||
begin
|
||||
logical actualResult;
|
||||
actualResult := isNumeric( n );
|
||||
write( s_w := 0
|
||||
, """", n, """ is "
|
||||
, if actualResult then "" else "not "
|
||||
, "numeric "
|
||||
, if actualResult = expectedResult then "" else " NOT "
|
||||
, "as expected"
|
||||
)
|
||||
end testIsNumeric ;
|
||||
|
||||
|
||||
testIsNumeric( "", false );
|
||||
testIsNumeric( "b", false );
|
||||
testIsNumeric( ".", false );
|
||||
testIsNumeric( ".'3", false );
|
||||
testIsNumeric( "3.'", false );
|
||||
testIsNumeric( "0.0z44", false );
|
||||
testIsNumeric( "-1IL", false );
|
||||
testIsNumeric( "4.5'23ILL", false );
|
||||
|
||||
write( "---------" );
|
||||
|
||||
testIsNumeric( "-1", true );
|
||||
testIsNumeric( " +.345", true );
|
||||
testIsNumeric( "4.5'23I", true );
|
||||
testIsNumeric( "-5'+3i", true );
|
||||
testIsNumeric( "-5'-3l", true );
|
||||
testIsNumeric( " -.345LI", true );
|
||||
|
||||
end.
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
⊃⎕VFI{w←⍵⋄((w='-')/w)←'¯'⋄w}'152 -3.1415926 Foo123'
|
||||
|
||||
1 1 0
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
⊃⎕VFI '¯' @ ('-'∘=) '152 -3.1415926 Foo123' ⍝ Fast: replacement of - with APL high-minus required for ⎕VFI
|
||||
1 1 0
|
||||
⊃⎕VFI '-' ⎕R '¯' ⊣ '152 -3.1415926 Foo123' ⍝ Simple: (ditto)
|
||||
1 1 0
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
{∧/⍵∊(⊃,¨'0123456789¯.+')}¨'152' '¯3.1415926' 'Foo123'
|
||||
1 1 0
|
||||
|
|
@ -0,0 +1,199 @@
|
|||
/* ARM assembly Raspberry PI */
|
||||
/* program strNumber.s */
|
||||
|
||||
/* Constantes */
|
||||
.equ STDIN, 0 @ Linux input console
|
||||
.equ STDOUT, 1 @ Linux output console
|
||||
.equ EXIT, 1 @ Linux syscall
|
||||
.equ READ, 3 @ Linux syscall
|
||||
.equ WRITE, 4 @ Linux syscall
|
||||
|
||||
.equ BUFFERSIZE, 100
|
||||
|
||||
|
||||
/* Initialized data */
|
||||
.data
|
||||
szMessNum: .asciz "Enter number : \n"
|
||||
|
||||
szMessError: .asciz "String is not a number !!!\n"
|
||||
szMessInteger: .asciz "String is a integer.\n"
|
||||
szMessFloat: .asciz "String is a float.\n"
|
||||
szMessFloatExp: .asciz "String is a float with exposant.\n"
|
||||
szCarriageReturn: .asciz "\n"
|
||||
|
||||
/* UnInitialized data */
|
||||
.bss
|
||||
sBuffer: .skip BUFFERSIZE
|
||||
|
||||
/* code section */
|
||||
.text
|
||||
.global main
|
||||
main:
|
||||
|
||||
loop:
|
||||
ldr r0,iAdrszMessNum
|
||||
bl affichageMess
|
||||
mov r0,#STDIN @ Linux input console
|
||||
ldr r1,iAdrsBuffer @ buffer address
|
||||
mov r2,#BUFFERSIZE @ buffer size
|
||||
mov r7, #READ @ request to read datas
|
||||
swi 0 @ call system
|
||||
ldr r1,iAdrsBuffer @ buffer address
|
||||
mov r2,#0 @ end of string
|
||||
sub r0,#1 @ replace character 0xA
|
||||
strb r2,[r1,r0] @ store byte at the end of input string (r0 contains number of characters)
|
||||
ldr r0,iAdrsBuffer
|
||||
bl controlNumber @ call routine
|
||||
cmp r0,#0
|
||||
bne 1f
|
||||
ldr r0,iAdrszMessError @ not a number
|
||||
bl affichageMess
|
||||
b 5f
|
||||
1:
|
||||
cmp r0,#1
|
||||
bne 2f
|
||||
ldr r0,iAdrszMessInteger @ integer
|
||||
bl affichageMess
|
||||
b 5f
|
||||
2:
|
||||
cmp r0,#2
|
||||
bne 3f
|
||||
ldr r0,iAdrszMessFloat @ float
|
||||
bl affichageMess
|
||||
b 5f
|
||||
3:
|
||||
cmp r0,#3
|
||||
bne 5f
|
||||
ldr r0,iAdrszMessFloatExp @ float with exposant
|
||||
bl affichageMess
|
||||
5:
|
||||
b loop
|
||||
|
||||
100: @ standard end of the program
|
||||
mov r0, #0 @ return code
|
||||
mov r7, #EXIT @ request to exit program
|
||||
svc 0 @ perform system call
|
||||
iAdrszMessNum: .int szMessNum
|
||||
iAdrszMessError: .int szMessError
|
||||
iAdrszMessInteger: .int szMessInteger
|
||||
iAdrszMessFloat: .int szMessFloat
|
||||
iAdrszMessFloatExp: .int szMessFloatExp
|
||||
iAdrszCarriageReturn: .int szCarriageReturn
|
||||
iAdrsBuffer: .int sBuffer
|
||||
/******************************************************************/
|
||||
/* control if string is number */
|
||||
/******************************************************************/
|
||||
/* r0 contains the address of the string */
|
||||
/* r0 return 0 if not a number */
|
||||
/* r0 return 1 if integer eq 12345 or -12345 */
|
||||
/* r0 return 2 if float eq 123.45 or 123,45 or -123,45 */
|
||||
/* r0 return 3 if float with exposant eq 123.45E30 or -123,45E-30 */
|
||||
controlNumber:
|
||||
push {r1-r4,lr} @ save registers
|
||||
mov r1,#0
|
||||
mov r3,#0 @ point counter
|
||||
1:
|
||||
ldrb r2,[r0,r1]
|
||||
cmp r2,#0
|
||||
beq 5f
|
||||
cmp r2,#' '
|
||||
addeq r1,#1
|
||||
beq 1b
|
||||
cmp r2,#'-' @ negative ?
|
||||
addeq r1,#1
|
||||
beq 2f
|
||||
cmp r2,#'+' @ positive ?
|
||||
addeq r1,#1
|
||||
2:
|
||||
ldrb r2,[r0,r1] @ control space
|
||||
cmp r2,#0 @ end ?
|
||||
beq 5f
|
||||
cmp r2,#' '
|
||||
addeq r1,#1
|
||||
beq 2b
|
||||
3:
|
||||
ldrb r2,[r0,r1]
|
||||
cmp r2,#0 @ end ?
|
||||
beq 10f
|
||||
cmp r2,#'E' @ exposant ?
|
||||
beq 6f
|
||||
cmp r2,#'e' @ exposant ?
|
||||
beq 6f
|
||||
cmp r2,#'.' @ point ?
|
||||
addeq r3,#1 @ yes increment counter
|
||||
addeq r1,#1
|
||||
beq 3b
|
||||
cmp r2,#',' @ comma ?
|
||||
addeq r3,#1 @ yes increment counter
|
||||
addeq r1,#1
|
||||
beq 3b
|
||||
cmp r2,#'0' @ control digit < 0
|
||||
blt 5f
|
||||
cmp r2,#'9' @ control digit > 0
|
||||
bgt 5f
|
||||
add r1,#1 @ no error loop digit
|
||||
b 3b
|
||||
5: @ error detected
|
||||
mov r0,#0
|
||||
b 100f
|
||||
6: @ float with exposant
|
||||
add r1,#1
|
||||
ldrb r2,[r0,r1]
|
||||
cmp r2,#0 @ end ?
|
||||
moveq r0,#0 @ error
|
||||
beq 100f
|
||||
cmp r2,#'-' @ negative exposant ?
|
||||
addeq r1,#1
|
||||
mov r4,#0 @ nombre de chiffres
|
||||
7:
|
||||
ldrb r2,[r0,r1]
|
||||
cmp r2,#0 @ end ?
|
||||
beq 9f
|
||||
cmp r2,#'0' @ control digit < 0
|
||||
blt 8f
|
||||
cmp r2,#'9' @ control digit > 0
|
||||
bgt 8f
|
||||
add r1,#1
|
||||
add r4,#1 @ counter digit
|
||||
b 7b
|
||||
8:
|
||||
mov r0,#0
|
||||
b 100f
|
||||
9:
|
||||
cmp r4,#0 @ number digit exposant = 0 -> error
|
||||
moveq r0,#0 @ erreur
|
||||
beq 100f
|
||||
cmp r4,#2 @ number digit exposant > 2 -> error
|
||||
movgt r0,#0 @ error
|
||||
bgt 100f
|
||||
mov r0,#3 @ valid float with exposant
|
||||
b 100f
|
||||
10:
|
||||
cmp r3,#0
|
||||
moveq r0,#1 @ valid integer
|
||||
beq 100f
|
||||
cmp r3,#1 @ number of point or comma = 1 ?
|
||||
moveq r0,#2 @ valid float
|
||||
movgt r0,#0 @ error
|
||||
100:
|
||||
pop {r1-r4,lr} @ restaur des 2 registres
|
||||
bx lr @ return
|
||||
/******************************************************************/
|
||||
/* display text with size calculation */
|
||||
/******************************************************************/
|
||||
/* r0 contains the address of the message */
|
||||
affichageMess:
|
||||
push {r0,r1,r2,r7,lr} @ save registers
|
||||
mov r2,#0 @ counter length */
|
||||
1: @ loop length calculation
|
||||
ldrb r1,[r0,r2] @ read octet start position + index
|
||||
cmp r1,#0 @ if 0 its over
|
||||
addne r2,r2,#1 @ else add 1 in the length
|
||||
bne 1b @ and loop
|
||||
@ so here r2 contains the length of the message
|
||||
mov r1,r0 @ address message in r1
|
||||
mov r0,#STDOUT @ code to write to the standard output Linux
|
||||
mov r7, #WRITE @ code call system "write"
|
||||
svc #0 @ call system
|
||||
pop {r0,r1,r2,r7,lr} @ restaur registers
|
||||
bx lr @ return
|
||||
|
|
@ -0,0 +1 @@
|
|||
$ awk 'function isnum(x){return(x==x+0)} BEGIN{print isnum("hello"),isnum("-42")}'
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
|
||||
|
||||
BYTE FUNC AreEqual(CHAR ARRAY a,b)
|
||||
BYTE i
|
||||
|
||||
IF a(0)#b(0) THEN
|
||||
RETURN (0)
|
||||
FI
|
||||
FOR i=1 to a(0)
|
||||
DO
|
||||
IF a(i)#b(i) THEN
|
||||
RETURN (0)
|
||||
FI
|
||||
OD
|
||||
RETURN (1)
|
||||
|
||||
BYTE FUNC IsNumeric(CHAR ARRAY s)
|
||||
CHAR ARRAY tmp(20)
|
||||
INT i
|
||||
CARD c
|
||||
REAL r
|
||||
|
||||
i=ValI(s)
|
||||
StrI(i,tmp)
|
||||
IF AreEqual(s,tmp) THEN
|
||||
RETURN (1)
|
||||
FI
|
||||
|
||||
c=ValC(s)
|
||||
StrC(c,tmp)
|
||||
IF AreEqual(s,tmp) THEN
|
||||
RETURN (1)
|
||||
FI
|
||||
|
||||
ValR(s,r)
|
||||
StrR(r,tmp)
|
||||
IF AreEqual(s,tmp) THEN
|
||||
RETURN (1)
|
||||
FI
|
||||
RETURN (0)
|
||||
|
||||
PROC Test(CHAR ARRAY s)
|
||||
BYTE res
|
||||
|
||||
res=IsNumeric(s)
|
||||
Print(s)
|
||||
Print(" is ")
|
||||
IF res=0 THEN
|
||||
Print("not ")
|
||||
FI
|
||||
PrintE("a number.")
|
||||
RETURN
|
||||
|
||||
PROC Main()
|
||||
Put(125) PutE() ;clear the screen
|
||||
Test("56233")
|
||||
Test("-315")
|
||||
Test("1.36")
|
||||
Test("-5.126")
|
||||
Test("3.7E-05")
|
||||
Test("1.23BC")
|
||||
Test("5.6.3")
|
||||
RETURN
|
||||
|
|
@ -0,0 +1,140 @@
|
|||
BYTE FUNC IsSign(CHAR c)
|
||||
IF c='- OR c='+ THEN
|
||||
RETURN (1)
|
||||
FI
|
||||
RETURN (0)
|
||||
|
||||
BYTE FUNC IsDigit(CHAR c)
|
||||
IF c>='0 AND c<='9 THEN
|
||||
RETURN (1)
|
||||
FI
|
||||
RETURN (0)
|
||||
|
||||
BYTE FUNC IsDot(CHAR c)
|
||||
IF c='. THEN
|
||||
RETURN (1)
|
||||
FI
|
||||
RETURN (0)
|
||||
|
||||
BYTE FUNC IsExpSymbol(CHAR c)
|
||||
IF c='E OR c='e THEN
|
||||
RETURN (1)
|
||||
FI
|
||||
RETURN (0)
|
||||
|
||||
BYTE FUNC IsNumeric(CHAR ARRAY s)
|
||||
DEFINE S_BEGIN="0"
|
||||
DEFINE S_SIGN="1"
|
||||
DEFINE S_BEFORE_DOT="2"
|
||||
DEFINE S_DOT="3"
|
||||
DEFINE S_AFTER_DOT="4"
|
||||
DEFINE S_EXP_SYMBOL="5"
|
||||
DEFINE S_EXP_SIGN="6"
|
||||
DEFINE S_EXP="7"
|
||||
|
||||
BYTE i,state
|
||||
CHAR c
|
||||
|
||||
i=1
|
||||
state=S_BEGIN
|
||||
WHILE i<=s(0)
|
||||
DO
|
||||
c=s(i)
|
||||
|
||||
IF state=S_BEGIN THEN
|
||||
IF IsSign(c) THEN
|
||||
state=S_SIGN
|
||||
ELSEIF IsDigit(c) THEN
|
||||
state=S_BEFORE_DOT
|
||||
ELSEIF IsDot(c) THEN
|
||||
state=S_DOT
|
||||
ELSE
|
||||
RETURN (0)
|
||||
FI
|
||||
ELSEIF state=S_SIGN THEN
|
||||
IF IsDigit(c) THEN
|
||||
state=S_BEFORE_DOT
|
||||
ELSEIF IsDot(c) THEN
|
||||
state=S_DOT
|
||||
ELSE
|
||||
RETURN (0)
|
||||
FI
|
||||
ELSEIF state=S_BEFORE_DOT THEN
|
||||
IF IsDigit(c) THEN
|
||||
state=S_BEFORE_DOT
|
||||
ELSEIF IsDot(c) THEN
|
||||
state=S_DOT
|
||||
ELSEIF IsExpSymbol(c) THEN
|
||||
state=S_EXP_SYMBOL
|
||||
ELSE
|
||||
RETURN (0)
|
||||
FI
|
||||
ELSEIF state=S_DOT THEN
|
||||
IF IsDigit(c) THEN
|
||||
state=S_AFTER_DOT
|
||||
ELSEIF IsExpSymbol(c) THEN
|
||||
state=S_EXP_SYMBOL
|
||||
ELSE
|
||||
RETURN (0)
|
||||
FI
|
||||
ELSEIF state=S_AFTER_DOT THEN
|
||||
IF IsDigit(c) THEN
|
||||
state=S_AFTER_DOT
|
||||
ELSEIF IsExpSymbol(c) THEN
|
||||
state=S_EXP_SYMBOL
|
||||
ELSE
|
||||
RETURN (0)
|
||||
FI
|
||||
ELSEIF state=S_EXP_SYMBOL THEN
|
||||
IF IsSign(c) THEN
|
||||
state=S_EXP_SIGN
|
||||
ELSEIF IsDigit(c) THEN
|
||||
state=S_EXP
|
||||
ELSE
|
||||
RETURN (0)
|
||||
FI
|
||||
ELSEIF state=S_EXP_SIGN THEN
|
||||
IF IsDigit(c) THEN
|
||||
state=S_EXP
|
||||
ELSE
|
||||
RETURN (0)
|
||||
FI
|
||||
ELSEIF state=S_EXP THEN
|
||||
IF IsDigit(c) THEN
|
||||
state=S_EXP
|
||||
ELSE
|
||||
RETURN (0)
|
||||
FI
|
||||
ELSE
|
||||
RETURN (0)
|
||||
FI
|
||||
i==+1
|
||||
OD
|
||||
|
||||
IF state=S_BEGIN OR state=S_DOT OR
|
||||
state=S_EXP_SIGN OR state=S_EXP_SIGN THEN
|
||||
RETURN (0)
|
||||
FI
|
||||
RETURN (1)
|
||||
|
||||
PROC Test(CHAR ARRAY s)
|
||||
BYTE res
|
||||
|
||||
res=IsNumeric(s)
|
||||
Print(s)
|
||||
Print(" is ")
|
||||
IF res=0 THEN
|
||||
Print("not ")
|
||||
FI
|
||||
PrintE("a number.")
|
||||
RETURN
|
||||
|
||||
PROC Main()
|
||||
Test("56233")
|
||||
Test("-315")
|
||||
Test("1.36")
|
||||
Test("-5.126")
|
||||
Test("3.7E-05")
|
||||
Test("1.23BC")
|
||||
Test("5.6.3")
|
||||
RETURN
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
public function isNumeric(num:String):Boolean
|
||||
{
|
||||
return !isNaN(parseInt(num));
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
package Numeric_Tests is
|
||||
function Is_Numeric (Item : in String) return Boolean;
|
||||
end Numeric_Tests;
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
package body Numeric_Tests is
|
||||
function Is_Numeric (Item : in String) return Boolean is
|
||||
Dummy : Float;
|
||||
begin
|
||||
Dummy := Float'Value (Item);
|
||||
return True;
|
||||
exception
|
||||
when others =>
|
||||
return False;
|
||||
end Is_Numeric;
|
||||
end Numeric_Tests;
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
with Ada.Text_Io; use Ada.Text_Io;
|
||||
with Numeric_Tests; use Numeric_Tests;
|
||||
|
||||
procedure Is_Numeric_Test is
|
||||
S1 : String := "152";
|
||||
S2 : String := "-3.1415926";
|
||||
S3 : String := "Foo123";
|
||||
begin
|
||||
Put_Line(S1 & " results in " & Boolean'Image(Is_Numeric(S1)));
|
||||
Put_Line(S2 & " results in " & Boolean'Image(Is_Numeric(S2)));
|
||||
Put_Line(S3 & " results in " & Boolean'Image(Is_Numeric(S3)));
|
||||
end Is_Numeric_Test;
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
integer
|
||||
is_numeric(text s)
|
||||
{
|
||||
return !trap_q(alpha, s, 0);
|
||||
}
|
||||
|
||||
integer
|
||||
main(void)
|
||||
{
|
||||
if (!is_numeric("8192&*")) {
|
||||
o_text("Not numeric.\n");
|
||||
}
|
||||
if (is_numeric("8192")) {
|
||||
o_text("Numeric.\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
String numericString = '123456';
|
||||
String partlyNumericString = '123DMS';
|
||||
String decimalString = '123.456';
|
||||
|
||||
System.debug(numericString.isNumeric()); // this will be true
|
||||
System.debug(partlyNumericString.isNumeric()); // this will be false
|
||||
System.debug(decimalString.isNumeric()); // this will be false
|
||||
System.debug(decimalString.remove('.').isNumeric()); // this will be true
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
-- isNumString :: String -> Bool
|
||||
on isNumString(s)
|
||||
try
|
||||
if class of s is string then
|
||||
set c to class of (s as number)
|
||||
c is real or c is integer
|
||||
else
|
||||
false
|
||||
end if
|
||||
on error
|
||||
false
|
||||
end try
|
||||
end isNumString
|
||||
|
||||
|
||||
|
||||
-- TEST
|
||||
on run
|
||||
|
||||
map(isNumString, {3, 3.0, 3.5, "3.5", "3E8", "-3.5", "30", "three", three, four})
|
||||
|
||||
--> {false, false, false, true, true, true, true, false, false, false}
|
||||
|
||||
end run
|
||||
|
||||
-- three :: () -> Int
|
||||
script three
|
||||
3
|
||||
end script
|
||||
|
||||
-- four :: () -> Int
|
||||
on four()
|
||||
4
|
||||
end four
|
||||
|
||||
|
||||
-- GENERIC FUNCTIONS FOR TEST
|
||||
|
||||
-- map :: (a -> b) -> [a] -> [b]
|
||||
on map(f, xs)
|
||||
tell mReturn(f)
|
||||
set lng to length of xs
|
||||
set lst to {}
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to lambda(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end map
|
||||
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: Handler -> Script
|
||||
on mReturn(f)
|
||||
if class of f is script then
|
||||
f
|
||||
else
|
||||
script
|
||||
property lambda : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
|
@ -0,0 +1 @@
|
|||
{false, false, false, true, true, true, true, false, false, false}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
on isNumString(s)
|
||||
if (s's class is not text) then return false
|
||||
try
|
||||
s as number
|
||||
return true
|
||||
on error
|
||||
return false
|
||||
end try
|
||||
end isNumString
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
print numeric? "hello world"
|
||||
print numeric? "1234"
|
||||
print numeric? "1234 hello world"
|
||||
print numeric? "12.34"
|
||||
print numeric? "!#@$"
|
||||
print numeric? "-1.23"
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
list = 0 .14 -5.2 ten 0xf
|
||||
Loop, Parse, list, %A_Space%
|
||||
MsgBox,% IsNumeric(A_LoopField)
|
||||
Return
|
||||
|
||||
IsNumeric(x) {
|
||||
If x is number
|
||||
Return, 1
|
||||
Else Return, 0
|
||||
}
|
||||
|
||||
;Output: 1 1 1 0 1
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
10 INPUT "Enter a string";S$:GOSUB 1000
|
||||
20 IF R THEN PRINT "Is num" ELSE PRINT"Not num"
|
||||
99 END
|
||||
1000 T1=VAL(S$):T1$=STR$(T1)
|
||||
1010 R=T1$=S$ OR T1$=" "+S$
|
||||
1099 RETURN
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
#La función isNumeric() es nativa de BASIC256.
|
||||
#Devuelve 1 (verdadero) si la expresión es un entero,
|
||||
#un número de punto flotante o una cadena que se puede
|
||||
#convertir directamente en un número.
|
||||
#De lo contrario, devuelve 0 (falso).
|
||||
|
||||
#Las siguientes cadenas numéricas son válidas:
|
||||
#“123”, “-345”, “234.234324”, “-34234.123”, “-2.567e7” y “6.7888E-8”.
|
||||
|
||||
s = "1234.056789"
|
||||
print s, " => "; isNumeric(s)
|
||||
s = "-2.567e7"
|
||||
print s, " => "; isNumeric(s)
|
||||
s = "Dog"
|
||||
print s, " => "; isNumeric(s)
|
||||
s = "Bad125"
|
||||
print s, " => "; isNumeric(s)
|
||||
s = "-0177"
|
||||
print s, " => "; isNumeric(s)
|
||||
s = "0b1110" #binario
|
||||
print s, " => "; isNumeric(s)
|
||||
s = "0o177" #octal
|
||||
print s, " => "; isNumeric(s)
|
||||
s = "0xff" #hexadecimal
|
||||
print s, " => "; isNumeric(s)
|
||||
end
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
REPEAT
|
||||
READ N$
|
||||
IF FN_isanumber(N$) THEN
|
||||
PRINT "'" N$ "' is a number"
|
||||
ELSE
|
||||
PRINT "'" N$ "' is NOT a number"
|
||||
ENDIF
|
||||
UNTIL N$ = "end"
|
||||
END
|
||||
|
||||
DATA "PI", "0123", "-0123", "12.30", "-12.30", "123!", "0"
|
||||
DATA "0.0", ".123", "-.123", "12E3", "12E-3", "12+3", "end"
|
||||
|
||||
DEF FN_isanumber(A$)
|
||||
ON ERROR LOCAL = FALSE
|
||||
IF EVAL("(" + A$ + ")") <> VAL(A$) THEN = FALSE
|
||||
IF VAL(A$) <> 0 THEN = TRUE
|
||||
IF LEFT$(A$,1) = "0" THEN = TRUE
|
||||
= FALSE
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
INPUT "Your string: ", s$
|
||||
|
||||
IF REGEX(s$, "^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?$") THEN
|
||||
PRINT "This is a number"
|
||||
ELSE
|
||||
PRINT "Not a number"
|
||||
ENDIF
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
set /a a=%arg%+0 >nul
|
||||
if %a% == 0 (
|
||||
if not "%arg%"=="0" (
|
||||
echo Non Numeric.
|
||||
) else (
|
||||
echo Numeric.
|
||||
)
|
||||
) else (
|
||||
echo Numeric.
|
||||
)
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
~:0\`#v_:"+"-!#v_:"-"-!#v_::"E"-\"e"-*#v_ v
|
||||
v _v# < < 0<
|
||||
>~:0\`#v_>::"0"-0\`\"9"-0`+!#v_:"."-!#v_::"E"-\"e"-*!#v_ v
|
||||
^ $< > > $ v
|
||||
>~:0\`#v_>::"0"-0\`\"9"-0`+!#v_:"."-!#v_::"E"-\"e"-*!#v_> v>
|
||||
^ $< >$~:0\`#v_:"+"-#v_v
|
||||
v $_v# < < :#<
|
||||
>~:0\`#v_>::"0"-0\`\"9"-0`+!#v_:"."-!#v_::"E"-\"e"-*!v 0 > v
|
||||
^ $< v < << ^_^#-"-"<
|
||||
> "ciremuN">:#,_@ >>#$_"ciremun toN">:#,_@^ <
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
43257349578692:/
|
||||
F
|
||||
|
||||
260780243875083/35587980:/
|
||||
S
|
||||
|
||||
247/30:~/#
|
||||
F
|
||||
|
||||
80000000000:~/#
|
||||
S
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
@("1.000-4E-10":~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#))
|
||||
F
|
||||
|
||||
@("1.0004E-54328":~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#))
|
||||
S
|
||||
|
||||
@("-464641.0004E-54328":~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#))
|
||||
S
|
||||
|
||||
@("1/2.0004E-10":~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#))
|
||||
F
|
||||
|
||||
@("1357E-10":~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#))
|
||||
S
|
||||
|
||||
@("1357e0":~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#))
|
||||
S
|
||||
|
||||
@("13579":~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#))
|
||||
S
|
||||
|
||||
@("1.246":~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#))
|
||||
S
|
||||
|
||||
@("0.0":~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#))
|
||||
S
|
||||
|
||||
@("0.0000":~/# (|"." (|? 0|`) (|~/#:>0)) (|(E|e) ~/#))
|
||||
S
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
(float2fraction=
|
||||
integerPart decimalPart d1 dn exp sign
|
||||
. @( !arg
|
||||
: ~/#?integerPart
|
||||
( &0:?decimalPart:?d1:?dn
|
||||
| "."
|
||||
[?d1
|
||||
(|? 0|`)
|
||||
( &0:?decimalPart
|
||||
| ~/#?decimalPart:>0
|
||||
)
|
||||
[?dn
|
||||
)
|
||||
( &0:?exp
|
||||
| (E|e) ~/#?exp
|
||||
)
|
||||
)
|
||||
& ( !integerPart*(-1:?sign):>0:?integerPart
|
||||
| 1:?sign
|
||||
)
|
||||
& !sign*(!integerPart+!decimalPart*10^(!d1+-1*!dn))*10^!exp
|
||||
);
|
||||
|
||||
( out$float2fraction$"1.2"
|
||||
& out$float2fraction$"1.02"
|
||||
& out$float2fraction$"1.01"
|
||||
& out$float2fraction$"10.01"
|
||||
& out$float2fraction$"10.01e10"
|
||||
& out$float2fraction$"10.01e1"
|
||||
& out$float2fraction$"10.01e2"
|
||||
& out$float2fraction$"10.01e-2"
|
||||
& out$float2fraction$"-10.01e-2"
|
||||
& out$float2fraction$"-10e-2"
|
||||
& out$float2fraction$"0.000"
|
||||
);
|
||||
|
|
@ -0,0 +1 @@
|
|||
ps^^-]to{"Int""Double"}\/~[\/L[1==?*
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
#include <sstream> // for istringstream
|
||||
|
||||
using namespace std;
|
||||
|
||||
bool isNumeric( const char* pszInput, int nNumberBase )
|
||||
{
|
||||
istringstream iss( pszInput );
|
||||
|
||||
if ( nNumberBase == 10 )
|
||||
{
|
||||
double dTestSink;
|
||||
iss >> dTestSink;
|
||||
}
|
||||
else if ( nNumberBase == 8 || nNumberBase == 16 )
|
||||
{
|
||||
int nTestSink;
|
||||
iss >> ( ( nNumberBase == 8 ) ? oct : hex ) >> nTestSink;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
|
||||
// was any input successfully consumed/converted?
|
||||
if ( ! iss )
|
||||
return false;
|
||||
|
||||
// was all the input successfully consumed/converted?
|
||||
return ( iss.rdbuf()->in_avail() == 0 );
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
bool isNumeric( const char* pszInput, int nNumberBase )
|
||||
{
|
||||
string base = "0123456789ABCDEF";
|
||||
string input = pszInput;
|
||||
|
||||
return (input.find_first_not_of(base.substr(0, nNumberBase)) == string::npos);
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
bool isNumeric(const std::string& input) {
|
||||
return std::all_of(input.begin(), input.end(), ::isdigit);
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
public static bool IsNumeric(string s)
|
||||
{
|
||||
double Result;
|
||||
return double.TryParse(s, out Result); // TryParse routines were added in Framework version 2.0.
|
||||
}
|
||||
|
||||
string value = "123";
|
||||
if (IsNumeric(value))
|
||||
{
|
||||
// do something
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
public static bool IsNumeric(string s)
|
||||
{
|
||||
try
|
||||
{
|
||||
Double.Parse(s);
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
int isNumeric (const char * s)
|
||||
{
|
||||
if (s == NULL || *s == '\0' || isspace(*s))
|
||||
return 0;
|
||||
char * p;
|
||||
strtod (s, &p);
|
||||
return *p == '\0';
|
||||
}
|
||||
|
|
@ -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.
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. Is-Numeric.
|
||||
|
||||
DATA DIVISION.
|
||||
WORKING-STORAGE SECTION.
|
||||
01 Numeric-Chars PIC X(10) VALUE "0123456789".
|
||||
|
||||
01 Success CONSTANT 0.
|
||||
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 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,7 @@
|
|||
(defn numeric? [s]
|
||||
(if-let [s (seq s)]
|
||||
(let [s (if (= (first s) \-) (next s) s)
|
||||
s (drop-while #(Character/isDigit %) s)
|
||||
s (if (= (first s) \.) (next s) s)
|
||||
s (drop-while #(Character/isDigit %) s)]
|
||||
(empty? s))))
|
||||
|
|
@ -0,0 +1 @@
|
|||
(numeric? [\1 \2 \3]) ;; yields logical true
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
(require '[clojure.edn :as edn])
|
||||
(import [java.io PushbackReader StringReader])
|
||||
|
||||
(defn number-string? [s]
|
||||
(boolean
|
||||
(when (and (string? s) (re-matches #"^[+-]?\d.*" s))
|
||||
(let [reader (PushbackReader. (StringReader. s))
|
||||
num (try (edn/read reader) (catch Exception _ nil))]
|
||||
(when num
|
||||
; Check that the string has nothing after the number
|
||||
(= -1 (.read reader)))))))
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
user=> (number-string? "2r101010")
|
||||
true
|
||||
user=> (number-string? "22/7")
|
||||
true
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
console.log (isFinite(s) for s in [5, "5", "-5", "5", "5e5", 0]) # all true
|
||||
console.log (isFinite(s) for s in [NaN, "fred", "###"]) # all false
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
<cfset TestValue=34>
|
||||
TestValue: <cfoutput>#TestValue#</cfoutput><br>
|
||||
<cfif isNumeric(TestValue)>
|
||||
is Numeric.
|
||||
<cfelse>
|
||||
is NOT Numeric.
|
||||
</cfif>
|
||||
|
||||
<cfset TestValue="NAS">
|
||||
TestValue: <cfoutput>#TestValue#</cfoutput><br>
|
||||
<cfif isNumeric(TestValue)>
|
||||
is Numeric.
|
||||
<cfelse>
|
||||
is NOT Numeric.
|
||||
</cfif>
|
||||
|
|
@ -0,0 +1 @@
|
|||
<cfoutput>#isNumeric(42)#</cfoutput>
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
5 print chr$(147);chr$(14)
|
||||
10 input "Enter a string";s$:gosub 1000:print
|
||||
20 if r then print "You entered a number.":goto 99
|
||||
30 print "That is not a number."
|
||||
99 end
|
||||
1000 t1=val(s$):t1$=str$(t1)
|
||||
1010 r=t1$=s$ or t1$=" "+s$
|
||||
1099 return
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
(defun numeric-string-p (string)
|
||||
(let ((*read-eval* nil))
|
||||
(ignore-errors (numberp (read-from-string string)))))
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(defun numeric-string-p (string)
|
||||
(ignore-errors (parse-number:parse-number string))) ; parse failed, return false (nil)
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
import std.stdio, std.string, std.array;
|
||||
|
||||
void main() {
|
||||
foreach (const 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, s.strip().isNumeric(true));
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
import std.stdio, std.string, std.conv, std.array, std.exception;
|
||||
|
||||
bool isNumeric(in string s) pure {
|
||||
immutable s2 = s.strip.toLower.replace("_", "").replace(",", "");
|
||||
try {
|
||||
s2.to!real;
|
||||
} catch (ConvException e) {
|
||||
if (s2.startsWith("0x"))
|
||||
return !s2[2 .. $].to!ulong(16)
|
||||
.collectException!ConvException;
|
||||
else if (s2.startsWith("0b"))
|
||||
return !s2[2 .. $].to!ulong(2)
|
||||
.collectException!ConvException;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void main() {
|
||||
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, s.isNumeric);
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
function IsNumericString(const inStr: string): Boolean;
|
||||
var
|
||||
i: extended;
|
||||
begin
|
||||
Result := TryStrToFloat(inStr,i);
|
||||
end;
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
program isNumeric;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses
|
||||
Classes,
|
||||
SysUtils;
|
||||
|
||||
function IsNumericString(const inStr: string): Boolean;
|
||||
var
|
||||
i: extended;
|
||||
begin
|
||||
Result := TryStrToFloat(inStr,i);
|
||||
end;
|
||||
|
||||
|
||||
{ Test function }
|
||||
var
|
||||
s: string;
|
||||
c: Integer;
|
||||
|
||||
const
|
||||
MAX_TRIES = 10;
|
||||
sPROMPT = 'Enter a string (or type "quit" to exit):';
|
||||
sIS = ' is numeric';
|
||||
sISNOT = ' is NOT numeric';
|
||||
|
||||
begin
|
||||
c := 0;
|
||||
s := '';
|
||||
repeat
|
||||
Inc(c);
|
||||
Writeln(sPROMPT);
|
||||
Readln(s);
|
||||
if (s <> '') then
|
||||
begin
|
||||
tmp.Add(s);
|
||||
if IsNumericString(s) then
|
||||
begin
|
||||
Writeln(s+sIS);
|
||||
end
|
||||
else
|
||||
begin
|
||||
Writeln(s+sISNOT);
|
||||
end;
|
||||
Writeln('');
|
||||
end;
|
||||
until
|
||||
(c >= MAX_TRIES) or (LowerCase(s) = 'quit');
|
||||
|
||||
end.
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
func String.IsNumeric() {
|
||||
try {
|
||||
parse(this) is Integer or Float
|
||||
} catch _ {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
var str = "1234567"
|
||||
print(str.IsNumeric())
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
def isNumeric(specimen :String) {
|
||||
try {
|
||||
<import:java.lang.makeDouble>.valueOf(specimen)
|
||||
return true
|
||||
} catch _ {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
fun isNumeric = logic by text value
|
||||
try
|
||||
^|So funny:
|
||||
|a) we check if it's castable to a real
|
||||
|b) we obtain the real 0.0
|
||||
|c) conversion from real to int to get 0
|
||||
|d) int can be converted to logical to get ⊥
|
||||
|e) we can negate the result
|
||||
|^
|
||||
return not when(value.contains("."), logic!int!(real!value * 0), logic!(int!value * 0))
|
||||
remedy
|
||||
return false
|
||||
end
|
||||
end
|
||||
fun main = int by List args
|
||||
if args.length == 1
|
||||
writeLine(isNumeric(args[0]))
|
||||
else
|
||||
writeLine("value".padEnd(8, " ") + " " + "numeric")
|
||||
for each text value in text["0o755", "thursday", "3.14", "0b1010", "-100", "0xff"]
|
||||
writeLine(value.padEnd(8, " ") + " " + isNumeric(value))
|
||||
end
|
||||
end
|
||||
return 0
|
||||
end
|
||||
exit main(Runtime.args)
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
PROGRAM NUMERIC
|
||||
|
||||
PROCEDURE IS_NUMERIC(S$->ANS%)
|
||||
LOCAL T1,T1$
|
||||
T1=VAL(S$)
|
||||
T1$=STR$(T1)
|
||||
ANS%=(T1$=S$) OR T1$=" "+S$
|
||||
END PROCEDURE
|
||||
|
||||
BEGIN
|
||||
PRINT(CHR$(12);)
|
||||
INPUT("Enter a string",S$)
|
||||
IS_NUMERIC(S$->ANS%)
|
||||
IF ANS% THEN PRINT("is num") ELSE PRINT("not num")
|
||||
END PROGRAM
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
proc is_numeric a$ . r .
|
||||
h = number a$
|
||||
r = 1 - error
|
||||
# because every variable must be used
|
||||
h = h
|
||||
.
|
||||
for s$ in [ "abc" "21a" "1234" "-13" "7.65" ]
|
||||
call is_numeric s$ r
|
||||
if r = 1
|
||||
print s$ & " is numeric"
|
||||
else
|
||||
print s$ & " is not numeric"
|
||||
.
|
||||
.
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
(string->number "albert")
|
||||
→ #f
|
||||
(string->number -666)
|
||||
→ -666
|
||||
(if (string->number 666) 'YES 'NO)
|
||||
→ YES
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
defmodule RC do
|
||||
def is_numeric(str) do
|
||||
case Float.parse(str) do
|
||||
{_num, ""} -> true
|
||||
_ -> false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
["123", "-12.3", "123.", ".05", "-12e5", "+123", " 123", "abc", "123a", "12.3e", "1 2"] |> Enum.filter(&RC.is_numeric/1)
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
is_numeric(L) ->
|
||||
Float = (catch erlang:list_to_float(L)),
|
||||
Int = (catch erlang:list_to_integer(L)),
|
||||
is_number(Float) orelse is_number(Int).
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
include get.e
|
||||
|
||||
function is_numeric(sequence s)
|
||||
sequence val
|
||||
val = value(s)
|
||||
return val[1]=GET_SUCCESS and atom(val[2])
|
||||
end function
|
||||
|
|
@ -0,0 +1 @@
|
|||
let is_numeric a = fst (System.Double.TryParse a)
|
||||
|
|
@ -0,0 +1 @@
|
|||
: numeric? ( string -- ? ) string>number >boolean ;
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
class Main
|
||||
{
|
||||
// function to see if str contains a number of any of built-in types
|
||||
static Bool readNum (Str str)
|
||||
{
|
||||
int := Int.fromStr (str, 10, false) // use base 10
|
||||
if (int != null) return true
|
||||
float := Float.fromStr (str, false)
|
||||
if (float != null) return true
|
||||
decimal := Decimal.fromStr (str, false)
|
||||
if (decimal != null) return true
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
public static Void main ()
|
||||
{
|
||||
echo ("For '2': " + readNum ("2"))
|
||||
echo ("For '-2': " + readNum ("-2"))
|
||||
echo ("For '2.5': " + readNum ("2.5"))
|
||||
echo ("For '2a5': " + readNum ("2a5"))
|
||||
echo ("For '-2.1e5': " + readNum ("-2.1e5"))
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
: is-numeric ( addr len -- )
|
||||
2dup snumber? ?dup if \ not standard, but >number is more cumbersome to use
|
||||
0< if
|
||||
-rot type ." as integer = " .
|
||||
else
|
||||
2swap type ." as double = " <# #s #> type
|
||||
then
|
||||
else 2dup >float if
|
||||
type ." as float = " f.
|
||||
else
|
||||
type ." isn't numeric in base " base @ dec.
|
||||
then then ;
|
||||
|
||||
s" 1234" is-numeric \ 1234 as integer = 1234
|
||||
s" 1234." is-numeric \ 1234. as double = 1234
|
||||
s" 1234e" is-numeric \ 1234e as float = 1234.
|
||||
s" $1234" is-numeric \ $1234 as integer = 4660 ( hex literal )
|
||||
s" %1010" is-numeric \ %1010 as integer = 10 ( binary literal )
|
||||
s" beef" is-numeric \ beef isn't numeric in base 10
|
||||
hex
|
||||
s" beef" is-numeric \ beef as integer = BEEF
|
||||
s" &1234" is-numeric \ &1234 as integer = 4D2 ( decimal literal )
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
FUNCTION is_numeric(string)
|
||||
IMPLICIT NONE
|
||||
CHARACTER(len=*), INTENT(IN) :: string
|
||||
LOGICAL :: is_numeric
|
||||
REAL :: x
|
||||
INTEGER :: e
|
||||
READ(string,*,IOSTAT=e) x
|
||||
is_numeric = e == 0
|
||||
END FUNCTION is_numeric
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
function isNumeric(const potentialNumeric: string): boolean;
|
||||
var
|
||||
potentialInteger: integer;
|
||||
potentialReal: real;
|
||||
integerError: integer;
|
||||
realError: integer;
|
||||
begin
|
||||
integerError := 0;
|
||||
realError := 0;
|
||||
|
||||
// system.val attempts to convert numerical value representations.
|
||||
// It accepts all notations as they are accepted by the language,
|
||||
// as well as the '0x' (or '0X') prefix for hexadecimal values.
|
||||
val(potentialNumeric, potentialInteger, integerError);
|
||||
val(potentialNumeric, potentialReal, realError);
|
||||
|
||||
isNumeric := (integerError = 0) or (realError = 0);
|
||||
end;
|
||||
|
|
@ -0,0 +1,95 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Dim Shared symbols(0 To 15) As UByte
|
||||
For i As Integer = 48 to 57
|
||||
symbols(i - 48) = i
|
||||
Next
|
||||
For i As Integer = 97 to 102
|
||||
symbols(i - 87) = i
|
||||
Next
|
||||
|
||||
Const plus As UByte = 43
|
||||
Const minus As Ubyte = 45
|
||||
Const dot As UByte = 46
|
||||
|
||||
Function isNumeric(s As Const String, base_ As Integer = 10) As Boolean
|
||||
If s = "" OrElse s = "." OrElse s = "+" OrElse s = "-" Then Return False
|
||||
Err = 0
|
||||
|
||||
If base_ < 2 OrElse base_ > 16 Then
|
||||
Err = 1000
|
||||
Return False
|
||||
End If
|
||||
|
||||
Dim t As String = LCase(s)
|
||||
|
||||
If (t[0] = plus) OrElse (t[0] = minus) Then
|
||||
t = Mid(t, 2)
|
||||
End If
|
||||
|
||||
If Left(t, 2) = "&h" Then
|
||||
If base_ <> 16 Then Return False
|
||||
t = Mid(t, 3)
|
||||
End if
|
||||
|
||||
If Left(t, 2) = "&o" Then
|
||||
If base_ <> 8 Then Return False
|
||||
t = Mid(t, 3)
|
||||
End if
|
||||
|
||||
If Left(t, 2) = "&b" Then
|
||||
If base_ <> 2 Then Return False
|
||||
t = Mid(t, 3)
|
||||
End if
|
||||
|
||||
If Len(t) = 0 Then Return False
|
||||
Dim As Boolean isValid, hasDot = false
|
||||
|
||||
For i As Integer = 0 To Len(t) - 1
|
||||
isValid = False
|
||||
|
||||
For j As Integer = 0 To base_ - 1
|
||||
If t[i] = symbols(j) Then
|
||||
isValid = True
|
||||
Exit For
|
||||
End If
|
||||
If t[i] = dot Then
|
||||
If CInt(Not hasDot) AndAlso (base_ = 10) Then
|
||||
hasDot = True
|
||||
IsValid = True
|
||||
Exit For
|
||||
End If
|
||||
Return False ' either more than one dot or not base 10
|
||||
End If
|
||||
Next j
|
||||
|
||||
If Not isValid Then Return False
|
||||
Next i
|
||||
|
||||
Return True
|
||||
End Function
|
||||
|
||||
Dim s As String
|
||||
s = "1234.056789"
|
||||
Print s, " (base 10) => "; isNumeric(s)
|
||||
s = "1234.56"
|
||||
Print s, " (base 7) => "; isNumeric(s, 7)
|
||||
s = "021101"
|
||||
Print s, " (base 2) => "; isNumeric(s, 2)
|
||||
s = "Dog"
|
||||
Print s, " (base 16) => "; isNumeric(s, 16)
|
||||
s = "Bad125"
|
||||
Print s, " (base 16) => "; isNumeric(s, 16)
|
||||
s = "-0177"
|
||||
Print s, " (base 8) => "; isNumeric(s, 8)
|
||||
s = "+123abcd.ef"
|
||||
Print s, " (base 16) => "; isNumeric(s, 8)
|
||||
s = "54321"
|
||||
Print s, " (base 6) => "; isNumeric(s, 6)
|
||||
s = "123xyz"
|
||||
Print s, " (base 10) => "; isNumeric(s)
|
||||
s = "xyz"
|
||||
Print s, " (base 10) => "; isNumeric(s)
|
||||
Print
|
||||
Print "Press any key to quit"
|
||||
Sleep
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
include "NSLog.incl"
|
||||
|
||||
local fn StringIsNumeric( string as CFStringRef ) as BOOL
|
||||
BOOL flag = NO
|
||||
ScannerRef scanner = fn ScannerWithString( string )
|
||||
if ( fn ScannerScanFloat( scanner, NULL ) )
|
||||
flag = fn ScannerIsAtEnd( scanner )
|
||||
end if
|
||||
end fn = flag
|
||||
|
||||
NSLog(@"%d",fn StringIsNumeric( @"1.23" ))
|
||||
NSLog(@"%d",fn StringIsNumeric( @"-123.4e5" ))
|
||||
NSLog(@"%d",fn StringIsNumeric( @"alpha" ))
|
||||
|
||||
HandleEvents
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
Public Sub Form_Open()
|
||||
Dim sAnswer, sString As String
|
||||
|
||||
sString = Trim(InputBox("Enter as string", "String or Numeric"))
|
||||
|
||||
If IsNumber(sString) Then sAnswer = "'" & sString & "' is numeric" Else sAnswer = "'" & sString & "' is a string"
|
||||
Print sAnswer
|
||||
|
||||
End
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func isNumeric(s string) bool {
|
||||
_, err := strconv.ParseFloat(s, 64)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("Are these strings numeric?")
|
||||
strings := []string{"1", "3.14", "-100", "1e2", "NaN", "rose"}
|
||||
for _, s := range strings {
|
||||
fmt.Printf(" %4s -> %t\n", s, isNumeric(s))
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
func isInt(s string) bool {
|
||||
for _, c := range s {
|
||||
if !unicode.IsDigit(c) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("Are these strings integers?")
|
||||
v := "1"
|
||||
b := false
|
||||
if _, err := strconv.Atoi(v); err == nil {
|
||||
b = true
|
||||
}
|
||||
fmt.Printf(" %3s -> %t\n", v, b)
|
||||
i := "one"
|
||||
fmt.Printf(" %3s -> %t\n", i, isInt(i))
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
def isNumeric = {
|
||||
def formatter = java.text.NumberFormat.instance
|
||||
def pos = [0] as java.text.ParsePosition
|
||||
formatter.parse(it, pos)
|
||||
|
||||
// if parse position index has moved to end of string
|
||||
// them the whole string was numeric
|
||||
pos.index == it.size()
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
println isNumeric('1')
|
||||
println isNumeric('-.555')
|
||||
println isNumeric('1,000,000')
|
||||
println isNumeric(' 1 1 1 1 ')
|
||||
println isNumeric('abcdef')
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
isInteger s = case reads s :: [(Integer, String)] of
|
||||
[(_, "")] -> True
|
||||
_ -> False
|
||||
|
||||
isDouble s = case reads s :: [(Double, String)] of
|
||||
[(_, "")] -> True
|
||||
_ -> False
|
||||
|
||||
isNumeric :: String -> Bool
|
||||
isNumeric s = isInteger s || isDouble s
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
areDigits = all isDigit
|
||||
isDigit selects ASCII digits i.e. '0'..'9'
|
||||
isOctDigit selects '0'..'7'
|
||||
isHexDigit selects '0'..'9','A'..'F','a'..'f'
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
static function isNumeric(n:String):Bool
|
||||
{
|
||||
if (Std.parseInt(n) != null) //Std.parseInt converts a string to an int
|
||||
{
|
||||
return true; //as long as it results in an integer, the function will return true
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
! = bin + 2*int + 4*flt + 8*oct +16*hex + 32*sci
|
||||
isNumeric("1001") ! 27 = 1 1 0 1 1 0
|
||||
isNumeric("123") ! 26 = 0 1 0 1 1 0
|
||||
isNumeric("1E78") ! 48 = 0 0 0 0 1 1
|
||||
isNumeric("-0.123") ! 4 = 0 0 1 0 0 1
|
||||
isNumeric("-123.456e-78") ! 32 = 0 0 0 0 0 1
|
||||
isNumeric(" 123") ! 0: leading blank
|
||||
isNumeric("-123.456f-78") ! 0: illegal character f
|
||||
|
||||
|
||||
FUNCTION isNumeric(string) ! true ( > 0 ), no leading/trailing blanks
|
||||
CHARACTER string
|
||||
b = INDEX(string, "[01]+", 128, Lbin) ! Lbin returns length found
|
||||
i = INDEX(string, "-?\d+", 128, Lint) ! regular expression: 128
|
||||
f = INDEX(string, "-?\d+\.\d*", 128, Lflt)
|
||||
o = INDEX(string, "[0-7]+", 128, Loct)
|
||||
h = INDEX(string, "[0-9A-F]+", 128, Lhex) ! case sensitive: 1+128
|
||||
s = INDEX(string, "-?\d+\.*\d*E[+-]*\d*", 128, Lsci)
|
||||
IF(anywhere) THEN ! 0 (false) by default
|
||||
isNumeric = ( b > 0 ) + 2*( i > 0 ) + 4*( f > 0 ) + 8*( o > 0 ) + 16*( h > 0 ) + 32*( s > 0 )
|
||||
ELSEIF(boolean) THEN ! 0 (false) by default
|
||||
isNumeric = ( b + i + f + o + h + s ) > 0 ! this would return 0 or 1
|
||||
ELSE
|
||||
L = LEN(string)
|
||||
isNumeric = (Lbin==L) + 2*(Lint==L) + 4*(Lflt==L) + 8*(Loct==L) + 16*(Lhex==L) + 32*(Lsci==L)
|
||||
ENDIF
|
||||
END
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
concept numeric(n) {
|
||||
number(n)
|
||||
errors {
|
||||
print(n, " is not numeric!")
|
||||
return
|
||||
}
|
||||
print(n, " is numeric :)")
|
||||
}
|
||||
|
||||
software {
|
||||
numeric("1200")
|
||||
numeric("3.14")
|
||||
numeric("3/4")
|
||||
numeric("abcdefg")
|
||||
numeric("1234test")
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
function isnumeric,input
|
||||
on_ioerror, false
|
||||
test = double(input)
|
||||
return, 1
|
||||
false: return, 0
|
||||
end
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
if isnumeric('-123.45e-2') then print, 'yes' else print, 'no'
|
||||
; ==> yes
|
||||
if isnumeric('picklejuice') then print, 'yes' else print, 'no'
|
||||
; ==> no
|
||||
|
|
@ -0,0 +1 @@
|
|||
write(image(x), if numeric(x) then " is numeric." else " is not numeric")
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
isNumeric=: _ ~: _ ". ]
|
||||
isNumericScalar=: 1 -: isNumeric
|
||||
TXT=: ,&' a scalar numeric value.' &.> ' is not';' represents'
|
||||
sayIsNumericScalar=: , TXT {::~ isNumericScalar
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
isNumeric '152'
|
||||
1
|
||||
isNumeric '152 -3.1415926 Foo123'
|
||||
1 1 0
|
||||
isNumeric '42 foo42 4.2e1 4200e-2 126r3 16b2a 42foo'
|
||||
1 0 1 1 1 1 0
|
||||
isNumericScalar '152 -3.1415926 Foo123'
|
||||
0
|
||||
sayIsNumericScalar '-3.1415926'
|
||||
-3.1415926 represents a scalar numeric value.
|
||||
|
|
@ -0,0 +1 @@
|
|||
Integer.parseInt("12345")
|
||||
|
|
@ -0,0 +1 @@
|
|||
Float.parseFloat("123.456")
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
public static void main(String[] args) {
|
||||
String value;
|
||||
value = "1234567";
|
||||
System.out.printf("%-10s %b%n", value, isInteger(value));
|
||||
value = "12345abc";
|
||||
System.out.printf("%-10s %b%n", value, isInteger(value));
|
||||
value = "-123.456";
|
||||
System.out.printf("%-10s %b%n", value, isFloatingPoint(value));
|
||||
value = "-.456";
|
||||
System.out.printf("%-10s %b%n", value, isFloatingPoint(value));
|
||||
value = "123.";
|
||||
System.out.printf("%-10s %b%n", value, isFloatingPoint(value));
|
||||
value = "123.abc";
|
||||
System.out.printf("%-10s %b%n", value, isFloatingPoint(value));
|
||||
}
|
||||
|
||||
static boolean isInteger(String string) {
|
||||
String digits = "0123456789";
|
||||
for (char character : string.toCharArray()) {
|
||||
if (!digits.contains(String.valueOf(character)))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static boolean isFloatingPoint(String string) {
|
||||
/* at least one decimal-point */
|
||||
int indexOf = string.indexOf('.');
|
||||
if (indexOf == -1)
|
||||
return false;
|
||||
/* assure only 1 decimal-point */
|
||||
if (indexOf != string.lastIndexOf('.'))
|
||||
return false;
|
||||
if (string.charAt(0) == '-' || string.charAt(0) == '+') {
|
||||
string = string.substring(1);
|
||||
indexOf--;
|
||||
}
|
||||
String integer = string.substring(0, indexOf);
|
||||
if (!integer.isEmpty()) {
|
||||
if (!isInteger(integer))
|
||||
return false;
|
||||
}
|
||||
String decimal = string.substring(indexOf + 1);
|
||||
if (!decimal.isEmpty())
|
||||
return isInteger(decimal);
|
||||
return true;
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
public boolean isNumeric(String input) {
|
||||
try {
|
||||
Integer.parseInt(input);
|
||||
return true;
|
||||
}
|
||||
catch (NumberFormatException e) {
|
||||
// s is not numeric
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
private static final boolean isNumeric(final String s) {
|
||||
if (s == null || s.isEmpty()) return false;
|
||||
for (int x = 0; x < s.length(); x++) {
|
||||
final char c = s.charAt(x);
|
||||
if (x == 0 && (c == '-')) continue; // negative
|
||||
if ((c >= '0') && (c <= '9')) continue; // 0 - 9
|
||||
return false; // invalid
|
||||
}
|
||||
return true; // valid
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
public static boolean isNumeric(String inputData) {
|
||||
return inputData.matches("[-+]?\\d+(\\.\\d+)?");
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
public static boolean isNumeric(String inputData) {
|
||||
NumberFormat formatter = NumberFormat.getInstance();
|
||||
ParsePosition pos = new ParsePosition(0);
|
||||
formatter.parse(inputData, pos);
|
||||
return inputData.length() == pos.getIndex();
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
public static boolean isNumeric(String inputData) {
|
||||
Scanner sc = new Scanner(inputData);
|
||||
return sc.hasNextInt();
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
function isNumeric(n) {
|
||||
return !isNaN(parseFloat(n)) && isFinite(n);
|
||||
}
|
||||
var value = "123.45e7"; // Assign string literal to value
|
||||
if (isNumeric(value)) {
|
||||
// value is a number
|
||||
}
|
||||
//Or, in web browser in address field:
|
||||
// javascript:function isNumeric(n) {return !isNaN(parseFloat(n)) && isFinite(n);}; value="123.45e4"; if(isNumeric(value)) {alert('numeric')} else {alert('non-numeric')}
|
||||
|
|
@ -0,0 +1 @@
|
|||
try tonumber catch false
|
||||
|
|
@ -0,0 +1 @@
|
|||
def is_numeric: true and try tonumber catch false;
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue