REM name address & phone no. database by simon laszcz ' CLEAR OPTION BASE 1 DIM table$(50,4) ' HIDEM GOSUB loadfile GOSUB menu WHILE mchoice%<>6 ON mchoice% GOSUB addnum,search,delnum,listing,sort GOSUB menu WEND GOSUB savefile SHOWM END ' PROCEDURE loadfile LOCAL name$,address$,std$,phone$ ' IF EXIST("A:\NATND.FLE")=TRUE THEN OPEN "r",#1,"A:\NATND.FLE",95 FIELD #1,20 AS name$,50 AS address$,10 AS std$,15 AS phone$ WHILE NOT EOF(#1) INC counter% GET #1,counter% ' table$(counter%,1)=name$ table$(counter%,2)=address$ table$(counter%,3)=std$ table$(counter%,4)=phone$ WEND CLOSE #1 ENDIF RETURN ' PROCEDURE savefile LOCAL name$,address$,std$,phone$,sub%,savef$ ' REPEAT INPUT "Save changes? (y/n) ",savef$ UNTIL UPPER$(savef$)="Y" OR UPPER$(savef$)="N" ' IF UPPER$(savef$)="Y" THEN IF EXIST("A:\NATND.FLE") THEN KILL "A:\NATND.FLE" ENDIF ' OPEN "r",#1,"A:\NATND.FLE",95 FIELD #1,20 AS name$,50 AS address$,10 AS std$,15 AS phone$ FOR sub%=1 TO counter% IF table$(sub%,1)<>SPACE$(20) THEN name$=table$(sub%,1) address$=table$(sub%,2) std$=table$(sub%,3) phone$=table$(sub%,4) ' PUT #1 ENDIF NEXT sub% CLOSE #1 ENDIF RETURN ' PROCEDURE menu REPEAT PRINT PRINT "Name, address and telephone number database" PRINT PRINT "1. Add a number" PRINT "2. Search" PRINT "3. Delete a number" PRINT "4. Listing" PRINT "5. Sort" PRINT "6. Quit" PRINT INPUT "Choice: ",mchoice% UNTIL mchoice%>0 AND mchoice%<7 RETURN ' PROCEDURE addnum LOCAL name$,address$,std$,phone$ ' IF counter%<50 THEN INC counter% ' PRINT PRINT "Name : " FORM INPUT 20 AS name$ PRINT "Address : " FORM INPUT 50 AS address$ PRINT "STD Code : " FORM INPUT 10 AS std$ PRINT "Phone Number : " FORM INPUT 15 AS phone$ ' table$(counter%,1)=SPACE$(20) table$(counter%,2)=SPACE$(50) table$(counter%,3)=SPACE$(10) table$(counter%,4)=SPACE$(15) ' LSET table$(counter%,1)=name$ LSET table$(counter%,2)=address$ LSET table$(counter%,3)=std$ LSET table$(counter%,4)=phone$ ENDIF RETURN ' PROCEDURE search LOCAL name$,sub% ' GOSUB getname IF name$<>SPACE$(20) THEN FOR sub%=1 TO counter% IF table$(sub%,1)=name$ THEN PRINT SPC(5)'table$(sub%,2) PRINT SPC(5)'table$(sub%,3)'table$(sub%,4) ENDIF NEXT sub% ENDIF RETURN ' PROCEDURE getname LOCAL temp$ ' PRINT PRINT "Name : " FORM INPUT 20 AS temp$ name$=SPACE$(20) LSET name$=temp$ RETURN ' PROCEDURE delnum LOCAL name$,sub% ' GOSUB getname IF name$<>SPACE$(20) THEN FOR sub%=1 TO counter% IF table$(sub%,1)=name$ THEN table$(sub%,1)=SPACE$(20) table$(sub%,2)=SPACE$(50) table$(sub%,3)=SPACE$(10) table$(sub%,4)=SPACE$(15) ENDIF NEXT sub% ENDIF RETURN ' PROCEDURE listing LOCAL sub% ' PRINT FOR sub%=1 TO counter% IF table$(sub%,1)<>SPACE$(20) THEN PRINT table$(sub%,1) PRINT SPC(5)'table$(sub%,2) PRINT SPC(5)'table$(sub%,3)'table$(sub%,4) ENDIF NEXT sub% RETURN ' PROCEDURE sort LOCAL sub%,finish!,count% finish!=FALSE count%=1 ' WHILE finish!=FALSE finish!=TRUE FOR sub%=1 TO counter%-count% IF table$(sub%,1)<table$(sub%+1,1) SWAP table$(sub%,1),table$(sub%+1,1) SWAP table$(sub%,2),table$(sub%+1,2) SWAP table$(sub%,3),table$(sub%+1,3) SWAP table$(sub%,4),table$(sub%+1,4) finish!=FALSE ENDIF NEXT sub% INC count% WEND RETURN |
After clearing the memory a 50 by 4 array is declared whose subscripting is forced to start at 1. The array will hold 50 records, each record containing a name, address, dialing code and 'phone number. The mouse pointer is hidden before an attempt is made to read a pre-existing data file. A menu is then displayed and so long as the user doesn't decide to quit, the appropriate procedure performed using the ON mchoice% GOSUB ... statement. The menu is re-displayed after each sub-routine. Before the program terminates the user is given the option of saving the data. The mouse pointer is then made visible. loadfile firsts tests for the existence of the data file. If found, it opens it in random access mode, and declares that the record is 95 characters long. The record fields are then defined using the FIELD statement. Each field is a string variable. The sum of their lengths must equal the length declared with the OPEN statement. The random access file is actually a relative file where records are numbered sequentially from 1 upto a possible 65535. While the record pointer is not at the end of file, the next record is retrieved. With each read counter% (the relative record number) is incremented. counter% is used to subscript the table$ array to which the fields are assigned. savefile first asks the user whether they want the data saved. If so it deletes the data file if it pre-exists. This ensures that deleted records are removed from the file. The file is opened in random access mode, the record fields declared and the records written. Blank array records are ignorred. Note that when records are written, field lengths must be as declared. In this program this is ensured in addnum. The file is then closed. Note that if not explicitly closed, files still open at the end of a program will be closed by END. addnum accepts contact data into local variables. The length of input is limited by FORM INPUT n. Array elements are expanded to their full sizes (as defined in the FIELD statement) by assigning them with the appropriate number of spaces. The contents of the local variables is then assigned to the array using LSET, which left justifies the data within the receiving variable and maintains its length. addnum maintains counter% which is a count of the number of records in the table$ array. search first obtains a name to search for by calling the getname sub-routine. As long as the name entered is not blank, the table$ array is searched. If a match is found the address and 'phone number are displayed 6 spaces from the left of the screen. getname accepts a name of upto 20 characters into a temporary variable. It then assign 20 spaces to the global variable name$ and assigns (with left justification within the variable) the temporary variable to it. Trailing spaces must be maintained in name$ so that a match may be made with the name stored in the table$ array. delnum works in a similar way to search, except that when a match is found the data is initialized by assigning the appropriate number of spaces to the array elements. listing reads through the array upto counter% and prints each record. sort implements a bubble sort which sorts the array into reverse order (Z-A) so that empty records are moved to the end of the array. It uses the boolean flag finish! to control when sorting stops. Array records found to be out of sequence are exchanged using the SWAP command. |