program looping with procedures.
Author |
Message |
sriyegna
|
Posted: Tue Mar 02, 2010 6:43 am Post subject: program looping with procedures. |
|
|
What is it you are trying to achieve?
I am trying to create a program in which i can convert hexadecimal to binary/decimal as well as loop the program to restart from the beggining if they choose a procedure.
What is the problem you are having?
I currently have a program with procedures and a menu and i am trying to create a button in the menu (which i know how to do) that will clear the screen of all the text but wiill keep the image as well as the buttons and the menu! I would also like to know how to convert hexadecimal to decimal or binary
Describe what you have tried to solve this problem
To solve this, i have tried using loops myself as well as messing around with hexadecimal and using the cls command. None seem to work. Sorry, i have just started this in my grade 10 course so i am not the best coder
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Below is the code of my program, it can convert Binary to Hexadecimal, Binary to Decimal, Decimal to Binary and Decimal to Hexadecimal
Turing: |
import GUI in "%oot/lib/GUI"
View.Set ("graphics:640;480,nobuttonbar")
const numofmenu := 8
% Number of menus and sub menus
var hexadecimal : string (4) := ""
var hexnum : int
var menu1, menu2, num, decimal, pic, font, divider : int
% The menus (eg, conversions)
% The num is the length of characters
% The decimal is the value of the input for D to B and output from B to D
var menuu : array 1 .. numofmenu of int
% The buttons inside the menu.
var binary, c : string
% The Binary is the input for the binary value as well as output from decimal
divider := 128
% Gives the variable divider a value of 128
pic := Pic.FileNew ("bin2dec.bmp")
% brings the pic from the T files directory
Pic.Draw (pic, 0, maxy - maxy, picCopy)
% puts the picture with maxy and maxx
procedure B2D
% Procedure to do the Binary to Decimal task
locate (3, 1)
% start on line 3 at the beggining
color (12)
% makes the text green
decimal := 0
% Declares decimal with a value of 0
put "Please enter the binary number you would like to convert to decimal:"
% A statement that asks for the binary number to convert it into decimal
get binary
% Will store the input in the binary variable
num := length (binary )
%Stores the number of characters in the binary variable in num
for t : 1 .. num
% Begin For Statement
if binary (t ) = "1" then
% if statement that says if binary is 1, then move to the next line
decimal := decimal + 2 ** (num - t )
% this gets the binary number by ading 2 to decimal and exponenting by the length of the decimal number - the loop
end if
% End the if statement
end for
% End the for statemtn
put "The binary number is: ", decimal, "."
locate (maxrow, 1)
%Begins on the very bottom row
put "Converted Binary To Decimal..." ..
end B2D
%Ends procedure B2D
procedure D2B
% Procedure to do the Decimal to Binary task
locate (3, 1)
% Starts at line 3 at the beginning
color (9)
% Makes the colour blue
put "Please enter a decimal number:"
get decimal
% Gets and stores the value for decimal
binary := intstr (decimal, 0, 2)
% converts the integer to a string
put "The binary number for ", decimal, " is: ", binary, "."
locate (maxrow, 1)
% Starts at the very bottom at the beginning
put "Converted Decimal To Binary..." ..
end D2B
% Ends the procedure D2B
procedure D2H
% Procedure to do the Decimal to Binary task
locate (3, 1)
% Starts at line 3 at the beginning
color (9)
% Makes the colour blue
put "Please enter a decimal number:"
get decimal
% Gets and stores the value for decimal
hexadecimal := intstr (decimal, 0, 16)
% converts the integer to a string
put "The Hexadecimal value for ", decimal, " is: ", hexadecimal, "."
locate (maxrow, 1)
% Starts at the very bottom at the beginning
put "Converted Decimal To Hexadecimal..." ..
end D2H
procedure B2H
% Procedure to do the Binary to Decimal task
locate (3, 1)
% start on line 3 at the beggining
color (12)
% makes the text green
decimal := 0
% Declares decimal with a value of 0
put "Please enter the binary number you would like to convert to Hexadecimal:"
% A statement that asks for the binary number to convert it into decimal
get binary
% Will store the input in the binary variable
num := length (binary )
%Stores the number of characters in the binary variable in num
for t : 1 .. num
% Begin For Statement
if binary (t ) = "1" then
% if statement that says if binary is 1, then move to the next line
decimal := decimal + 2 ** (num - t )
% this gets the binary number by ading 2 to decimal and exponenting by the length of the decimal number - the loop
end if
% End the if statement
end for
% End the for statemtn
hexadecimal := intstr (decimal, 0, 16)
% converts the integer to a string
put "The Hexadecimal value for ", binary, " is: ", hexadecimal, "."
locate (maxrow, 1)
%Begins on the very bottom row
put "Converted Binary To Decimal..." ..
end B2H
procedure blank
end blank
var button1 : int := GUI.CreateButton (20, 285, 300, "Binary To Decimal", B2D )
% Creates a button that goes to the binary to decimal conversion procedure
var button3 : int := GUI.CreateButton (320, 285, 300, "Binary To Hexadecimal", B2H )
% Creates a button that goes to the binary to decimal conversion procedure
var button2 : int := GUI.CreateButton (20, 140, 300, "Decimal To Binary", D2B )
% Creates a button that goes tot he decimal to binary conversion procedure
var button4 : int := GUI.CreateButton (320, 140, 300, "Decimal To Hexadecimal", D2H )
% Creates a button that goes tot he decimal to binary conversion procedure
% Makes the menus
menu1 := GUI.CreateMenu ("File")
%Makes the menu File
menuu (1) := GUI.CreateMenuItem ("Quit", GUI.Quit)
% Makes the submenu quit to quit the program
menu2 := GUI.CreateMenu ("Conversions")
%Makes the second menu Conversions
menuu (3) := GUI.CreateMenuItem ("Binary To Decimal", B2D )
menuu (4) := GUI.CreateMenuItem ("Binary To Hexadecimal", B2H )
menuu (5) := GUI.CreateMenuItem ("---", blank )
% Makes the submenu of the second menu Binary to Decimal
menuu (6) := GUI.CreateMenuItem ("Decimal To Binary", D2B )
% makes the second submenu of the second menu Decimal to Binary
menuu (7) := GUI.CreateMenuItem ("Decimal To Hexadecimal", D2H )
% Begins the Loop
loop
exit when GUI.ProcessEvent
%Exits the loop once it has completed the process
end loop
% Ends the loops
% code that hides the window once the quit button has been pressed
Window.Hide (defWinID )
|
Please specify what version of Turing you are using
4.1 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Turing_Gamer
|
Posted: Tue Mar 02, 2010 8:27 am Post subject: RE:program looping with procedures. |
|
|
I would work it like this:
- Have an input key down with some delay when pressed...
- Have a counter for each number inputed and exit when enter.
- Calculate each number accordingly (For hexidecimal, have 'if # is 10 then # = A', etc.) |
|
|
|
|
|
sriyegna
|
Posted: Tue Mar 02, 2010 4:11 pm Post subject: RE:program looping with procedures. |
|
|
ah yes, if statements. Il give that a shot right now |
|
|
|
|
|
sriyegna
|
Posted: Wed Mar 03, 2010 7:08 am Post subject: RE:program looping with procedures. |
|
|
well, thanks! i managed to use if statements to create a hex to decimal convertor and just used the decimal to convert it into binary
As for the clear screen sort of thing. im still not sure as to what to do |
|
|
|
|
|
Turing_Gamer
|
Posted: Wed Mar 03, 2010 8:28 am Post subject: Re: program looping with procedures. |
|
|
Turing: | loop
cls
%Drawing code
View.Update
%Movement code (in games)
%Changing variable code (speed, etc.)
end loop |
Was it really that hard? |
|
|
|
|
|
sriyegna
|
Posted: Fri Mar 05, 2010 9:22 am Post subject: RE:program looping with procedures. |
|
|
lol yea kinda... This is the first thing im actually writing, havent gotten into the GUI part but thought id try and build this for a bonus. Sorry |
|
|
|
|
|
Turing_Gamer
|
Posted: Fri Mar 05, 2010 12:33 pm Post subject: Re: program looping with procedures. |
|
|
Lol it is ok. Havn't used GUI either. |
|
|
|
|
|
SNIPERDUDE
|
Posted: Sat Mar 13, 2010 10:49 pm Post subject: RE:program looping with procedures. |
|
|
The built-in GUI in Turing is quite messy, ugly, and bothersome. Worry about GUI later, try making it text based just to figure out the conversions first. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
|
|