Computer Science Canada

How to identify numbers idividually (read more)

Author:  drummaboy [ Tue Dec 08, 2009 9:27 pm ]
Post subject:  How to identify numbers idividually (read more)

What is it you are trying to achieve?
I want to make it so that when my calculator outputs a binary number (decimal to binary)
a red ball (drawfilloval) lights up if the number is one and doesn't light up if it's zero (meaning it's off)



What is the problem you are having?
I don't know how to detect each number so that I can make each ball light up if the binary number is 1


Ex: 1110 0001
I want 8 balls at the bottom of the screen
the first 3 would light up (b/c the first three numbers are 1's)
the fourth to seventh one would be off
and the eighth one on.

and when I say one... I want it jus to be a drawoval and then if it's 1 then it becomes a
drawfilloval that's red. Thanks




Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>

Turing:


%DRUMMA BOY'S CALCULATOR PROJECT
%MR. X
%GR.X
%

% procedure for the main menu
procedure mainmenu
     var beg : int
    var ending : int
beg := 1
ending := 2
    beg := ending
%Background colour
    colorback (black)
    drawfillbox (0, 0, maxx, maxy, black)
    %My writting in the background
    var fnt : int
    fnt := Font.New ("Harrington:25")
    Draw.Text ("Calculator program", 150, maxy div 2, fnt, brightred)
    %my options in the menu
    locate (15, 20)
    Text.Color (75)
    put "Press 1 - For Conversion from Decimal to Binary"
    locate (18, 20)
    Text.Color (25)
    put "Press 2 - For Conversion from Binary to Decimal"
    locate (21, 20)
    Text.Color (5)
    put "Press 3 - To exit"

% end of procedure main  menu
% start of the actual calculator


% my picture of the calculator
var calculator : int
calculator := Pic.FileNew ("calculate.bmp")
var num : int
var button : string (1)
locate (24,20)
Text.Color (yellow)
get num

if num = 1 then
    locate (24, 20)
    Text.Color (75)
    put "You have chosen Decimal to binary"
    delay (1500)

    drawfillbox (0, 0, maxx, maxy, yellow)
    colorback (yellow)
    Pic.Draw (calculator, 150, -100, picCopy)
    locate (1, 20)
    colorback (black)
   
    beg := 1
    ending := 100000
    var decimal : int
    var final : string
    for i : beg .. ending
        colorback (black)
        color (white)
        put "Choose a decimal between 0 and 255"

        colorback (white)
        locate (4, 35)
        color (black)
        get decimal
        % if the number is less than 0 or greater than 255 this happens
        if decimal < 0 or decimal > 255 then
            put "between 0 and 255"
            delay (1000)
            Text.Cls
            drawfillbox (0, 0, maxx, maxy, yellow)
            colorback (yellow)
            Pic.Draw (calculator, 150, -100, picCopy)
            locate (1, 20)
            colorback (black)
        else
            % changing my integer to a string and converting it
            %to a base 2 therefore making it binary without the use
            %of div or mod
            final := intstr (decimal, 0, 2)
            locate (5, 30)
            put "Your binary number is"
            locate (6, 35)
            Text.Color (9)
            put final
            colorback (black)
            Text.Color (50)
            %options after your answer is calculated
            put "if you want to clear the screen & keep the calculator press 'z'"
            put "If you want to go to the main menu press 'm'"
            put "if you want to get rid of the calculator, press any other button"
            % getch is used to wait until a button is pressed.
            % If i don't press a button nothing happens

            getch (button)
            cls
            if button = ("z") then
                Text.Cls
                drawfillbox (0, 0, maxx, maxy, yellow)
                colorback (yellow)
                Pic.Draw (calculator, 150, -100, picCopy)
                locate (1, 20)
                colorback (black)

            elsif button = ("m") then
            mainmenu
           
           
    end if
            end if
            end for
         
            end if

if num = 2 then
    locate (24, 20)
    Text.Color (25)
    put "You have chosen Binary to Decimal"
    delay (1500)
    beg := 1
    ending := 100000
    for l : beg .. ending
        %this is my background for my calculator
        drawfillbox (0, 0, maxx, maxy, yellow)
        colorback (yellow)
        Pic.Draw (calculator, 150, -100, picCopy)
        locate (1, 20)
        colorback (white)
        put "Enter a Binary number (between 0-8 characters)"

        locate (5, 35)
        var binary : string
        var finale : int

        get binary
        if binary < "0" or binary > "11111111" then
            put "Error - Between 0 - 8 characters and no negative numbers"
            put "Binary is contained of 0's and 1's -Try again"
            delay (3000)
            Text.Cls
        else
            % changing my variable for a sting to an integer
            % It takes the number you input and identifies what base you have
            % and converts it to base 10
            % Ex: If you comment out the if statmnt above and change
            % strint (binary,2) to strint (binary,16) and run the program.
            % and you type in FF, the outcome would be 255 (base 10).


            finale := strint (binary, 2)
            locate (4, 35)
            Text.Color (65)
            put "your decimal number is"
            locate (5, 35)
            Text.Color (35)
            put finale
            delay (1000)
            %options after your answer is calculated
            put "if you want to clear the screen & keep the calculator press 'z'"
            put "If you want to go to the main menu press 'm'"
            put "if you want to get rid of the calculator, press any other button"
            % getch is used to wait until a button is pressed.
            % If i don't press a button nothing happens

            getch (button)
            cls
            if button = ("z") then
                Text.Cls
                drawfillbox (0, 0, maxx, maxy, yellow)
                colorback (yellow)
                Pic.Draw (calculator, 150, -100, picCopy)
                locate (1, 20)
                colorback (black)





                elsif button = ("m") then

                    mainmenu

                end if
            end if
        end for
            end if
   

    if num = 3 then
        locate (24, 20)
        Text.Color (5)
        put "You have chosen to exit"
        colorback (black)
        drawfillbox (0,0,maxx,maxy,black)
        Draw.Text ("Bye", maxx div 2 -50, maxy div 2,fnt,brightred)
        delay (2000)
        quit
    end if


end mainmenu
mainmenu




Please specify what version of Turing you are using
4.1.1

Author:  drummaboy [ Tue Dec 08, 2009 9:28 pm ]
Post subject:  Re: How to identify numbers idividually (read more)

I forgot to put in the calculator image

Author:  GalacticVenus [ Tue Dec 08, 2009 10:24 pm ]
Post subject:  RE:How to identify numbers idividually (read more)

For Binary, I recommend making each number acquired and scanned individually. The way it's programmed now, it looks like I could just type in 0999 9999 and it would accept it.


Something like

for count : 1 .. 8
hasch
if 1
makeovalred
elsif 0
do nothing
else say "not a valid binary character".
end if
end count


(Of course, I started Turing a week or 2 ago, so get Tony's advice before doing anything. Razz)

Author:  mirhagk [ Tue Dec 08, 2009 10:40 pm ]
Post subject:  RE:How to identify numbers idividually (read more)

well as long as you can get the binary numbers you can make them into booleans (which is the same thing as binary, where 1 is true and 0 is false)
Turing:

var binary : string := "11100001"
var binary_num : array 1 .. 8 of boolean
for i : 1 .. 8
    if binary (i) = "1" then
        binary_num (i) := true
    else
        binary_num (i) := false
    end if
end for
for i : 1 .. 8 %draw balls
    if binary_num (i) then
        drawfilloval (100 + 20 * i, 50, 10, 10, red)
    end if
end for


: