Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Objective:
- Program with arrays of structures. You will write a basic ToDo-list program.
- Description:
- Your program will maintain an array of 10 Task structures which each includes:
- a Boolean "done" field indicating if the task is done
- string description of the task
- an integer priority
- Your program will print each unfinished task, one-per-line, in decreasing order of priority, breaking ties by increasing "lexicographic" order of the description (i.e. the < operator on the string class). Lines should be numbered starting with 0, and the priority of the task should be shown after the line number. Users can then enter one of the following:
- -<i> to mark the task in row number i as done
- +<description> to add a new task with the given description (your program should then ask for a priority for the new task; the new task will replace a task that has already been marked as done if there is one)
- x to exit the program
- Example
- Here is an example of what your program might look like during operation:
- Row Task
- 0 [85] Eat breakfast
- 1 [50] Finish Lab 2
- 2 [23] Call my mom
- 3 [10] Eat Lunch
- Please enter -<row>, +<description>, or x (to exit):
- -0
- Entering -0 as shown above would produce the following output:
- Row Task
- 0 [50] Finish Lab 2
- 1 [23] Call my mom
- 2 [10] Eat Lunch
- Please enter -<row>, +<description>, or x (to exit):
- +Re-read Lab2 description
- What priority? 60
- Given the additional task entered above the program would output the following:
- Row Task
- 0 [60] Re-read Lab2 description
- 1 [50] Finish Lab 2
- 2 [23] Call my mom
- 3 [10] Eat Lunch
- Please enter -<row>, +<description>, or x (to exit):
- x
- Entering x tells the program to exit.
- Tips
- Note that you should not need to dynamically allocate any memory (i.e. you don't need to call new or delete).
- You can give the done member a default value of true in the Task struct definition.
- Suggested process:
- name your file todo.cpp
- define the Task struct (with done defaulting to true)
- declare the array of 10 Tasks as a local variable in main
- write a function that prints the task list (only the not done items):
- void printList(const Task tasks[], int n);
- implement the following function that returns true if the first parameter should be listed before the second:
- bool isLessThan(const Task& a, const Task& b);
- We want to keep all of the finished tasks at the end of the list, sort by priority if both tasks are either done or not done, and then sort by the description if both tasks have the same priority. Here is the basic logic for isLessThan:
- bool isLessThan(const Task& a, const Task& b) {
- // first try to arrange based on difference in "done" field
- // if only one of the two tasks is done, then a should come first if it has not been done yet
- // if a.done != b.done, then return !a.done
- // next, try to arrange based on priority
- // if a.priority != b.priority, then return a.priority > b.priority
- // if they have the same done status and the same priority, then just arrange by description
- return a.description < b.description;
- }
- implement the following function that swaps the contents of the two Tasks:
- void swap(Task&, Task&);
- Implement the sorting functionality:
- void sort(Task a[], int n) {
- for (int i = 0; i < n; ++i) {
- for (int j = i + 1; j < n; ++j) {
- if (isLessThan(a[j], a[i])) {
- swap(a[i], a[j]);
- }
- }
- }
- }
- Write the code to loop and allow user input, sorting and printing the list on each iteration. We don't care about remembering tasks that have been completed. Therefore, because of the way you are sorting, new tasks can simply replace the last task in the array, or your should print an error message if the last task is not yet done. Note that you can first read one character, c, from cin and then check to see if it is '-', '+', or 'x', and then handle the rest of the input appropriately (recall that you can use the getline(cin, s) function the remaining characters on a line of input into a string s). To pass off, you can assume that the user will only give valid input, but feel free to try to make your program user-friendly in the face of errors.
- After your code is working, you will need to submit your todo.cpp file and pass-off in-person with a TA or instructor.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement