• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Check if string is real number

Status
Not open for further replies.
Level 3
Joined
Jan 29, 2021
Messages
34
Hi guys.
The task is next:
I have a string, it could be anything in theory.
I need function

function IsReal takes string s returns boolean
endfunction

that checks, whether or not string can be converted to real number
I dont need full tests like including -(negative values) or exponential form like 3.15e7

Just simple checks like

0 zero is valid
123 any integer number is valid as well
0.12345 if it starts with "0" and is not "0" the next char right after "0" has to be "." and then only digits
12353.5645 any other combinations with digits separeted with exactly one "."

If string contains any other characters exceps digits or "." function has to return false
 
Level 3
Joined
Jan 29, 2021
Messages
34
No, you didnt get the point.
I dont want to check that 10.000000 == 10

I want to convert user input to real number, expecting user to type it in right format, but he can type whatever he wants

For example
I expect user to type
string userInput = "123.456"

to be able to do
real x = S2R(userInput)

but what if he types
string userInput = "hello bitch, you expected a number to be here!? AHAHHA FUCK YOU"

and then again I try
real x = S2R(userInput)

Then what do I get?
The thing is this fucking jass doesnt support error or exceptions check, thats it
 
Level 3
Joined
Jan 29, 2021
Messages
34
found the existing solution and changed it to jass syntax
Works even wider


Code:
function IsDigit takes string s returns boolean
return (s == "0") or (s == "1") or (s == "2") or (s == "3") or (s == "4") or (s == "5") or (s == "6") or (s == "7") or (s == "8") or (s == "9")
endfunction

function IsRealNumber takes string s returns boolean

// To check if a '.' or 'e' is found in given
// string. We use this flag to make sure that
// either of them appear only once.
local integer i = 1
local boolean flagDotOrE = false

if (StringLength(s) == 0) then
    return false
endif

// if string is of length 1 and the only
// character is not a digit
if (StringLength(s) == 1 and not(IsDigit(SubString(s, 0, 1)))) then
    return false
endif

// If the 1st char is not '+', '-', '.' or digit
if ((SubString(s, 0, 1) != "+") and (SubString(s, 0, 1) != "-") and (SubString(s, 0, 1) != ".") and (not(IsDigit(SubString(s, 0, 1))))) then
    return false
endif

loop
exitwhen (i >= StringLength(s))
    // If any of the char does not belong to
    // {digit, +, -, ., e}
    if ((not(IsDigit(SubString(s, i, i + 1)))) and (SubString(s, i, i + 1) != "+") and (SubString(s, i, i + 1) != "-") and (SubString(s, i, i + 1) != "e") and (SubString(s, i, i + 1) != ".")) then
        return false
    endif

    if (SubString(s, i, i + 1) == ".") then
        // checks if the char 'e' has already occurred before '.'
        // If yes, returns false.
        if (flagDotOrE == true) then
            return false
        endif

        // If '.' is the last character.
        if (i + 1 >= StringLength(s)) then
            return false
        endif

        // if '.' is not followed by a digit.
        if (not(IsDigit(SubString(s, i + 1, i + 2)))) then
            return false
        endif
    elseif (SubString(s, i, i + 1) == "e") then
        // set flagDotOrE  when e is encountered.
        set flagDotOrE = true

        // If 'e' is the last Character
        if (i + 1 >= StringLength(s)) then
            return false
        endif

        // if there is no digit before 'e'.
        if (not(IsDigit(SubString(s, i - 1, i)))) then
            return false
        endif

        // if e is not followed either by         
        // '+', '-' or a digit
        if ((not(IsDigit(SubString(s, i + 1, i + 2)))) and (SubString(s, i, i + 1) != "+") and (SubString(s, i, i + 1) != "-")) then
            return false
        endif
    endif

    set i = i + 1
endloop

// If the string skips all above cases, then it is numeric
return true
endfunction
 
Level 3
Joined
Jan 29, 2021
Messages
34
mmm, that thing

string message = "random keyboard pushing like gdfgreger"
real x = S2R(messge)

and we get x == 0

mmm, what a great language

Do u think its good getting S2R("nngeibmnnreioenibier[nbqirejnfqwqp[wkf") == 0?
mmm

shouldnt it be -1 like return value from function that returns integer and in case of error returns -1?

Anyway, why not
 
Status
Not open for further replies.
Top