PROJECT 4 HINTS Class Roster Management System

advertisement
PROJECT 4 HINTS
Class Roster
Management System
COURSE ROSTER MANAGEMENT IN PROLOG
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 PROLOG
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  Bonus!
5. Display roster sorted by grade  Bonus!
6. Add a student to roster
7. Remove a student from roster
8. Exit
No Display Student Info option.
HOW DO WE 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.
x = func(a, b)  func(a, b, x)
• 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.
• How to do this in Prolog? See t1.pl.
• Need to have a ‘.’ after each input since read is expecting a Prolog term.
DEALING WITH INPUT
Example: read 2 items (id and grade) and return the list of two items just read.
See t2.pl.
read_student_info([A, B]) :write('\tStudent ID: '),
read(A),
write('\tStudent Name: '),
read(B).
LOADING FROM AND STORING 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.
tell(File),
write(X),
write('.'),  do not forget this!
told.
INPUT
There are multiple ways we could
provide input to Prolog. For example,
Student ID: “1”.
OR Student ID: ‘1’.
OR Student ID: 1.
Likewise with name and grade. So, to
ensure uniformity, your program must
work with exactly the same input as
provided in the sample execution.
Class roster management system
==============================
MENU
==============================
0. Reset roster
1. Load roster from file
2. Store roster to file
3. Display roster sorted by ID
4. Add student to roster
5. Remove student from roster
6. Exit
Enter your choice (followed by a ’.’): 4.
Add a student to the class roster.
Student ID: 1.
Student Name: "Michael Jordan".
Grade: 90.
DISPLAYING A RECORD
Note the structure of a single record in our roster:
[1, “Michael Jordan”, 90]
To display this record, we should display each
element separately with an appropriate writing
function.
display_record([ID, Name, Grade]) :write(ID), nl,
format(“~s”, [Name]), nl,
write(Grade).
Output of display_record([1, “Michael”, 90]):
1
Michael
90
REMOVING AND SORTING
For removing a student, consider the takeout example from the notes. But instead of a
list of items, we have a list of lists now.
For sorting a roster, consider the naïve_sort or implement your own sorting predicates.
Any built-in sorting functionality which you may find online is not allowed. You must
implement the sorting yourself.
ONE LAST TIP
Do NOT write the whole program in one shot!!!!
Write one function at a time. Test before considering the next function.
My solution (minus bonus features) has 106 lines of Prolog code.
Your program must be started the following way:
?- [‘roster.pl’].
?- menu([]).
If you get a warning about singleton variables, it means you’ve created a variable
that is never used again. Consider using the anonymous variable _ at those times.
Download