%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Subtraction %
%Kostia Palanski %
%Mr. Wong %
%Binary Subtraction Program %
%Done: Feb. 20th 2005 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Graphics Settings
View.Set ("graphics:400;400,position:bottom;left,nobuttonbar")
drawfillbox (0, 0, maxx, maxy, black)
colorback (black)
color (yellow)
%Variable Declaration
var snum1, snum2 : string
var lsnum1, lsnum2 : int
var msnum1, msnum2 : string
var streamin, streamout : int
var osubput : string
var queryn1, queryn2, query : string
var neg1, neg2, neg : int := 0
var aosnum2 : array 1 .. 8 of int
var aosnum1 : array 1 .. 8 of int
var finalAnswer : array 1 .. 8 of int
var finalAnswerint : int
var oflow : int := 0
%TwosCompliment Variables
var twoC : string := "00000001"
var aotwoC : array 1 .. 8 of int
for f : 1 .. 8
aotwoC (f) := strint (twoC (f))
end for
%Ones Compliment for Num2
procedure onesComplimentNum2
for e : 1 .. 8
if aosnum2 (e) = 1 then
aosnum2 (e) := 0
elsif aosnum2 (e) = 0 then
aosnum2 (e) := 1
end if
end for
end onesComplimentNum2
%Ones Compliment for Num1
procedure onesComplimentNum1
for e : 1 .. 8
if aosnum1 (e) = 1 then
aosnum1 (e) := 0
elsif aosnum1 (e) = 0 then
aosnum1 (e) := 1
end if
end for
end onesComplimentNum1
%Ones Compliment for FinalAnswer
procedure onesComplimentFA
for e : 1 .. 8
if finalAnswer (e) = 1 then
finalAnswer (e) := 0
elsif finalAnswer (e) = 0 then
finalAnswer (e) := 1
end if
end for
end onesComplimentFA
%Twos Compliment for aosnum1
procedure twosComplimentNum1
%%%%%%%%%%%%%%%%%%%%%%%%
var final : array 1 .. 8 of int
var fdigit : int := 0
var carry : int := 0
%%%%%%%%%%%%%%%%%%%%%%%%
for decreasing g : 8 .. 1
if g = 1 and aosnum1 (g) + aotwoC (g) = 0 and carry = 0 then
fdigit := 0
elsif g = 1 and aosnum1 (g) + aotwoC (g) = 1 and carry = 0 then
fdigit := 1
elsif g = 1 and aosnum1 (g) + aotwoC (g) = 1 and carry = 1 then
fdigit := 10
elsif g = 1 and aosnum1 (g) + aotwoC (g) = 2 and carry = 0 then
fdigit := 10
elsif g = 1 and aosnum1 (g) + aotwoC (g) = 2 and carry = 1 then
fdigit := 11
end if
%%%%%%%%%%%%%%%%%%%%%%%%
if aosnum1 (g) + aotwoC (g) = 0 and carry = 0 then
final (g) := 0
carry := 0
elsif aosnum1 (g) + aotwoC (g) = 0 and carry = 1 then
final (g) := 1
carry := 0
elsif aosnum1 (g) + aotwoC (g) = 1 and carry = 0 then
final (g) := 1
carry := 0
elsif aosnum1 (g) + aotwoC (g) = 1 and carry = 1 then
final (g) := 0
carry := 1
elsif aosnum1 (g) + aotwoC (g) = 2 and carry = 0 then
final (g) := 0
carry := 1
elsif aosnum1 (g) + aotwoC (g) = 2 and carry = 1 then
final (g) := 1
carry := 1
end if
end for
%Put output
open : streamout, "subput.txt", put
for h : 1 .. 8
put : streamout, final (h) ..
end for
close : streamout
%Get it to one variable
open : streamin, "subput.txt", get
get : streamin, osubput : *
close : streamin
%Reassign the new TwosCompliment to aosnum1
for i : 1 .. 8
aosnum1 (i) := strint (osubput (i))
end for
end twosComplimentNum1
%Twos Compliment for aosnum2
procedure twosComplimentNum2
%%%%%%%%%%%%%%%%%%%%%%%%
var final : array 1 .. 8 of int
var fdigit : int := 0
var carry : int := 0
%%%%%%%%%%%%%%%%%%%%%%%%
for decreasing g : 8 .. 1
if g = 1 and aosnum2 (g) + aotwoC (g) = 0 and carry = 0 then
fdigit := 0
elsif g = 1 and aosnum2 (g) + aotwoC (g) = 1 and carry = 0 then
fdigit := 1
elsif g = 1 and aosnum2 (g) + aotwoC (g) = 1 and carry = 1 then
fdigit := 10
elsif g = 1 and aosnum2 (g) + aotwoC (g) = 2 and carry = 0 then
fdigit := 10
elsif g = 1 and aosnum2 (g) + aotwoC (g) = 2 and carry = 1 then
fdigit := 11
end if
%%%%%%%%%%%%%%%%%%%%%%%%
if aosnum2 (g) + aotwoC (g) = 0 and carry = 0 then
final (g) := 0
carry := 0
elsif aosnum2 (g) + aotwoC (g) = 0 and carry = 1 then
final (g) := 1
carry := 0
elsif aosnum2 (g) + aotwoC (g) = 1 and carry = 0 then
final (g) := 1
carry := 0
elsif aosnum2 (g) + aotwoC (g) = 1 and carry = 1 then
final (g) := 0
carry := 1
elsif aosnum2 (g) + aotwoC (g) = 2 and carry = 0 then
final (g) := 0
carry := 1
elsif aosnum2 (g) + aotwoC (g) = 2 and carry = 1 then
final (g) := 1
carry := 1
end if
end for
%Put output
open : streamout, "subput.txt", put
for h : 1 .. 8
put : streamout, final (h) ..
end for
close : streamout
%Get it to one variable
open : streamin, "subput.txt", get
get : streamin, osubput : *
close : streamin
%Reassign the new TwosCompliment to aosnum2
for i : 1 .. 8
aosnum2 (i) := strint (osubput (i))
end for
end twosComplimentNum2
%Twos Compliment for finalAnswer
procedure twosComplimentFA
%%%%%%%%%%%%%%%%%%%%%%%%
var final : array 1 .. 8 of int
var fdigit : int := 0
var carry : int := 0
%%%%%%%%%%%%%%%%%%%%%%%%
for decreasing g : 8 .. 1
if g = 1 and finalAnswer (g) + aotwoC (g) = 0 and carry = 0 then
fdigit := 0
elsif g = 1 and finalAnswer (g) + aotwoC (g) = 1 and carry = 0 then
fdigit := 1
elsif g = 1 and finalAnswer (g) + aotwoC (g) = 1 and carry = 1 then
fdigit := 10
elsif g = 1 and finalAnswer (g) + aotwoC (g) = 2 and carry = 0 then
fdigit := 10
elsif g = 1 and finalAnswer (g) + aotwoC (g) = 2 and carry = 1 then
fdigit := 11
end if
%%%%%%%%%%%%%%%%%%%%%%%%
if finalAnswer (g) + aotwoC (g) = 0 and carry = 0 then
final (g) := 0
carry := 0
elsif finalAnswer (g) + aotwoC (g) = 0 and carry = 1 then
final (g) := 1
carry := 0
elsif finalAnswer (g) + aotwoC (g) = 1 and carry = 0 then
final (g) := 1
carry := 0
elsif finalAnswer (g) + aotwoC (g) = 1 and carry = 1 then
final (g) := 0
carry := 1
elsif finalAnswer (g) + aotwoC (g) = 2 and carry = 0 then
final (g) := 0
carry := 1
elsif finalAnswer (g) + aotwoC (g) = 2 and carry = 1 then
final (g) := 1
carry := 1
end if
end for
%Put output
open : streamout, "subput.txt", put
for h : 1 .. 8
put : streamout, final (h) ..
end for
close : streamout
%Get it to one variable
open : streamin, "subput.txt", get
get : streamin, osubput : *
close : streamin
%Reassign the new TwosCompliment to aosnum1
for i : 1 .. 8
finalAnswer (i) := strint (osubput (i))
end for
end twosComplimentFA
%Subtraction Procedure using Addition
procedure subtract
var final : array 1 .. 8 of int
var fdigit : int := 0
var carry : int := 0
%%%%%%%%%%%%%%%%%%%%%%%%
for decreasing j : 8 .. 1
if j = 1 and aosnum1 (j) + aosnum2 (j) = 0 and carry = 0 then
fdigit := 0
elsif j = 1 and aosnum1 (j) + aosnum2 (j) = 1 and carry = 0 then
fdigit := 1
elsif j = 1 and aosnum1 (j) + aosnum2 (j) = 1 and carry = 1 then
fdigit := 10
elsif j = 1 and aosnum1 (j) + aosnum2 (j) = 2 and carry = 0 then
fdigit := 10
elsif j = 1 and aosnum1 (j) + aosnum2 (j) = 2 and carry = 1 then
fdigit := 11
end if
%%%%
if aosnum1 (j) + aosnum2 (j) = 0 and carry = 0 then
final (j) := 0
carry := 0
elsif aosnum1 (j) + aosnum2 (j) = 0 and carry = 1 then
final (j) := 1
carry := 0
elsif aosnum1 (j) + aosnum2 (j) = 1 and carry = 0 then
final (j) := 1
carry := 0
elsif aosnum1 (j) + aosnum2 (j) = 1 and carry = 1 then
final (j) := 0
carry := 1
elsif aosnum1 (j) + aosnum2 (j) = 2 and carry = 0 then
final (j) := 0
carry := 1
elsif aosnum1 (j) + aosnum2 (j) = 2 and carry = 1 then
final (j) := 1
carry := 1
end if
end for
%Outputs data to text file
open : streamout, "subput.txt", put
for h : 1 .. 8
put : streamout, final (h) ..
end for
close : streamout
%Get it to one variable
open : streamin, "subput.txt", get
get : streamin, osubput : *
close : streamin
%Reassign the new TwosCompliment to finalAnswer
for i : 1 .. 8
finalAnswer (i) := strint (osubput (i))
end for
end subtract
%%%%%%%%%%%%%%%%%%%%%%%%%
%Beginning of Program
loop
%Set overflow to 0 for repeated attempts
oflow := 0
%Error trapping loops
loop
put "Is the first number negative? (y/n)"
get queryn1
if queryn1 = "Y" or queryn1 = "N" or queryn1 = "y" or
queryn1 = "yes" or queryn1 = "no" or queryn1 = "n" then
exit
else
put "Wrong input, this is a yes or no question.."
delay (400)
end if
end loop
%%%
loop
put "Please enter the first binary number.."
get snum1
lsnum1 := length (snum1)
if lsnum1 > 7 then
put "Only 7 bits can be inputed.."
else
exit
end if
end loop
%%
loop
put "Is the second number negative? (y/n)"
get queryn2
if queryn2 = "Y" or queryn2 = "N" or queryn2 = "y" or
queryn2 = "yes" or queryn2 = "no" or queryn2 = "n" then
exit
else
put "Wrong input, this is a yes or no question.."
delay (400)
end if
end loop
%%
loop
put "Please enter the binary number to subtract.."
get snum2
lsnum2 := length (snum2)
if lsnum2 > 7 then
put "Only 7 bits can be inputed.."
else
exit
end if
end loop
%Setting negative values for loops
if queryn1 = "y" or queryn1 = "yes" or queryn1 = "Yes" or queryn1 = "Y" then
neg1 := 1
end if
if queryn2 = "y" or queryn2 = "yes" or queryn2 = "Yes" or queryn2 = "Y" then
neg2 := 1
end if
%Variables
msnum1 := snum1
msnum2 := snum2
%%%%%%%%%
%Zero addition and array input for aosnum1
var difnum1 : int := 8 - lsnum1
for a : 1 .. difnum1
msnum1 := "0" + msnum1
end for
for b : 1 .. 8
aosnum1 (b) := strint (msnum1 (b))
end for
%%%%%%%%%%%%
%Zero addition and array input aosnum2
var difnum2 : int := 8 - lsnum2
for c : 1 .. difnum2
msnum2 := "0" + msnum2
end for
for d : 1 .. 8
aosnum2 (d) := strint (msnum2 (d))
end for
%%%%%%%%%%%%
%What to do if negatives occur
if neg1 = 1 then
onesComplimentNum1
twosComplimentNum1
end if
if neg2 = 0 then
onesComplimentNum2
twosComplimentNum2
end if
%Run Subtract Procedure
subtract
put ""
put ""
%Overflow error trapping
if aosnum1 (1) = 0 and aosnum2 (1) = 0 and finalAnswer (1) = 1 then
put "The subtraction resulted in an overflow..."
oflow := 1
elsif aosnum1 (1) = 1 and aosnum2 (1) = 1 and finalAnswer (1) = 0 then
put "The subtraction resulted in an overflow..."
oflow := 1
end if
%Ones and Twos compliment of output is negative
if finalAnswer (1) = 1 then
neg := 1
onesComplimentFA
twosComplimentFA
end if
%Proceed with answer only of overflow = 0
if oflow = 0 then
finalAnswerint := strint (osubput)
put "The subtraction of these two values produces.."
if neg = 1 then
put "-", finalAnswerint
elsif neg = 0 then
put finalAnswerint
end if
end if
put ""
put ""
%Another set of calculations?
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
else
exit
end if
end loop
%End Program
|