Advertisement
Guest User

ad

a guest
Jan 22nd, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. Objective:
  2.  
  3. Program with arrays of structures. You will write a basic ToDo-list program.
  4.  
  5. Description:
  6.  
  7. Your program will maintain an array of 10 Task structures which each includes:
  8.  
  9. a Boolean "done" field indicating if the task is done
  10. string description of the task
  11. an integer priority
  12. 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:
  13.  
  14. -<i> to mark the task in row number i as done
  15. +<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)
  16. x to exit the program
  17. Example
  18.  
  19. Here is an example of what your program might look like during operation:
  20.  
  21. Row Task
  22. 0 [85] Eat breakfast
  23. 1 [50] Finish Lab 2
  24. 2 [23] Call my mom
  25. 3 [10] Eat Lunch
  26. Please enter -<row>, +<description>, or x (to exit):
  27. -0
  28. Entering -0 as shown above would produce the following output:
  29.  
  30. Row Task
  31. 0 [50] Finish Lab 2
  32. 1 [23] Call my mom
  33. 2 [10] Eat Lunch
  34. Please enter -<row>, +<description>, or x (to exit):
  35. +Re-read Lab2 description
  36. What priority? 60
  37. Given the additional task entered above the program would output the following:
  38.  
  39. Row Task
  40. 0 [60] Re-read Lab2 description
  41. 1 [50] Finish Lab 2
  42. 2 [23] Call my mom
  43. 3 [10] Eat Lunch
  44. Please enter -<row>, +<description>, or x (to exit):
  45. x
  46. Entering x tells the program to exit.
  47.  
  48. Tips
  49.  
  50. Note that you should not need to dynamically allocate any memory (i.e. you don't need to call new or delete).
  51. You can give the done member a default value of true in the Task struct definition.
  52. Suggested process:
  53. name your file todo.cpp
  54. define the Task struct (with done defaulting to true)
  55. declare the array of 10 Tasks as a local variable in main
  56. write a function that prints the task list (only the not done items):
  57. void printList(const Task tasks[], int n);
  58. implement the following function that returns true if the first parameter should be listed before the second:
  59. bool isLessThan(const Task& a, const Task& b);
  60. 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:
  61.  
  62. bool isLessThan(const Task& a, const Task& b) {
  63. // first try to arrange based on difference in "done" field
  64. // if only one of the two tasks is done, then a should come first if it has not been done yet
  65. // if a.done != b.done, then return !a.done
  66.  
  67. // next, try to arrange based on priority
  68. // if a.priority != b.priority, then return a.priority > b.priority
  69.  
  70. // if they have the same done status and the same priority, then just arrange by description
  71. return a.description < b.description;
  72. }
  73. implement the following function that swaps the contents of the two Tasks:
  74. void swap(Task&, Task&);
  75. Implement the sorting functionality:
  76. void sort(Task a[], int n) {
  77. for (int i = 0; i < n; ++i) {
  78. for (int j = i + 1; j < n; ++j) {
  79. if (isLessThan(a[j], a[i])) {
  80. swap(a[i], a[j]);
  81. }
  82. }
  83. }
  84. }
  85. 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.
  86. 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