Perl validating numbers

bathroom-blowjob

' is NOT a number '0' is a number '0.0' is a number '.123' is a number '-.123' is a number '12E3' is a number '12E-3' is a number '12+3' is NOT a number 'end' is NOT a number0:? = null) return true return false } public static Void main () { echo ("For '2': " + read Num ("2")) echo ("For '-2': " + read Num ("-2")) echo ("For '2.5': " + read Num ("2.5")) echo ("For '2a5': " + read Num ("2a5")) echo ("For '-2.1e5': " + read Num ("-2.1e5")) }} float if type ." as float = " f. then then ; s" 1234" is-numeric \ 1234 as integer = 1234s" 1234." is-numeric \ 1234.

bathroom-blowjob

Returns true if it does, false otherwise % logical procedure is Numeric( 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 next Char ; char Pos := char Pos + 1; logical procedure have( string(1) value ch ) ; ( char Pos 0 and text(max Char//1) = " " do max Char := max Char - 1; % skip leading spacesx % while have( " " ) do next Char; % skip optional sign % if have( "+" ) or have( "-" ) then next Char; if have Digit then begin % have a digit sequence % had Digits := true; while have Digit do next Char end if_have_sign ; if have( "." ) then begin % real or imaginary number % next Char; is Real := true; had Digits := had Digits or have Digit; while have Digit do next Char end if_have_point ; % should have had some digits % ok := had Digits; if ok and have( "'" ) then begin % the number has an exponent % is Real := true; next Char; % skip optional sign % if have( "+" ) or have( "-" ) then next Char; % must have a digit sequence % ok := have Digit; while have Digit do next Char; end if_ok_and_have_exponent ; % if it is a real number, there could be L/I suffixes % if ok and is Real 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; next Char end while_have_L_or_I ; % there can be at most one L and at most 1 I % ok := ( LCount = max Char end if_ok ; ok end is Numeric ; % test the is Numeric procedure % procedure test Is Numeric( string(32) value n ; logical value expected Result ) ; begin logical actual Result; actual Result := is Numeric( n ); write( s_w := 0 , """", n, """ is " , if actual Result then "" else "not " , "numeric " , if actual Result = expected Result then "" else " NOT " , "as expected" ) end test Is Numeric ; test Is Numeric( "", false ); test Is Numeric( "b", false ); test Is Numeric( ".", false ); test Is Numeric( ".'3", false ); test Is Numeric( "3.'", false ); test Is Numeric( "0.0z44", false ); test Is Numeric( "-1IL", false ); test Is Numeric( "4.5'23ILL", false ); write( "---------" ); test Is Numeric( "-1", true ); test Is Numeric( " +.345", true ); test Is Numeric( "4.5'23I", true ); test Is Numeric( "-5'+3i", true ); test Is Numeric( "-5'-3l", true ); test Is Numeric( " -.345LI", true ); end. 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"); is Numeric("12"): true is Numeric(" 12 "): true is Numeric("hello12"): false is Numeric("-12"): true is Numeric("02"): true is Numeric("0-12"): false is Numeric("+12"): true is Numeric("1.5"): true is Numeric("1,000"): true is Numeric("1_000"): true is Numeric("0x10"): false is Numeric("0b10101111_11110000_11110000_00110011"): false is Numeric("-0b10101"): false is Numeric("0x10.5"): falseis Numeric("12"): true is Numeric(" 12 "): true is Numeric("hello12"): false is Numeric("-12"): true is Numeric("02"): true is Numeric("0-12"): false is Numeric("+12"): true is Numeric("1.5"): true is Numeric("1,000"): true is Numeric("1_000"): true is Numeric("0x10"): true is Numeric("0b10101111_11110000_11110000_00110011"): true is Numeric("-0b10101"): false is Numeric("0x10.5"): false, which will return true if the argument passed is either an integer or a float.

" " is not numeric as expected "b " is not numeric as expected ". Erlang also has two functions to transform a string to either a floating number or an integer, which will be used in conjunction with The 'from Str' methods return a parsed number or given an error.

||

Returns true if it does, false otherwise % logical procedure is Numeric( 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 next Char ; char Pos := char Pos + 1; logical procedure have( string(1) value ch ) ; ( char Pos 0 and text(max Char//1) = " " do max Char := max Char - 1; % skip leading spacesx % while have( " " ) do next Char; % skip optional sign % if have( "+" ) or have( "-" ) then next Char; if have Digit then begin % have a digit sequence % had Digits := true; while have Digit do next Char end if_have_sign ; if have( "." ) then begin % real or imaginary number % next Char; is Real := true; had Digits := had Digits or have Digit; while have Digit do next Char end if_have_point ; % should have had some digits % ok := had Digits; if ok and have( "'" ) then begin % the number has an exponent % is Real := true; next Char; % skip optional sign % if have( "+" ) or have( "-" ) then next Char; % must have a digit sequence % ok := have Digit; while have Digit do next Char; end if_ok_and_have_exponent ; % if it is a real number, there could be L/I suffixes % if ok and is Real 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; next Char end while_have_L_or_I ; % there can be at most one L and at most 1 I % ok := ( LCount = max Char end if_ok ; ok end is Numeric ; % test the is Numeric procedure % procedure test Is Numeric( string(32) value n ; logical value expected Result ) ; begin logical actual Result; actual Result := is Numeric( n ); write( s_w := 0 , """", n, """ is " , if actual Result then "" else "not " , "numeric " , if actual Result = expected Result then "" else " NOT " , "as expected" ) end test Is Numeric ; test Is Numeric( "", false ); test Is Numeric( "b", false ); test Is Numeric( ".", false ); test Is Numeric( ".'3", false ); test Is Numeric( "3.'", false ); test Is Numeric( "0.0z44", false ); test Is Numeric( "-1IL", false ); test Is Numeric( "4.5'23ILL", false ); write( "---------" ); test Is Numeric( "-1", true ); test Is Numeric( " +.345", true ); test Is Numeric( "4.5'23I", true ); test Is Numeric( "-5'+3i", true ); test Is Numeric( "-5'-3l", true ); test Is Numeric( " -.345LI", true ); end. 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"); is Numeric("12"): true is Numeric(" 12 "): true is Numeric("hello12"): false is Numeric("-12"): true is Numeric("02"): true is Numeric("0-12"): false is Numeric("+12"): true is Numeric("1.5"): true is Numeric("1,000"): true is Numeric("1_000"): true is Numeric("0x10"): false is Numeric("0b10101111_11110000_11110000_00110011"): false is Numeric("-0b10101"): false is Numeric("0x10.5"): falseis Numeric("12"): true is Numeric(" 12 "): true is Numeric("hello12"): false is Numeric("-12"): true is Numeric("02"): true is Numeric("0-12"): false is Numeric("+12"): true is Numeric("1.5"): true is Numeric("1,000"): true is Numeric("1_000"): true is Numeric("0x10"): true is Numeric("0b10101111_11110000_11110000_00110011"): true is Numeric("-0b10101"): false is Numeric("0x10.5"): false, which will return true if the argument passed is either an integer or a float." " is not numeric as expected "b " is not numeric as expected ". Erlang also has two functions to transform a string to either a floating number or an integer, which will be used in conjunction with The 'from Str' methods return a parsed number or given an error.

]]
terrem43.ru

21 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>