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

Username:   Password: 
 RegisterRegister   
 Turing Conversion program
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
TaKer




PostPosted: Sun May 30, 2010 10:06 pm   Post subject: Turing Conversion program

What is it you are trying to achieve?
i am trying to create a program that will allow 12 conversions. Decimal- binary, Dec-hexa, Dec-Oct and so forth.

What is the problem you are having?
I need help creating this program as i need the program to use functions
I have used the search button so please dont bother rubbing it in my face. The programs i found do not use functions, so they are not of much use to me.

Here is the code i have so far with the initial start up to the program. I am new to this and have very little clue to what i am doing. If you can help me with the other functions i will be greatful
Turing:


%Main start up, asking for what to convert%
var sChoose : string := ""
var iChoose2 : int := 0
var sChoosedecimal : int := 0
var sChoosebinary : string := ""
var sChoosehexadecimal : string := ""
var sChooseanswer : boolean
var sChooseoctal : string := ""
loop
    put "This is the Conversion program that will convert any number"
    put "What do you want to convert, decimal, binary or hexadecimal"
    get sChoose
    exit when sChoose = "decimal" or sChoose = "binary" or sChoose = "hexadecimal"
end loop
%gets value for the first choice%
loop
    put "Enter your ", sChoose, " number"
    if sChoose = "decimal" then
        get sChoosedecimal
        sChooseanswer := true
    elsif sChoose = "binary" then
        get sChoosebinary
        sChooseanswer := true
    elsif sChoose = "hexadecimal" then
        get sChoosehexadecimal
        sChooseanswer := true
elsif sChoose = "octal" then
get sChooseoctal
sChooseanswer:= true
    end if
    exit when sChooseanswer = true
end loop
%gets the value of the second choice%
loop
    put "What do you want to convert it into?"
    put "Enter 1 for decimal, 2 for binary, 3 for hexadecimal or 4 for octal"
    get iChoose2
    if iChoose2 = 1 then
        put "Enter your decimal number from 1-255"
    elsif iChoose2 = 2 then
        put "Enter your 8 digit binary number"
    elsif iChoose2 = 3 then
        put "Enter your 2 digit hexadecimal value"
    end if
    exit when iChoose2 = 1 or iChoose2 = 2 or iChoose2 = 3
end loop
%Decimal to Binary conversion%
function dectobin (iNum2 : int) : string
    var iBinary : int := 256
    var stringbin : string := ""
    var iNum := iNum2
    for i : 1 .. 8
        iBinary := iBinary div 2
        if iNum - iBinary > 0 then
            stringbin := stringbin + "1"
            iNum := iNum - iBinary
        else
            stringbin := stringbin + "0"
        end if
    end for
    result stringbin
end dectobin
%Octal to Decimal Conversion%
        function octtodec (iNum2 : string) : int
            var iOctal : int := 0
            if iNum2 (1) = "1" then
                iOctal := iOctal + 64 %this is where 8 is to the power of 2 multiplied by the number entered, for the first digit of octal%
            elsif iNum2 (1) = "2" then
                iOctal := iOctal + 128
            elsif iNum2 (1) = "3" then
                iOctal := iOctal + 192
            elsif iNum2 (1) = "4" then
                iOctal := iOctal + 256
            elsif iNum2 (1) = "5" then
                iOctal := iOctal + 320
            elsif iNum2 (1) = "6" then
                iOctal := iOctal + 384
            elsif iNum2 (1) = "7" then
                iOctal := iOctal + 448
            end if
            if iNum2 (2) = "1" then % this is where 8 is to the power of 1 and multiplied by the number entered, for the second digit of octal%
                iOctal := iOctal + 8
            elsif iNum2 (2) = "2" then
                iOctal := iOctal + 16
            elsif iNum2 (2) = "3" then
                iOctal := iOctal + 24
            elsif iNum2 (2) = "4" then
                iOctal := iOctal + 32
            elsif iNum2 (2) = "5" then
                iOctal := iOctal + 40
            elsif iNum2 (2) = "6" then
                iOctal := iOctal + 48
            elsif iNum2 (2) = "7" then
                iOctal := iOctal + 56
            end if
            if iNum2 (3) = "1" then %this is where there is no power of 8 and it is just the number entered for the last digit%
                iOctal := iOctal + 1
            elsif iNum2 (3) = "2" then
                iOctal := iOctal + 2
            elsif iNum2 (3) = "3" then
                iOctal := iOctal + 3
            elsif iNum2 (3) = "4" then
                iOctal := iOctal + 4
            elsif iNum2 (3) = "5" then
                iOctal := iOctal + 5
            elsif iNum2 (3) = "6" then
                iOctal := iOctal + 6
            elsif iNum2 (3) = "7" then
                iOctal := iOctal + 7
            end if
            result iOctal
        end octtodec



Please specify what version of Turing you are using
<Answer Here>
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Sun May 30, 2010 10:38 pm   Post subject: RE:Turing Conversion program

You can do this whole thing with one function that takes a number to be converted, its current base and the base to convert to. It's a math thing more than a programming thing. I've got a ruby or perl version of this somewhere but I doubt you can read it.
TaKer




PostPosted: Sun May 30, 2010 10:46 pm   Post subject: RE:Turing Conversion program

Right now i am looking for the longer way as the teacher expects me to have 12 functions...
Insectoid




PostPosted: Sun May 30, 2010 11:01 pm   Post subject: RE:Turing Conversion program

Your teacher expects you to be retarded. Make one good function that is worthy of handing in, then a pile of stupid useless ones that do nothing useful. God, I hate this curriculum.
Tony




PostPosted: Mon May 31, 2010 12:12 am   Post subject: RE:Turing Conversion program

Insectoid's point is that all of those functions are almost identical. If you can write one, you can write them all. Taking it a step further -- all of those differences could be replaced by using a variable for the base.

You might also be interested in strint -- that will change your "2"s to 2s, so that you don't have to write out all of the if-elsif-elsif-...s
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: