CSSE2310代写,CSSE2310代做,CSSE7231代写,C++编程代写
CSSE2310代写,CSSE2310/CSSE7231代写
Command Line Arguments
Your program (search) is to accept command line arguments as follows:
./search [-exact|-prefix|-anywhere] [-sort] pattern [filename]
In other words, your program should accept between one and four command arguments - zero, one or two option arguments followed by a pattern, then optionally followed by a dictionary filename. The option argument that specifies the type of search is ONE of -exact, -prefix or -anywhere. If this argument is not given, the default search type is -exact. Search types are explained below. If the -sort argument is present, then the output will be sorted alphabetically (also explained below). The -sort option may come before the search type option if both are present. The only argument which must always be present is the pattern itself. The pattern consists only of question mark characters and/or letters (upper or lower case) and may be empty. A question mark indicates a match with any letter. Some example patterns are:
abc
a?d
A???c?e
?b?D?
??
The last argument (if present) is the path name of the dictionary file, i.e. the name of a file containing words – one per line. Each line (including the last) is assumed to be terminated by a newline character (\n) only. You may assume there are no blank lines and that no words are longer than 40 characters. The filename can be relative to the current directory or absolute (i.e. begins with a /). Some examples are:
/usr/share/dict/words
../dictname
mydictionary (refers to a file with this name in the current directory)
If the filename is not specified, the default should be used (/usr/share/dict/words).
Program Operation
The program is to operate as follows
• If the program receives an invalid command line, e.g. (but not limited to) an invalid number of command line arguments, or an option argument begins with ‘–’ and it is not one of -exact, -prefix, -anywhere or -sort, or if more than one search type argument is present, then your program should print the message: Usage: search [-exact|-prefix|-anywhere] [-sort] pattern [filename]
to standard error, and exit with a non-zero exit status.
• If the given dictionary filename does not exist or can not be opened for reading, your program should print the message:
search: file "filename" can not be opened
to standard error, and exit with a non-zero exit status. (The italicised filename is replaced by the actual filename i.e. the argument given on the command line. The double quotes must be present.)
• If the given pattern contains characters other than question marks (?) and letters (either case), then your program should print the message:
search: pattern should only contain question marks and letters
to standard error, and exit with a non-zero exit status.
• Your program should then print out all lines in the given file that match the given pattern (if any). Lines are printed to standard output. The details of the pattern matching are described below. When all matches have been printed, your program should exit. If at least one match was printed, your program should exit with an exit status of zero. If no matches were reported, your program should exit with a non-zero exit status.
• Advanced Functionality: If the -sort argument is present, then the output lines must be sorted alphabetically - i.e. in the same order as a standard dictionary, with the case of each letter being ignored for sorting purposes (but the case of the words in the dictionary must be preserved when output). If two matching words are identical for sorting purposes, e.g. Miss and miss, then they may be output in either order.
Without the -sort argument, then the output lines must be in the same order as they were in the dictionary file. If you do not implement this advanced functionality, then your program should just ignore the -sort argument and output lines in the same order as they were in the dictionary file. (i.e. do not throw an error if -sort is specified but not implemented by your program).
Pattern Matching – Types of Search
As indicated above, there are three types of search (exact, prefix and anywhere). An exact match (the default type) means that the pattern should match a word exactly - matched words will be the same length as the pattern and each character in the pattern matches the corresponding character in the word (upper or lower case). A question mark in the pattern matches any letter in the word (but not numbers or punctuation characters). For example, consider a dictionary file containing the following lines:
3rd
Arctic
ardency
ardent
arduous
are
area
arena
aren't
With exact matching, the pattern ‘?r?’ will match ‘are’ only (but not ‘3rd’). The pattern ‘ar????’ will match ‘Arctic’ and ‘ardent’ but not ‘aren’t’.
(Words that contain nonalphabetic characters such as numbers, hyphens or apostrophes are never reported as matches for any of the search types.)
With prefix matching, the pattern matches the beginning part of the word – i.e. any number of letters (including zero) may follow the matching characters. For a file containing the lines given above, the prefix pattern ‘are’ will match ‘are’, ‘area’ and ‘arena’. The prefix pattern ‘?????’ will match all words which are five or more letters long (and contain only letters).
With anywhere matching, the pattern matches any part of the word, i.e. any number of letters (including zero) may precede the matching characters and any number of letters (including zero) may follow the matching characters. For a file containing the lines given above, the anywhere pattern ‘en’ will match ‘ardency’, ‘ardent’ and ‘arena’. The anywhere pattern ‘re?’ will match ‘area’ and ‘arena’. The anywhere pattern ‘T’ will match ‘Arctic’ and ‘ardent’.
