COP4020 Programming Assignment 3 Class Roster Management System in Prolog

advertisement
COP4020 Programming Assignment 3
Class Roster Management System in Prolog
Due April 26, 2015
Objective
Gain experience with logic programming in Prolog.
Summary
In this assignment, we will write a class roster management system that provides basic
functions for entering, removing, and displaying student information in a course roster using
the Prolog language. The course roster is stored as a table of student entries with each entry
having three fields: the unique student ID (an integer), the student name (a string), and the
grade (an integer). The menu for the management system has the following choices:
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 a student to roster
5. Remove a student from roster
6. Exit
Note: In this assignment, you do not have to include the ability to display a single student’s
information as you did in the previous assignment. Also, you do not need to check whether
a student is already in the roster before adding them.
The Reset roster function resets the current roster to an empty roster. The Load roster
from file function prompts the user for a file name and loads the roster from the file. The
Store roster to file function prompts the user for a file name and stores the current roster
in the indicated file. The Display roster function displays the current roster, sorted by ID
in ascending order. The Add a student function prompts the user for the student ID, name,
and grade (one in each line) and adds the student to the current roster. The Remove student
function prompts the user to input either the student ID or name, and removes the student
from the roster (or reports that the student is not in the roster). An example run is shown
below.
The program must be able to display the menu and perform at least the following two tasks:
enter one student’s information (add a student to roster) and display the student informa1
tion (display roster). Programs that cannot perform these two functions (this includes all
programs with compiling errors) will receive at most 10 points. If a program can perform
these two functions, it will be graded as follows. Note that there are two bonus functions
for 5 points each. These are very similar to the Display roster sorted by ID and I encourage
everyone to try to implement them. If you do, make sure to add an entry to the menu for
each additional function.
• Reset roster (5 points)
• Exit (5 points)
• Add a student to roster (30 points)
• Display roster sorted by ID (30 points)
• Display roster sorted by name (BONUS 5 points)
• Display roster sorted by grade (BONUS 5 points)
• Remove a student from roster (10 points)
• Store roster to file (10 points)
• Load roster from file (10 points)
Some other miscellaneous notes:
• My sample implementation of this assignment (minus bonus features) has 106 lines of
Prolog code.
• After you implement the basic menu code, you should implement the Add a student to
roster and one of the Display roster functions.
• You can assume that the input is correct: the code can have undefined behavior when
the input is wrong.
• The program will be loaded as [’roster.pl’]. The menu will be started as menu([]).
• Note the usage of double and single quotes as well as periods in the sample execution.
Your program must work with EXACTLY this input.
Assignment Submission
Submissions are due on April 26 at 11:59pm. You should submit your roster.pl file to
Blackboard with your name and FSUID in a comment at the top of the file.
2
Sample Execution
?- [’roster.pl’].
% roster.pl compiled 0.00 sec, 1 clauses
true.
?- menu([]).
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.
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: 2.
Student Name: "Justin Fincher".
Grade: 80.
3
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: 5.
Student Name: "Amy S. Stevenson".
Grade: 92.
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: 4.
Student Name: "Mike Harris".
Grade: 97.
Class roster management system
==============================
MENU
==============================
0. Reset roster
4
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 ’.’): 3.
Display roster, sorted by ID.
No.1: ID = 1, Name = Michael Jordan, Grade = 90
No.2: ID = 2, Name = Justin Fincher, Grade = 80
No.3: ID = 4, Name = Mike Harris, Grade = 97
No.4: ID = 5, Name = Amy S. Stevenson, Grade = 92
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: 3.
Student Name: "Sally Smith".
Grade: 100.
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
5
Enter your choice (followed by a ’.’): 3.
Display roster, sorted by ID.
No.1: ID = 1, Name = Michael Jordan, Grade = 90
No.2: ID = 2, Name = Justin Fincher, Grade = 80
No.3: ID = 3, Name = Sally Smith, Grade = 100
No.4: ID = 4, Name = Mike Harris, Grade = 97
No.5: ID = 5, Name = Amy S. Stevenson, Grade = 92
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 ’.’): 2.
Enter a filename to store roster to: ’oldroster.txt’.
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 ’.’): 5.
Remove a student from the class roster.
Enter a student name or ID: 3.
Student removed.
Class roster management system
==============================
6
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 ’.’): 3.
Display roster, sorted by ID.
No.1: ID = 1, Name = Michael Jordan, Grade = 90
No.2: ID = 2, Name = Justin Fincher, Grade = 80
No.3: ID = 4, Name = Mike Harris, Grade = 97
No.4: ID = 5, Name = Amy S. Stevenson, Grade = 92
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 ’.’): 5.
Remove a student from the class roster.
Enter a student name or ID: "Mike Harris".
Student removed.
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
7
6. Exit
Enter your choice (followed by a ’.’): 3.
Display roster, sorted by ID.
No.1: ID = 1, Name = Michael Jordan, Grade = 90
No.2: ID = 2, Name = Justin Fincher, Grade = 80
No.3: ID = 5, Name = Amy S. Stevenson, Grade = 92
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 ’.’): 5.
Remove a student from the class roster.
Enter a student name or ID: 10.
Student does not exist.
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 ’.’): 0.
Reset roster (now empty).
Class roster management system
==============================
8
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 ’.’): 3.
Display roster, sorted by ID.
No records to display.
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 ’.’): 1.
Enter a filename to load roster from: ’oldroster.txt’.
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 ’.’): 3.
Display roster, sorted by ID.
No.1: ID = 1, Name = Michael Jordan, Grade = 90
9
No.2:
No.3:
No.4:
No.5:
ID
ID
ID
ID
=
=
=
=
2,
3,
4,
5,
Name
Name
Name
Name
=
=
=
=
Justin Fincher, Grade = 80
Sally Smith, Grade = 100
Mike Harris, Grade = 97
Amy S. Stevenson, Grade = 92
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 ’.’): 6.
Good-bye!
true .
10
Download