C++ Intro

hw 6: sort, reverse, uniquify


Description

In this assignment you will write a program that uses C++ Standard Library vector container. The program will collect input from the user and sort, reverse, or uniquify the set of the input strings. While working on this assignment, you will have a chance to use C++ command-line arguments and the library algorithms to implement the required functionality.

Part 1. Getting started

  1. Consider the following program that inputs multiple lines of text from the user:
    
    #include <cassert>
    #include <iostream>
    #include <string>
    #include <vector>
    
    int main( int argc, char* argv[] )
    {
        std::vector< std::string > vect;
        std::string input;
        while ( std::getline( std::cin, input ) ) {
            vect.push_back( input );
        }
        return 0;
    }
    
    
    
    The code instantiates a vector of strings named vect, a string object named input , and collects an arbitrary number of strings from the standard input. In order to terminate the input, the user must enter CTRL^Z on a single line to specify the magic "end of file" marker. Such end of input is processed automatically by the getline( ) system function. Therefore, the program has an elegant way of checking for input availability.

  2. Take a look at another small sample, which examines the first command-line argument specified on the command line:
    
    #include <cassert>
    #include <iostream>
    
    int main( int argc, char* argv[] )
    {
        assert( argc > 1 ); // make sure at least one argument present
        for ( int idx = 0; argv[1][idx]; ++idx )
            std::cout << argv[1][idx] << std::endl;
        return 0;
    }
    
    
    
    The program prints individual characters of the first command-line argument. For example, if this program is invoked with argument abc , it will print the following output:
            C:\>myprog.exe abc
            a
            b
            c
    
            C:\>
    

Part 2. The assignment

  1. Your assignment is to create a useful utility to get user input and, according to the command line argument, sort, reverse, or uniquify the input lines and then print the result. These actions are controlled by the command argument in the following manner. The argument could be any combination of the uppercase or lowercase letters 'S', 'R', and 'U', which should be interpreted as the request to either sort alphabetically, or reverse, or eliminate non-unique lines from the input. The following examples demonstrate possible user commands and their results. (The output generated by the program is highlighted for clarity.)

    Description Command line, user input, and program output:
    User wants to sort lines of text:
    C:\>myprog.exe S
    def
    abc
    xyz
    ^Z
    abc
    def
    xyz
    C:\>
    
    User wants to reverse the order of the text lines:
    C:\>myprog.exe R
    one
    two
    three
    ^Z
    three
    two
    one
    C:\>
    
    User wants to sort and exclude non-unique lines from the result.
    C:\>myprog.exe SU
    xyz
    ab
    ddd
    ab
    xyz
    ^Z
    ab
    ddd
    xyz
    C:\>
    


Part 3. The steps to implement your program

  1. You can start your coding by copying and pasting the first sample that inputs multi-line text from the user. Make sure that the program compiles and runs properly.

  2. Add the following code to print the text on the screen:
    
        std::copy(
            vect.begin(),
            vect.end(),
            std::ostream_iterator< std::string >( std::cout, "\n" )
            );
    
    
  3. For this modified code to compile properly, you must also add the library header that provides STL algorithms:
    
    #include <algorithm>
    
    
  4. Again, make sure that the program compiles and runs properly at this point.

  5. Add code to access individual characters of the command-line argument from another sample above:
    
        for ( int idx = 0; argv[1][idx]; ++idx ) {
            std::cout << argv[1][idx] << std::endl;
        }
    
    
    Instead of printing characters on the screen, use a switch statement, or a series of if-else statements to examine individual characters. You are now ready to write code to handle each possible case: 'S', 'R', and 'U', as well as their lowercase equivalents, 's', 'r', and 'u'.

  6. Use the following STL algorithm to implement sort :
    
        std::sort(
            vect.begin(),
            vect.end()
            );
    
    
  7. Use the following STL algorithm to implement reverse :
    
        std::reverse(
            vect.begin(),
            vect.end()
            );
    
    
  8. Elimination of non-unique lines from the input is a bit trickier. Since some lines will be removed from the original input, the resulting text will become shorter. Because of this, the std::unique algorithm of the C++ Standard library returns the new end of the container that is being processed by the program. That new end must be used when the program displays the final output. Here is an example:
    
        std::vector< std::string >::iterator new_end = std::unique(
            vect.begin(),
            vect.end()
            );
    
        std::copy(
            vect.begin(),
            new_end, // in all other cases: vect.end(),
            std::ostream_iterator< std::string >( std::cout, "\n" )
            );
    
    
    While this is not a big challenge, the program should distinguish "uniquify" from all other cases, where the entire vector is printed. Since the program does its work and exits, your code could have the proper way of displaying the output for each algorithm and use a return statement to exit out of the program.

  9. Finally, you can improve your program by adding some logic to handle a situation when no argument on the command line is present. In such case, program should default to 'U', that is, assume that the user wants to uniquify the input.