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

Username:   Password: 
 RegisterRegister   
 hELP! ALL CONVERSTIONS PROJECT....
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
hexx




PostPosted: Mon Dec 09, 2002 6:12 pm   Post subject: hELP! ALL CONVERSTIONS PROJECT....

help me.... i have stupid project to do.....

i finsihed decimal to all places.....

i need to do:

-binary to all bases
-Hex to all bases and
-Octal to all bases


--------------------------------------------------------------------------------------
plz fix this HEX > DECIMAL prog for me:
code:

var hd:string

put "Enter number (hex) toconvert to decimal)"
get hd


var temp : string := ""
    var m, t, c, final, final1 : int := 0


    temp := hd
    c := length (temp)
    for i : 1 .. length (temp) - 1
        c := c - 1
        put "I LOOP ", i
        put "C LOOP ", c

        if temp (i) = "A" then
            m := 10
        elsif temp (i) = "B" then
            m := 11
        elsif temp (i) = "C" then
            m := 12
        elsif temp (i) = "D" then
            m := 13
        elsif temp (i) = "E" then
            m := 14
        elsif temp (i) = "F" then
            m := 15
        else
            m := strint (temp (i))
        end if

        if c = 1 and temp (i + 1) = "A" then
            final1 := 10
        elsif c = 1 and temp (i + 1) = "B" then
            final1 := 11
        elsif c = 1 and temp (i + 1) = "C" then
            final1 := 12
        elsif c = 1 and temp (i + 1) = "D" then
            final1 := 13
        elsif c = 1 and temp (i + 1) = "E" then
            final1 := 14
        elsif c = 1 and temp (i + 1) = "F" then
            final1 := 15
        elsif c = 1 then
            final1 := strint (temp (i))

        end if


        put "Number or letters replaced by? ", m

        t := m * 16 ** c

        put "Just multiploed m by 16 and squred by c loop.. ", t

        final := final + t

        if c = 1 then
            final := final + final1
        end if
        put "MY FINAL LAST ", final1
        put "FINAL: ", final

    end for



    put final
Sponsor
Sponsor
Sponsor
sponsor
Dan




PostPosted: Mon Dec 09, 2002 6:43 pm   Post subject: hummm

well i dont know what you are sposted to do for this progject excltey but you can use a comand in turing to covert bases.


hex to dec
code:

var num: string := "FF"

put strint(num, 16)


dec to hex
code:

var num: int := 10

put intstr(num,0, 16)


dec to binaery
code:

var num: int := 10

put intstr(num,0, 2)



the code for intstr is

intstr ( i : int [ , width : int [ , base : int ] ] ) : string

the code for strint is

strint ( s : string [ , base : int ] ) : int


P.S. some teachers dont like this method becuse they did not take the time to look at the strint and intstr fuctions. so they think this is a big progject.
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
hexx




PostPosted: Mon Dec 09, 2002 11:55 pm   Post subject: (No subject)

lol is IT THAT EASY? man... if i knew.. that

but our teacher is amrking the code so i would apprecaite it someone could get me the LONG VERSION of this...


thnx!
Tony




PostPosted: Tue Dec 10, 2002 12:14 am   Post subject: (No subject)

well tell your teacher that its a bad programming practice to reinvent the wheel as the functions are already there, build in and ready to use. It saves time (therefor money), are executed faster and more accuratly and with no bugs (or less then your function).

anyway, binary to decimal:

code:

var d : int := 128
var num : string := "00000010"
var decimal : int := 0
for i : 1 .. 8
    if num (i) = "1" then
        decimal := decimal + d
    end if
    d := round (d / 2)
end for

put decimal


In other bases, you just replace d with base ** length(num) and replace /2 with /base

that gotta convert every base to decimal for you.
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  [ 4 Posts ]
Jump to:   


Style:  
Search: