HELP-phonebook in turing
Author |
Message |
helloolleh
|
Posted: Mon May 25, 2009 8:01 am Post subject: HELP-phonebook in turing |
|
|
What is it you are trying to achieve?
I am trying out a new code in turing. Can somebody please tell me how to:
1. Initialize the Phone Book
2. View Phone Book content
3. Sort in ascending order by last name
4. Search by last name
5. Exit the program
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>
Turing: |
<Add your code here>
import GUI
%Function that turns any alphabet to its lowercase
function lowercase (var input : string) : string
var a,b : int
for i : 1 .. length (input )
if ord (input (i )) < 97 and ord (input (i )) >= 65 then
a := (ord (input (i ))) + 32
input := input (1 .. (i - 1)) + chr (a ) + input (i + 1 .. *)
end if
end for
result input
end lowercase
var name, phone, filename, line, id, look, replace, boxline : string := ""
var choice : string (1) := ""
var stream, count, add, search, edit, getout, wind, pname, pnumber, font1, box : int := 0
font1 := Font.New ("serif:8") %Font, for writing minute things
procedure guiname (text : string)
GUI.SetSelection (pname, 0, 0)
GUI.SetActive (pname )
name := GUI.GetText (pname )
if phone = "" then
GUI.SetActive (pnumber )
end if
end guiname
% Text Field procedure for the Contact Number
procedure guinumber (text : string)
GUI.SetSelection (pnumber, 0, 0)
GUI.SetActive (pnumber )
phone := GUI.GetText (pnumber )
if name = "" then
GUI.SetActive (pname )
end if
end guinumber
% Text Field procedure for the Search Keyword
procedure guilook (text : string)
GUI.SetSelection (pname, 0, 0)
GUI.SetActive (pname )
look := GUI.GetText (pname )
if replace = "" then
GUI.SetActive (pnumber )
end if
end guilook
% Text Field procedure for the Replacing a number in a contact
procedure guireplace (text : string)
GUI.SetSelection (pnumber, 0, 0)
GUI.SetActive (pnumber )
replace := GUI.GetText (pnumber )
if look = "" then
GUI.SetActive (pname )
end if
end guireplace
/* Editing Procedure:- Takes a filename, search keyword and replacing text respectively. Then looks for the keyword
in the file and changes the line after the keyword with replace*/
procedure change (var filename, look, replace : string)
var stream, counter : int := 0
open : stream, filename, get, mod, put, seek
var sentence, message : string := ""
look := lowercase (look )
loop
counter := counter + 1
exit when eof (stream )
get : stream, sentence : *
exit when sentence = look or eof (stream )
end loop
if sentence not= look then
message := "No matches found for " + look + "."
locate (1, 1)
put message
delay (1000)
else
message := " "
end if
close : stream
if message = " " then
open : stream, filename, get, mod, put, seek
for i : 1 .. counter
get : stream, sentence : *
end for
put : stream, replace
close : stream
put "Change was successfully made."
delay (1000)
end if
end change
/* Id is set to Yes to make sure the file is closed after it is opened. If the file doesn't exist, then the id is changed
to No*/
id := "yes"
wind := Window.Open ("Phone Book") %opens a window
Window.Set (1, "title:Phone Book")
/*Procedcures for the choices in the Main menu in the order that they appear when run*/
procedure one
GUI.Dispose (add )
GUI.Dispose (search )
GUI.Dispose (edit )
GUI.Dispose (getout )
choice := "1"
end one
procedure two
GUI.Dispose (add )
GUI.Dispose (search )
GUI.Dispose (edit )
GUI.Dispose (getout )
choice := "2"
end two
procedure three
GUI.Dispose (add )
GUI.Dispose (search )
GUI.Dispose (edit )
GUI.Dispose (getout )
choice := "3"
end three
procedure four
GUI.Dispose (add )
GUI.Dispose (search )
GUI.Dispose (edit )
GUI.Dispose (getout )
choice := "4"
end four
%Procedure for creating the buttons.
procedure buttons
add := GUI.CreateButtonFull (10, 110, 20, "Add Contact", one, 20, "1", true)
search := GUI.CreateButtonFull (10, 80, 20, "Search Contact", two, 20, "2", false)
edit := GUI.CreateButtonFull (10, 50, 20, "Edit Contact", three, 20, "3", false)
getout := GUI.CreateButtonFull (10, 20, 20, "Exit", four, 20, "4", false)
%Makes Sure the buttons are disabled the window is not active; prevents an opening of a new window
loop
exit when GUI.ProcessEvent or choice not= ""
loop
exit when GUI.ProcessEvent or Window.GetActive not= 1 or choice not= ""
end loop
loop
exit when Window.GetActive = 1 or choice not= ""
end loop
exit when GUI.ProcessEvent or choice not= ""
end loop
end buttons
% Program Start here
loop
choice := ""
%Setting screen to graphics mode to enable buttons and other GUI
View.Set ("graphics:150;140")
Text.Cls
GUI.SetBackgroundColor (8)
buttons
%ADD CONTACT
if choice = "1" then
Window.SetPosition (1, 360, 250)
name := ""
phone := ""
id := "yes"
View.Set ("graphics:260;140")
GUI.SetBackgroundColor (8)
colorback (8)
pname := GUI.CreateTextField (110, 100, 100, "", guiname )
pnumber := GUI.CreateTextField (110, 50, 100, "", guinumber )
var nameLabel := GUI.CreateLabelFull (100, 100, "Name", 0, 0,
GUI.RIGHT, 0)
var phoneLabel := GUI.CreateLabelFull (100, 50, "Phone No.", 0, 0,
GUI.RIGHT, 0)
Font.Draw ("Press Enter after each entry", 70, 10, font1, black)
loop
exit when GUI.ProcessEvent or (name not= "" and phone not= "")
end loop
GUI.Dispose (pname )
GUI.Dispose (pnumber )
GUI.Dispose (nameLabel )
GUI.Dispose (phoneLabel )
if name = "quit" then
return
end if
name := lowercase (name )
if phone = "quit" then
return
end if
filename := name (1) + ".txt"
open : stream, filename, get, seek, mod, put
seek : stream, *
put : stream, name
put : stream, phone
put : stream, " "
cls
put "Contact Stored. Press any key to continue..." ..
getch (choice )
if id = "yes" then
close : stream
end if
% Search Contact
elsif choice = "2" then
Window.SetPosition (1, 360, 250)
id := "yes"
View.Set ("graphics:260;140")
GUI.SetBackgroundColor (8)
colorback (8)
phone := " "
name := ""
pname := GUI.CreateTextField (100, 70, 100, "First word/letter(s) of Contact Name", guiname )
var nameLabel := GUI.CreateLabelFull (90, 70, "Name", 0, 0,
GUI.RIGHT, 0)
loop
exit when GUI.ProcessEvent or name not= ""
end loop
GUI.Dispose (pname )
GUI.Dispose (nameLabel )
View.Set ("graphics:320;220")
GUI.SetBackgroundColor (8)
box := GUI.CreateTextBox (10, 10, 300, 180)
if name = "quit" then
return
end if
name := lowercase (name )
filename := name (1) + ".txt"
open : stream, filename, get
if stream < 1 then
id := "no"
boxline := "No more matches. Press any key to continue..."
GUI.AddLine (box, boxline )
else
loop
count := 0
loop
exit when eof (stream ) = true
get : stream, line : *
count := count + index (line, name )
exit when eof (stream ) = true or count > 0
end loop
% Data is sent to a textbox, where the user can scroll through the results
if count > 0 then
if length (line ) > 2 then
boxline := "Contact Name: " + line
GUI.AddLine (box, boxline )
exit when eof (stream ) = true
get : stream, line : *
boxline := "Contact Phone: " + line
GUI.AddLine (box, boxline )
boxline := " "
GUI.AddLine (box, boxline )
else
boxline := line
GUI.AddLine (box, boxline )
end if
else
boxline := "No more matches. Press any key to continue..."
GUI.AddLine (box, boxline )
end if
exit when eof (stream ) = true
end loop
end if
loop
exit when GUI.ProcessEvent or hasch
end loop
GUI.Dispose (box )
cls
choice := ""
if id = "yes" then
close : stream
end if
%EDIT CONTACT
elsif choice = "3" then
Window.SetPosition (1, 360, 250)
look := ""
replace := ""
View.Set ("graphics:260;140")
GUI.SetBackgroundColor (8)
colorback (8)
pname := GUI.CreateTextField (110, 100, 100, "Exact Contact Name", guilook )
pnumber := GUI.CreateTextField (110, 50, 100, "Number to replace", guireplace )
var nameLabel := GUI.CreateLabelFull (100, 100, "Name", 0, 0,
GUI.RIGHT, 0)
var phoneLabel := GUI.CreateLabelFull (100, 50, "Phone No.", 0, 0,
GUI.RIGHT, 0)
Font.Draw ("Press Enter after each entry", 70, 10, font1, black)
loop
exit when GUI.ProcessEvent or (look not= "" and replace not= "")
end loop
look := lowercase (look )
filename := look (1) + ".txt"
change (filename, look, replace )
GUI.Dispose (pname )
GUI.Dispose (pnumber )
GUI.Dispose (nameLabel )
GUI.Dispose (phoneLabel )
%EXIT
else
exit
end if
end loop
|
Please specify what version of Turing you are using
<Answer Here> |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Tallguy
|
Posted: Mon May 25, 2009 8:21 am Post subject: RE:HELP-phonebook in turing |
|
|
two words "data files"
your life would be so much easier if you to create a atafile then call on the data stored within with a sorting procedure
i just did a phone book program for my compsci class, just don't over think it |
|
|
|
|
|
CodeMonkey2000
|
Posted: Mon May 25, 2009 8:25 am Post subject: RE:HELP-phonebook in turing |
|
|
1. Initialize the Phone Book
I'm not sure what you mean.
2. View Phone Book content
Just open all the text files you made, and get all the numbers and name.
3. Sort in ascending order by last name
This one is a bit tricky. In each folder you need to get all information, and store it in an array. Then you use the bubble sort (it's the easiest to implement). Just google the bubble sort. Now you resave the list.
I have to go now, I'll elaborate later. |
|
|
|
|
|
Tallguy
|
Posted: Mon May 25, 2009 8:31 am Post subject: Re: HELP-phonebook in turing |
|
|
for sorting use a bubble sort as stated before, its like this
Turing: |
for last : 1 .. numger of entries
for i : 1 .. numger of entries - 1
if entry (i) < entry (i + 1) then
random := false
const temp := entry (i)
entry (i) := entry (i + 1) %swamp the two variables
entry (i + 1) := temp
end if
end for
exit when random
end for
|
|
|
|
|
|
|
|
|