Assignment 3 - the Department of Computer Science

advertisement
CS115-001: Assignment 3
Winter 2010 (201010)
Due Date and Time: Friday, February 26, 2010 at 10:30 AM
1. (40 marks) A rational number is a number that can be expressed as a fraction
whose numerator and denominator are integers. Examples of rational numbers are
2/4 and 8/5. Write a program to implement and demonstrate the operation of a
Rational class that supports the following:











A default class constructor that initializes a Rational object by setting the
numerator to 0 (i.e., zero) and the denominator to 1 (i.e., one).
An explicit class constructor that initializes a Rational object by setting the
numerator to 5 (i.e., five) and the denominator to 11 (i.e., eleven).
An arithmetic add operation that computes the sum of two Rational
objects.
An arithmetic subtraction operation that computes the difference of two
Rational objects.
An arithmetic multiplication operation that computes the product of two
Rational objects.
An arithmetic division operation that computes the dividend of two
Rational objects.
A simplification operation that reduces a Rational object to its lowest
possible terms.
A Boolean operation that compares two Rational objects to determine
whether they are equal.
A Boolean operation that compares two Rational objects to determine
whether one is less than another.
A write operation that displays the value of a Rational object in either
fractional form (i.e., numerator/denominator) or decimal form (i.e.,
9.999999).
An operation that determines whether output is in fractional or decimal
format.
All output should be directed to the screen. When the program runs, it should first
declare and initialize three Rational objects, two for operands in the arithmetic
operations (one initialized by the default constructor and one by the explicit
constructor) and one for the result (initialized by the default constructor). Other
Rational objects can be declared if required.
To control the program, use the following menu:
1 – Set object 1
2 – Set object 2
3 – Add objects 1 and 2
4 – Subtract object 2 from object 1
5 – Multiply objects 1 and 2
6 – Divide object 1 by object 2
7 – Simplify object 3
8 – Compare objects 1 and 2
9 – Write object
10 – Set fractional output
11 – Set decimal output
12 – Exit
Select item: 99
If the selected item is 1 or 2, the program should prompt for the values of the
numerator and denominator, as follows:
Enter the numerator for object 9:
Enter the denominator for object 9:
If the selected item is 3, 4, 5, or 6, the corresponding operation should be
performed on the values in the first and second Rational objects, with the
result stored in the third Rational object. Output should be in the following
format (addition shown):
9/9 + 9/9 = 9/9 (or 9.999999 + 9.999999 = 9.999999)
If the selected item is 7, the value in the third Rational object should be
simplified, with the result stored back in the third Rational object. Output
should be in the following format:
9/9 = 9/9 (or 9.999999 = 9.999999)
If the selected item is 8, the magnitude of the value in the first Rational object
should be compared to the second. Output should be in the following format
(greater than shown):
9/9 > 9/9 (or 9.999999 > 9.999999)
If the selected item is 9, the program should prompt for the object to print, as
follows:
Select object: 9
Demonstrate the operation of all items for both fractional and decimal output.
2. (60 marks) Write a program to implement and demonstrate an array-based
SortedList class similar to that discussed in class. However, rather than
defining the ItemType as an int as done in class, the ItemType should be
defined as an EmployeeType, where EmployeeType is a struct defined,
as follows:
enum GenderType {MALE, FEMALE, UNKNOWN};
struct DateType
{
int month;
int day;
int year;
};
struct PayHistoryType
{
DateType lastPaidDate;
float lastPaidAmount;
};
struct EmployeeType
{
int number;
string name;
int age;
GenderType gender;
PayHistoryType payHistory;
};
The program should read a file called employees.in to load the employee data
into the SortedList, and all output should be directed to the screen. The
format of records in employees.in is as follows:








number: 5 numeric characters
name: 25 alphabetic characters
age: 2 numeric characters
gender: 1 alphabetic character (M = male, F = female, U = Unknown)
lastPaidMonth: 2 numeric characters
lastPaidDay: 2 numeric characters
lastPaidYear: 4 numeric characters
lastPaidAmount: 6 numeric characters containing two decimal places
(note that the decimal is not stored in the file)
To control the program, use the following menu:
1
2
3
4
5
6
7
8
9
–
–
–
–
–
–
–
–
-
Load employee data into list
Check if list is empty
Check if list is full
Check length of list
Insert new employee
Delete employee
Check if employee is in list
Print complete list
Exit
Select item: 9
When you are ready to test or run your program, use the file employees.in
from the directory ~hilder/cs115/assignment3/datafiles as input to
your program. To demonstrate the operation of your program, follow the
sequence of events in the file sequence, also from the directory
~hilder/cs115/assignment3/datafiles.
Note: Please note that you are required to use functions (extensively) to complete this
assignment. The main function in both programs may contain some variable
declarations, but it should contain no sequential code, only a series of function calls.
In addition, no global variables are allowed.
What to hand in: Submit your source code and a script that captures: (1) the
compilation and execution of the first program, (2) the compilation and execution of
the second program. Also, submit module structure charts for the programs that
describe the function hierarchy.
Download