PROJECT 3 HINTS Class Roster Management System

advertisement
PROJECT 3 HINTS
Class Roster
Management System
COURSE ROSTER MANAGEMENT IN SCHEME
• A class roster is list of student entries of the form (ID, name, grade)
Examples:
(“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 (BONUS)
5. Display roster sorted by grade (BONUS)
6. Display student info
7. Add a student to roster
8. Remove a student from roster
9. Exit
HOW DO WE MAINTAIN THE ROSTER?
There is no storage (i.e. no variables) in Scheme.
The roster has either to be an argument to a function or a return value.
The function to print a menu needs to have an argument for the roster.
• (menu roster)
MENU SYSTEM
1. Display the menu.
2. Read input from user.
3. Perform some operation on the roster and continue.
How do we do this in scheme? See t1.scm.
DEALING WITH INPUT
Read 2 items (id and grade) and return the list of just two items just read?
(define read-2-items
(lambda ()
(begin
(display “input id and grade: “)
(list (read) (read))
)
)
Let’s say we want to append this item to the roster, what to do? See t2.scm
LOAD FROM FILE AND STORE TO FILE
• Straight-forward
• (write object file) to write the whole roster to a file.
• (read file) to read the whole roster from a file.
• Implement store first and then test with load.
• Don’t forget to close your file after you write to it!
SORTING
At the very least, you need to sort the roster by ID.
• An easy solution: Selection Sort! Find the record with the smallest ID in the unsorted
sublist, exchanging it with the leftmost unsorted element (putting it in sorted order),
and moving the sublist boundaries one element to the right.
• One implementation requires the following functions to be defined:
• (select-sort roster)
; Sort roster by id in ascending order.
• (smallest roster record)
; Return the record with the smallest id in roster.
• (remove roster record)
; Remove the record from roster.
SMALLEST
Here’s a sample implementation of smallest for a list of integers (you will have
a list of lists!).
(define smallest
(lambda (lst min)
(cond ((null? lst) min)
; List is null, return current smallest element
((< (car lst) min) (smallest (cdr lst) (car lst))) ; Compare smallest with head of list
(else (smallest (cdr lst) min))
)
)
)
Note: you may have to perform some casting. Scheme provides
Sample call:
(smallest mylist (car mylist))
many casting functions like (string->number x) which returns
the integer representation of the string x.
REMOVE
Here’s a sample implementation of remove for a list of integers.
(define remove
(lambda (lst item)
(cond ((null? lst) '() )
((equal? (car lst) item) (cdr lst))
(else (cons (car lst) (remove (cdr lst) item)))
)
)
)
Sample call: (remove ‘(1 2 3 4 5 6) 5)
SELECTION SORT
Once you have smallest and remove implemented, implementing select-sort is as
simple as adding the smallest element to a list returned by a recursive call to selectsort with a list that has that smallest element removed.
ONE LAST TIP…
Do NOT write the whole program in one shot!!!!
 One function at a time. Test before considering the next function.
 You do not have the same debugging capabilities available to you as you do in C/C++.
 That said, my implementation (minus the bonus parts) has ~230 lines of code and took
about 3 hours total …and I didn’t have the same hints that you do ;)
Download