Important remarks on coursework programming 1. Put in comments where necessary in your program source. 2. Use meaningful variable names. 3. Put in indentations. 4. Use break points and watches to debug your program. 5. The default path of QP is in the QP folder. It can be changed in the QP option, environment. Therefore, you should copy your data files from your disk onto the QP directory when you try to run the program. Otherwise, “Run time error” will be resulted when assigning the file. 6. The default setting of our computer system is to hide up the file extension (隱藏 副檔名). If you look at a file named “data.txt”, it may actually be “data.txt.txt”. Therefore, you should set in “My computer” to show the file extension. Every time when you log on, you must do this step. 7. Due to limitation of DOS, the memory size of QP is very limited. The total data size is equal to 640KB. A string will take up 256B. Therefore, an array of string of 256 elements inside, i.e. array[1..2560] of string, will take up all the memory space available. If you take up too much space, “Run time error” will be resulted. 8. Do remember to initialize your arrays or variables before using them. Say: For n := 1 to 100 do If table[n] = inputchar then count[n] := count[n] + 1; In the above, the array “count” must be initialized before it can be used. 9. Be careful about not exceeding the limit of array or variables, say: Var a: array [1..200] of string[2]; s : string; b : array[1..100] of integer; Begin For n:= 1 to 200 do Begin a[n] := ‘ ‘; {this is okay} b[n] := 0; {this line can incur a run-time error, since it only has 100 elements inside} end; a[1] := s; {this line also has an error, because a has only 2 char inside each element, but s has 256 characters} Or if you have the array limit of say 50 lines, and you want to read in each line from the data file into the array, then you must avoid making a data file of more than 50 lines. 10. Validation and verification Data validation and verification refers to exception handling in programming. In which when users input an undesirable value, the program can check out and recorrect the error by reasking user to input again. Points to remember: 1. User inputs must be in strings, never be in integers or in reals. This could avoid run-time error caused by wrong user inputs. 2. Instead of directly reading / openning the file name provided by users. The file names can be checked by first reading all the file names from disk. Then check if the input file name is inside. If it is not, then prompt (提示) user. Some built-in functions that you can use: Getdir – get current directory (folder) Chdir – change directory Mkdir – make (create) directory Rmdir – remove (delete) directory Erase – erase (delete) a file Rename – rename a file or directory Findfirst – run this function the first time you want to find a file, then Findnext – after findfirst, the next time you want to find a file (for findfirst and findnext, you must use a variable of type SearchRec, then it will return in SearchRec.name… Refer to the demo program ob below) ) Refer to the next page for more details. You can find this out in QP help. {It will demonstrate how to create a folder, and check if it doesn’t exist, then create it and change to that new folder. Then create a file inside the folder. Write a line inside the file. Then rename the file. List out the content of the new folder. Finally, delete the new file and the new folder just created.} PROGRAM direct; { DIRECT.PAS illustrates the following directory and file handling routines: ChDir FindFirst Erase FindNext GetDir MkDir Rename RmDir and the following standard variable: DosError } USES Dos; CONST program_name = 'DIRECT '; program_desc = 'demonstrates various directory routines.'; drive = 0; { current drive } VAR current_dir : STRING; new_dir : STRING; sresults : SearchRec; new_file : TEXT; new_name : STRING; BEGIN Writeln( program_name, program_desc ); Writeln; { Get the current working directory. } GetDir( drive, current_dir ); REPEAT { Prompt user for name for test directory. } Write( 'Enter name for new subdirectory of ', current_dir, ' :'); Readln( new_dir ); { Make sure it doesn't already exist. } FindFirst( new_dir, Directory, sresults ); IF (DosError = 0) THEN Writeln ('A subdirectory by that name already exists.'); UNTIL (DosError = 18); { 18 means no more matches } { Try to create a new directory, and if successful, change to it. } MkDir( new_dir ); ChDir( new_dir ); { Create a file in this directory. } Writeln( 'Creating new file test.doc in directory ', new_dir ); Assign( new_file, 'test.doc' ); Rewrite( new_file ); Writeln( new_file, 'This file was written by DIRECT.EXE.' ); Close( new_file ); Write( 'Enter new name for test.doc: '); Readln( new_name ); Rename( new_file, new_name ); { List the contents of this new subdirectory. } Writeln( 'Contents of subdirectory ', new_dir, ':' ); Writeln; FindFirst( '*.*', Directory, sresults ); WHILE (DosError = 0) DO BEGIN Writeln( sresults.Name ); FindNext( sresults ); END; { Delete new file. } {*.* means all the files} Erase( new_file ); { Return to previous directory and delete new one. } ChDir( current_dir ); Writeln( 'Deleting new subdirectory.' ); RmDir( new_dir ); End.