Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Useful Procedures
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
blaster009




PostPosted: Mon May 15, 2006 7:24 pm   Post subject: Useful Procedures

Haha, just felt like making a post...This is a file I usually import into my other programs. It's got a couple of neat things in it.

code:

% Created by Ben Cassell
% Created 2006/01/25
% Last Updated 2006/03/01
% Useful assortment of procedures

% Show all preset colours
proc colourShow
    var window : int := Window.Open ("graphics:564;500")
    var font : int := Font.New ("timesnewroman:10")
    for i : 0 .. 16
        for j : 0 .. 14
            Draw.FillBox (i * 32, j * 32, i * 32 + 32 - 1, j * 32 + 32 - 1, (i + 1) + (j * 17))
            Draw.Text (intstr (i + 1), i * 32 + 10, 485, font, 7)
            Draw.Text (intstr (j + 1), 547, j * 32 + 10, font, 7)
        end for
    end for
end colourShow

% Find a Fibonacci number at a position: Must use 0, 1, 1, userNum
proc fibFind (numBefore, numCurrent, numPosition, numStop : int)
    if numPosition ~= numStop then
        fibFind (numCurrent, numCurrent + numBefore, numPosition + 1, numStop)
    else
        put numCurrent
    end if
end fibFind

% Find the hypoteneuse of a triangle
fcn hypFind (side1, side2 : real) : real
    result sqrt (side1 ** 2 + side2 ** 2)
end hypFind

% Reverse an array of integers
proc intReverse (var list : array 0 .. * of int)
    var tempList : array 0 .. upper (list) of int
    for i : 0 .. upper (list)
        tempList (i) := list (i)
    end for
    for i : 0 .. upper (list)
        list (i) := tempList (upper (tempList) - i)
    end for
end intReverse

% Swap two integers
proc intSwap (var list : array 0 .. * of int, pos1, pos2 : int)
    var tempNum : int
    tempNum := list (pos1)
    list (pos1) := list (pos2)
    list (pos2) := tempNum
end intSwap

% Quick sort a set of integers: Must use list, lower index, upper index
proc intSort (var list : array 0 .. * of int, left, right : int)
    var pivot : int
    intSwap (list, left, (left + right) div 2)
    var small := left
    for i : left + 1 .. right
        if list (i) <= list (left) then
            small += 1
            intSwap (list, small, i)
        end if
    end for
    intSwap (list, left, small)
    pivot := small
    if left < pivot - 1 then
        intSort (list, left, pivot - 1)
    end if
    if pivot + 1 < right then
        intSort (list, pivot + 1, right)
    end if
end intSort

% Reverse an array of reals
proc realReverse (var list : array 0 .. * of real)
    var tempList : array 0 .. upper (list) of real
    for i : 0 .. upper (list)
        tempList (i) := list (i)
    end for
    for i : 0 .. upper (list)
        list (i) := tempList (upper (tempList) - i)
    end for
end realReverse

% Swap two reals
proc realSwap (var list : array 0 .. * of real, pos1, pos2 : int)
    var tempNum : real
    tempNum := list (pos1)
    list (pos1) := list (pos2)
    list (pos2) := tempNum
end realSwap

% Quick sort a set of reals: Must use list, lower index, upper index
proc realSort (var list : array 0 .. * of real, left, right : int)
    var pivot : int
    realSwap (list, left, (left + right) div 2)
    var small := left
    for i : left + 1 .. right
        if list (i) <= list (left) then
            small += 1
            realSwap (list, small, i)
        end if
    end for
    realSwap (list, left, small)
    pivot := small
    if left < pivot - 1 then
        realSort (list, left, pivot - 1)
    end if
    if pivot + 1 < right then
        realSort (list, pivot + 1, right)
    end if
end realSort

% Find the slope of a line
fcn slopeFind (x1, y1, x2, y2 : real) : real
    result (y2 - y1) / (x2 - x1)
end slopeFind

% Mirror a string
fcn strMirror (word : string) : string
    var resultWord : string := ""
    for decreasing i : length (word) .. 1
        resultWord += word (i)
    end for
    result resultWord
end strMirror

% Reverse an array of reals
proc strReverse (var list : array 0 .. * of string)
    var tempList : array 0 .. upper (list) of string
    for i : 0 .. upper (list)
        tempList (i) := list (i)
    end for
    for i : 0 .. upper (list)
        list (i) := tempList (upper (tempList) - i)
    end for
end strReverse

% Remove all blank spaces from a string
fcn strSpaceRemove (var word : string) : string
    var resultWord : string := ""
    for i : 1 .. length (word)
        if word (i) ~= " " then
            resultWord += word (i)
        end if
    end for
    result resultWord
end strSpaceRemove

% Test a word to check for a palindrome
fcn palindromeCheck (var word : string, spaces : boolean) : boolean
    var reverseWord : string
    word := Str.Lower (word)
    if ~spaces then
        word := strSpaceRemove (word)
    end if
    reverseWord := strMirror (word)
    if word = reverseWord then
        result true
    end if
    result false
end palindromeCheck

% Swap two strings
proc strSwap (var list : array 0 .. * of string, pos1, pos2 : int)
    var tempNum : string
    tempNum := list (pos1)
    list (pos1) := list (pos2)
    list (pos2) := tempNum
end strSwap

% Quick sort a set of strings: Must use list, lower index, upper index
proc strSort (var list : array 0 .. * of string, left, right : int)
    var pivot : int
    strSwap (list, left, (left + right) div 2)
    var small := left
    for i : left + 1 .. right
        if list (i) <= list (left) then
            small += 1
            strSwap (list, small, i)
        end if
    end for
    strSwap (list, left, small)
    pivot := small
    if left < pivot - 1 then
        strSort (list, left, pivot - 1)
    end if
    if pivot + 1 < right then
        strSort (list, pivot + 1, right)
    end if
end strSort
Sponsor
Sponsor
Sponsor
sponsor
NikG




PostPosted: Tue May 16, 2006 12:38 am   Post subject: (No subject)

Some cool/useful functions.
One note: you forgot the result in the strMirror function.
Codi




PostPosted: Tue May 16, 2006 12:02 pm   Post subject: (No subject)

Good
Cervantes




PostPosted: Tue May 16, 2006 5:34 pm   Post subject: (No subject)

A minor error: you don't import that, you include it. You can only import a Turing Unit, which can contain a class or module (or monitor?).

So, why don't you make some modules out of that? You can organize things nicely, that way. An Array module, a String module, a Math module...

Now, take that a step further. These modules are already part of the Turing library. You know the length function? It's really String.length. And the best part is that extending the Turing library is easy.
NikG




PostPosted: Tue May 16, 2006 10:38 pm   Post subject: (No subject)

Cervantes wrote:
You know the length function? It's really String.length.

Shouldn't that be Str.length?
Cervantes




PostPosted: Wed May 17, 2006 4:07 pm   Post subject: (No subject)

Good point. Thanks. Smile
[Gandalf]




PostPosted: Wed May 17, 2006 4:53 pm   Post subject: (No subject)

Codi wrote:
Good

One word answers are... not good. Why don't you read [The Rules]? Your post would generally fall under spam since it is quite useless.

I recall that Catalyst (I think) made a similar collection of useful functions and put it into a module quite a while ago, though I couldn't find it. I'm pretty sure it was called turLib, but that too yields no results.

Anyway, good job.
blaster009




PostPosted: Wed May 17, 2006 9:49 pm   Post subject: (No subject)

NikG wrote:
Some cool/useful functions.
One note: you forgot the result in the strMirror function.


Tsk tsk. I made no mistake. That's a practical joke I meant to play on people who wanted to use it. Really! I mean it! Like, completely honest! Seriously! Razz (Thanks for noticing...I fixed it up)
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: