Computer Science Canada

Binary Addition Program

Author:  Pa|2a|)oX [ Sat Feb 19, 2005 10:37 am ]
Post subject:  Binary Addition Program

This is just a small binary addition program, takes any two values and adds them together without converting between bases (binary to decimal and back).

It is not error trapped so if you enter a number other than 0 or 1 an error will most likely occur.

Here's the code.


code:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Kostia P.                            %%
%%Mr. Wong                            %%
%%Program : Binary Addition in Turing.%%
%%Finished February 17th 2005         %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Set the screen size, colour and font
View.Set ("graphics:340;200,position:bottom;left,nobuttonbar")
drawfillbox (0, 0, maxx, maxy, black)
colorback (black)
color (yellow)

%Variable Declaration
var snum1, snum2 : string
var lnum1 : int
var lnum2 : int
var query : string (1)

%Procedure - Length of StringNum1 is Greater
procedure glnum1
    %Individual Variables
    var dif : int := lnum1 - lnum2
    var lenum2 : string := snum2
    for x : 1 .. dif
        lenum2 := "0" + lenum2
    end for
    %%%%%%%%%%%%%%%%%%%%%%%%
    var add1, add2 : array 1 .. lnum1 of int
    var final : array 1 .. lnum1 of int
    var fdigit : int := 0
    var carry : int := 0
    %%%%%%%%%%%%%%%%%%%%%%%%
    for i : 1 .. lnum1
        add1 (i) := strint (snum1 (i))
    end for
    for i : 1 .. lnum1
        add2 (i) := strint (lenum2 (i))
    end for
    %%%%%%%%%%%%%%%%%%%%%%%%
    for decreasing k : lnum1 .. 1
        if k = 1 and add1 (k) + add2 (k) = 0 and carry = 0 then
            fdigit := 0
        elsif k = 1 and add1 (k) + add2 (k) = 1 and carry = 0 then
            fdigit := 1
        elsif k = 1 and add1 (k) + add2 (k) = 1 and carry = 1 then
            fdigit := 10
        elsif k = 1 and add1 (k) + add2 (k) = 2 and carry = 0 then
            fdigit := 10
        elsif k = 1 and add1 (k) + add2 (k) = 2 and carry = 1 then
            fdigit := 11
        end if
        %%%%
        if add1 (k) + add2 (k) = 0 and carry = 0 then
            final (k) := 0
            carry := 0
        elsif add1 (k) + add2 (k) = 0 and carry = 1 then
            final (k) := 1
            carry := 0
        elsif add1 (k) + add2 (k) = 1 and carry = 0 then
            final (k) := 1
            carry := 0
        elsif add1 (k) + add2 (k) = 1 and carry = 1 then
            final (k) := 0
            carry := 1
        elsif add1 (k) + add2 (k) = 2 and carry = 0 then
            final (k) := 0
            carry := 1
        elsif add1 (k) + add2 (k) = 2 and carry = 1 then
            final (k) := 1
            carry := 1
        end if
    end for
    %Put the final values
    put "The addition these two values produce.."
    put fdigit ..
    if lnum1 > 1 then
        for t : 2 .. lnum1
            put final (t) ..
        end for
    end if

end glnum1
%Procedure - Second number is greater
procedure glnum2
    %Reversal of variables
    var temp1, temp2 : string
    temp1 := snum1
    temp2 := snum2
    snum1 := temp2
    snum2 := temp1

    lnum1 := length (snum1)
    lnum2 := length (snum2)
    %%%%%%%%%%%%%%%%%%%%%%%%
    var dif : int := lnum1 - lnum2
    var lenum2 : string := snum2
    for x : 1 .. dif
        lenum2 := "0" + lenum2
    end for
    %%%%%%%%%%%%%%%%%%%%%%%%
    var add1, add2 : array 1 .. lnum1 of int
    var final : array 1 .. lnum1 of int
    var fdigit : int := 0
    var carry : int := 0
    %%%%%%%%%%%%%%%%%%%%%%%%
    for i : 1 .. lnum1
        add1 (i) := strint (snum1 (i))
    end for
    for i : 1 .. lnum1
        add2 (i) := strint (lenum2 (i))
    end for
    %%%%%%%%%%%%%%%%%%%%%%%%
    for decreasing k : lnum1 .. 1
        if k = 1 and add1 (k) + add2 (k) = 0 and carry = 0 then
            fdigit := 0
        elsif k = 1 and add1 (k) + add2 (k) = 1 and carry = 0 then
            fdigit := 1
        elsif k = 1 and add1 (k) + add2 (k) = 1 and carry = 1 then
            fdigit := 10
        elsif k = 1 and add1 (k) + add2 (k) = 2 and carry = 0 then
            fdigit := 10
        elsif k = 1 and add1 (k) + add2 (k) = 2 and carry = 1 then
            fdigit := 11
        end if
        %%%%%%%%%%%%%%%%%%%%%%%%
        if add1 (k) + add2 (k) = 0 and carry = 0 then
            final (k) := 0
            carry := 0
        elsif add1 (k) + add2 (k) = 0 and carry = 1 then
            final (k) := 1
            carry := 0
        elsif add1 (k) + add2 (k) = 1 and carry = 0 then
            final (k) := 1
            carry := 0
        elsif add1 (k) + add2 (k) = 1 and carry = 1 then
            final (k) := 0
            carry := 1
        elsif add1 (k) + add2 (k) = 2 and carry = 0 then
            final (k) := 0
            carry := 1
        elsif add1 (k) + add2 (k) = 2 and carry = 1 then
            final (k) := 1
            carry := 1
        end if
    end for
    %Put final answer
    put "The addition these two values produce.."
    put fdigit ..
    if lnum1 > 1 then
        for t : 2 .. lnum1
            put final (t) ..
        end for
    end if

end glnum2

%Equal Amount
procedure glequal
    %%%%%%%%%%%%%%%%%%%%%%%%
    var add1, add2 : array 1 .. lnum1 of int
    var final : array 1 .. lnum1 of int
    var fdigit : int := 0
    var carry : int := 0
    %%%%%%%%%%%%%%%%%%%%%%%%
    for i : 1 .. lnum1
        add1 (i) := strint (snum1 (i))
    end for
    for i : 1 .. lnum2
        add2 (i) := strint (snum2 (i))
    end for
    %%%%%%%%%%%%%%%%%%%%%%%%
    for decreasing k : lnum1 .. 1
        if k = 1 and add1 (k) + add2 (k) = 0 and carry = 0 then
            fdigit := 0
        elsif k = 1 and add1 (k) + add2 (k) = 1 and carry = 0 then
            fdigit := 1
        elsif k = 1 and add1 (k) + add2 (k) = 1 and carry = 1 then
            fdigit := 10
        elsif k = 1 and add1 (k) + add2 (k) = 2 and carry = 0 then
            fdigit := 10
        elsif k = 1 and add1 (k) + add2 (k) = 2 and carry = 1 then
            fdigit := 11
        end if
        %%%%%%%%%%%%%%%%%%%%%%%%
        if add1 (k) + add2 (k) = 0 and carry = 0 then
            final (k) := 0
            carry := 0
        elsif add1 (k) + add2 (k) = 0 and carry = 1 then
            final (k) := 1
            carry := 0
        elsif add1 (k) + add2 (k) = 1 and carry = 0 then
            final (k) := 1
            carry := 0
        elsif add1 (k) + add2 (k) = 1 and carry = 1 then
            final (k) := 0
            carry := 1
        elsif add1 (k) + add2 (k) = 2 and carry = 0 then
            final (k) := 0
            carry := 1
        elsif add1 (k) + add2 (k) = 2 and carry = 1 then
            final (k) := 1
            carry := 1
        end if
    end for
    %Put final answer
    put "The addition these two values produce.."
    put fdigit ..
    if lnum1 > 1 then
        for t : 2 .. lnum1
            put final (t) ..
        end for
    end if
end glequal
%Procedure - Call glnum1, glnum2, equal
procedure procCall
    if lnum1 > lnum2 then
        glnum1
    elsif lnum1 < lnum2 then
        glnum2
    else
        glequal
    end if
end procCall
%Question Loop
loop
    %Get snum1, snum2
    put "Please enter the first binary number.."
    get snum1
    put "Please enter the second binary number.."
    get snum2
    lnum1 := length (snum1)
    lnum2 := length (snum2)
    procCall
    put ""
    put ""
    put "Would you like to calculate another value?"
    get query
    if query = "y" or query = "yes" or query = "Yes" or query = "Y" then
        delay (400)
        cls
    elsif query = "n" or query = "N" or query = "No" or query = "no" then
        exit
    else
        exit
    end if
end loop
%End program
[/code]

Author:  cycro1234 [ Sat Feb 19, 2005 1:49 pm ]
Post subject: 

Nice Smile

Somebody made a program similar to this. They converted keys to binary. Razz


: