Course roster management in Prolog • Data to be managed – Example

advertisement
Course roster management in Prolog
• Data to be managed
– A class roster is list of student entries of the form
[ID, name, grade]
– Example
[“001”, “John Smith”, 59]
[“010”, “Amy M. Sosa”, 100]
[“500”, “Mike Harris”, 80]
• In scheme, this data structure will be represented as a
list:
[[“001” “John Smith” 59], [“010” “Amy M. Sosa” 100], [“500” “Mike Harris” 80]]
Course roster management in scheme
• The roster management system supports the
following functions (in the menu):
0. Reset roster
1. Load roster from file
2. Store roster to file
3. Display roster sorted by ID
4. Display roster sorted by name
5. Display roster sorted by grade
6. Display student info
7. Add a student to roster
8. Remove a student from roster
9. Exit
How to maintain the roster?
• Similar to Scheme, variables in Prolog have limited use
(but better than Scheme).
– Critical data structure is still passing from one function to
another.
– Functions in Prolog do not return anything, everything is in
the function arguments.
– The function to print a menu needs to have an argument
for the roster.
• Menu (roster).
Menu system
• Display the menu
• Read input from user
– can at least remember the input in Prolog.
• Perform some operation on the roster and
continue.
• Do this in Prolog? See t1.pl.txt
– Need to have a ‘.’ after each input.
– Use “…” for multiple word strings.
Dealing with input
• Read 2 items (id and grade) and return the list
of just two items just read?
read_student_info([A, B]) :write('\tStudent ID: '),
read(A),
write('\tStudent Name: '),
read(B).
See t2.pl.txt
Load from file and store to file
• Remember to close the file when it is done.
– Like Scheme one write(X) writes the whole data
structure, and one read(X) reads the whole data
structure out.
– Need to put a ‘.’ after the write(X) in order for
this to work correctly.
telling(Old),
tell(File),
write(X),
write('.'),
told,
tell(Old),
Order of software development
• Do NOT write the whole
program in one shot!!!!
– One function at a time. Test
before considering the next
function.
Download