Topical Information

This program should help you understand the use of (char-mixed) input and list management.

Program Information

Some students (as early as junior high or even as late as college) find reading assignment lists difficult. They see the teacher say something like 4-10 and do problem 4 and problem 10 — but not the other 5 assigned problems!

To help these poor lost souls, we'll write a program that can take in the (possibly) hyphenated assignment list and print back out a list of all problems assigned. Just in case, we'll also make sure our list is sorted (non-decreasing order) and contains no duplicates. (Yes, you must write your own sort function. You may NOT use the sort function from the algorithm standard library! Which of the sorts we learned in Chapter 6 is most appropriate for these circumstances?)

Here are some examples:

User Types In...We Display...
L1Do problem 1 of L.
L1-3 Do problems 1, 2, and 3 of L.
L1,2,5 Do problems 1, 2, and 5 of L.
L1-3,5 Do problems 1, 2, 3, and 5 of L.
L1-3,5-7 Do problems 1, 2, 3, 5, 6, and 7 of L.
L1-1,3-3,5-8 Do problems 1, 3, 5, 6, 7, and 8 of L.
L4-5,1-3,7-10 Do problems 1, 2, 3, 4, 5, 7, 8, 9, and 10 of L.
L4-5,1-3,7-10,8-12 Do problems 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, and 12 of L.

Let the problem set name be either a single character, a single word, or a quoted, space-containing string. The problem set name will always be entered first. If the user uses a quoted problem set name, allow them to use either a single quote (tick-mark) or double quote — but the open and close can be assumed to match. Spaces around the input should be optional (so "L1-3" should result in the same answer as "L 1 - 3"). All items/hyphenated groups are to be comma separated (so "L1,2,5" is okay but "L1 2 5" and "L125" are not). We'll assume that all problems are to be done for simplicity. (See the option below about evens vs. odds for more fun.) If the display of the problem list is longer than a line, wrap it gracefully — don't let the terminal window wrap it for you (or the printer will chop it off!).

Thought Provoking Questions

  1. In what data type can you store something like the problem set name that might be anything from one character to a whole bunch of text?

  2. How can you detect that the problem set name is a quoted string? How can you read in the whole thing to a single variable? (Don't worry that the user has missed a close quote.)

  3. If the user's problem set name is a quoted string, how can you remove the quotes from it? (Just like C++ won't leave your quotes on a string when it prints it, you shouldn't leave the user's quotes on their string. They are just there to separate the problem set name from the problem list.)

  4. When placing items (say problem numbers?) into a list, how can you keep those items sorted from the beginning?

  5. How can you avoid placing a duplicate item into a list? (Note: not how can you remove duplicates from a list — avoid having any duplicates in the list in the first place!)

  6. If the problem list is long (like in math or physics), how can you wrap the long output line to multiple lines without too much difficulty? (Hint: The output line can be around 70-75 chars long, we saw in 121 how to tell how many digits long an integer is, we know we are outputting a comma and space between the problem numbers, and we know how many chars we are adding before the list and after the list. Just don't forget the 'and' before the last element. So, knowing all that, especially that you need to drop to a new line after every 70-ish chars you've output, how do you tell if total chars printed is 70*n?)

This assignment is (Level 4).

Options