Computer Science Canada

Base Converter

Author:  Blindmelon_53 [ Mon Apr 26, 2004 7:22 am ]
Post subject:  Base Converter

This is my base converter that I created with some help from the compsci members

code:

%The "Number System Converter"
%Enter a number, the base its in and it will convert it to the base you desire

var winID : int
winID := Window.Open ("position:center;center") %Opens the window with the screen size specified
%The following procedure fades in text when you call them with the proper perameters filled
procedure FadeIn (title : string, x, y : int, size, speed : int)
    var font : int := Font.New ("Comic Sans MS:" + intstr (size))
    for decreasing i : 31 .. 16
        Font.Draw (title, x, y, font, i)
        delay (speed)
    end for
    put ""
end FadeIn
%The following procedure fades out text when you call them with the proper perameters filled
procedure FadeOut (title : string, x, y : int, size, speed : int)
    var font : int := Font.New ("Comic Sans MS:" + intstr
        (size))
    for i : 16 .. 31
        Font.Draw (title, x, y, font, i)
        delay (speed)
    end for
    put ""
end FadeOut

%This is where the Fade in and Fade out
FadeIn ("Number System Converter", 25, 199, 35, 100)
FadeOut ("Number System Converter", 25, 199, 35, 100)

FadeIn ("Converts Base to Base", 200, 199, 15, 100)
FadeOut ("Converts Base to Base", 200, 199, 15, 100)

FadeIn ("By Danny Klassen", 200, 199, 15, 75)
FadeOut ("By Danny Klassen", 200, 199, 15, 75)

%The next four lines draws a box the size of the screen that fades from white to black
for decreasing j : 31 .. 16
    Draw.FillBox (0, 0, maxx, maxy, j)
    delay (100)
end for

procedure converter

    var base1, base2, dec : int     %Declares all of the int variables
    var num1, num2, input1, input2 : string     %Declares all of the string variables
    colorback (16)     %sets the background color to black
    color (31)     %Sets the text color to white
    locate (1, 1)     %locates the text to row 1 column 1

    put "What would you like to do?"
    put "i - Learn About Bases"
    put "c - Convert Numbers"
    get input1
    if input1 = "i" then
        put "Base 2 = Binary"
        put "Base 8 = Octal"
        put "Base 10 = Decimal"
        put "Base 16 = Hexidecimal"
        put "Are you done? x to exit"
        get input2
        if input2 = "x" then
            cls
            converter
        else
            put "invalid entry"
            converter
        end if
    elsif input1 = "c" then

        put "Which number would you like to convert?"
        get num1 %Receives input from user

        cls

        put "Which base is this number in?"
        get base1 %Receives input from user

        cls

        put "Which base would you like to convert this number to?"
        get base2 %Receives input from user

        cls
        /*The following takes all of the input received and basically
         repeats it all for the users benifit*/
        put "You inputed", " ", num1, " ",
            " as the number you wanted converted"
        put "You inputed", " ", base1, " ",
            " as the base you are converting from"
        put "You inputed", " ", base2, " ",
            " as the base you are converting to"
        put ""
        /*The next two lines are the heart of the program.
         They do all of the converting of numbers from any base to any base*/
        dec := strint (num1, base1)
        num2 := intstr (dec, 0, base2)

        color (54) %Sets text color to light blue
        /*The following put statements output the conversion data in color coding
         so that it is easy to see where the conversion is */
        put num1, " " ..
        color (31)
        put "in base", " " ..
        color (yellow)
        put base1 ..
        color (31)
        put " ", "is", " " ..
        color (54)
        put num2, " " ..
        color (31)
        put "in base ", " " ..
        color (yellow)
        put base2
        color (31)

        put " "
else
converter
        end if

end converter
/*The next lines of code are a menu I created to allow the user to choose if s/he wants to
 convert another number. If they input 1 the it will go back to the begining otherwise it will
 exit*/
procedure menu
    var input : int
    loop
        put " Would you like to convert another number?"
        put " 1. Yes "
        put " 2. No "
        put " Input 1 or 2 for the above selection options"
        get input

        if input = 1 then
            cls %Clears the screen
            converter %Calls the converter procedure
        elsif input = 2 then
            Window.Close (winID) %Closes the window
            exit %ends the program
        else

            put "Invalid Entry"
            delay (2000)
            cls
            menu %Calls the menu procedure
        end if
    end loop
end menu


converter %Calls the procedure converter
menu %Calls the procedure menu

Author:  Flashkicks [ Mon Apr 26, 2004 10:41 am ]
Post subject: 

Danny- I Like I Like Laughing


: