
-----------------------------------
chrispminis
Tue Mar 07, 2006 11:06 pm

strreal emulation
-----------------------------------
We all know Turing comes with a few helpful conversion commands.

strint
intstr
strreal etc.

But for one of my assignments, these are forbidden. Now one of the sub assignments for this main one is to emulate the strreal function. I have already succeeded in creating a multidigit integer converter but im stumped on decimal numbers. Here's what I have so far:

var str : string
var number : real := 0
var mark : int
var dec : boolean := false


get str
for i : 1 .. length (str) %Check if theres a decimal.
    if str (i) = "." then
        mark := i
        dec := true
    end if
end for

for i : 1 .. length (str)
    if dec = true then
        if str (i) not= "." then
            if i < length (str) - mark then
                number += (ord (str (* +1 - i)) - 48) * 10 ** (i - 1) %If the number is after the decimal do normal.
            elsif i >= length (str) - mark then
                number += (ord (str (* +1 - i)) - 48) * 10 ** (i - 2) %If the number is before it then take off a power of 10.
            end if
        end if
    else
        number += (ord (str (* +1 - i)) - 48) * 10 ** (i - 1) %Conversion for an integer.
    end if
end for

if dec = true then % If there was a decimal then divide the number by appropriate power of 10.
    number := number / (10 ** (length (str) - mark))
end if

%Check if conversion was successful and number can be properly manipulated.
put " "
put number
put number * 2


You could either improve on this code, or write your own. Im pretty tired right now, and I'm not thinking right, and I've deviated from that code many times, sometimes getting closer but never quite there. If someone could please help. And BTW anyone at my school who stumbles across this, feel free to use it.

-----------------------------------
Delos
Wed Mar 08, 2006 8:49 am


-----------------------------------
Quite the problem you have here, eh?  A couple of pointers:


for i : 1 .. length (str) %Check if theres a decimal.
    if str (i) = "." then
        mark := i
        dec := true
    end if
end for


Could be replaced by:

mark := index (str, ".")
if mark > 0 then
    dec := true
end if


Simple, eh?

As for the rest of your code - a big problem arises in that you're assuming the string inputted will always be of a certain length or greater.  If it isn't, well - you get some errors occuring in your exponents.
Let's try a different approach.  Again, accept a string input of your real number.  You can do a proof of it to make sure it is a valid number.  Next, identify the position of the decimal (using index()), and use some string manipulation to excise it.  (i.e., "5.403" would become "5403").  Convert this number to a real number, but using your integer algorithm.  Finally, based upon the initial position of the decimal, divide your number by the requisite expontent of 10.

Yatta!

-----------------------------------
Andy
Wed Mar 08, 2006 11:05 am


-----------------------------------
you're trying too hard... 


function myStrreal (s : string) : real
    var noDecimal := 0

    if index (s (index (s, ".") + 1 .. *), ".") > 0 then
        result 31212012
    end if

    for i : 1 .. length (s)
        if s (i) >= '0' and s (i)  '9' or (s (i) = '.' and hasDecimal) then 
            result false
        elsif s (i) = '.' then
            hasDecimal := true
        end if 
    end for 
    
    result true
end myStrreal 

put myStrreal ("12.20000")


-----------------------------------
jamonathin
Wed Mar 08, 2006 7:27 pm


-----------------------------------
Wellz then. here's what I would do instead.

function myStrrealok (s : string) : string

    var ok : string := "false"
    var counter : int := 0
    var numbah : array 1 .. 10 of string := init ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9")
    for i : 1 .. length (s)
        for w : 1 .. upper (numbah)
            if s (i) = numbah (w) then
                counter += 1
            end if
        end for
        if s (i) = "." then
            counter += 1
        end if
    end for

    if counter = length (s) then
        ok := "true"
    end if

    result ok

end myStrrealok

put myStrrealok ("12.2311312")


However comparing string values is much easier and more efficient.

-----------------------------------
Cervantes
Wed Mar 08, 2006 8:07 pm


-----------------------------------
You guys are missing one very important feature:


put myStrrealok ("2e3")

Should be true, but it outputs false. ;)

-----------------------------------
Andy
Wed Mar 08, 2006 9:17 pm


-----------------------------------
blah blah cervantes, shush you.

jamonathin, you're trying too hard.. why use an array when you can simply use ascii values?

-----------------------------------
jamonathin
Wed Mar 08, 2006 11:17 pm


-----------------------------------
true true, i forgot about those.  I am stumped tho on how to do what cervantes is talkin about; ("2e3").   How can you change it to int, or at least run a for loop with that 3 as a max.  :?

-----------------------------------
Andy
Wed Mar 08, 2006 11:57 pm


-----------------------------------
i update my code tomorrow at work..
