编程辅导 C C++ Java Python MIPS Processing 网络家教 在线辅导

编程家教 远程写代码 Debug 讲解答疑 课后答疑 不是中介,本人直接答疑

微信: ittutor QQ: 14061936 Email: ittutor@qq.com

导航

the assignment is to write a search tool application that can be run from the command line as follows:

./search -s wordsFile file1 file2 ....fileN

The search program tries to search for each word in the wordFile, and print out line numbers of the file if it found a match for that word in filei. The program can look up more than one file. There is also a -s option that allows to ignore case when doing match and also matches the word which might be part of bigger word.

The wordsFile will contain zero or more words, one to a line. For this assignment a word is defined as a sequence of alphabetic characters. Each word is guaranteed to have at most 40 characters, not counting the newline character. Each file will contain zero or more lines of text and there will be at most 160 characters, not counting the newline character, on each line.

Output

The output for this program will be to stdout. Error messages and usage messages, when needed, will be to stderr.

For each file listed, print the file name on the first line of output. Search the file for each of the words in wordsFile. For each word, print the word followed by a list of line numbers that contain the word. If the word appears more than once on a line, then print an asterisk (*) after the line number. Put one blank space after the word, and one blank space in-between the numbers. If the word does not appear in the file, do not print the word. Once the file has been searched for all the desired matches, print a blank line.

Without the -s option we are looking for matches of complete words, not partial matches. Thus, if you are looking for the word able, then reasonable, and tablespoon will not be a match. Thus, if your file contains the following:

  Bart was an able student. Tables were his specialty. 
  Lisa was a capable student. Able72’s and 473able’s were his specialty.

The first sentence matches able, but not Tables. So the line number would appear but without an asterisk. The second sentence matches 473able’s since the 3 and the apostrophe are not alphabetic characters. If -s were used, then the second line would have an asterisk since it would also match Able72’s.

Here is a sample set of what the output should look like (The files can found in hw6.zip)

 
Note: > indicates the command line in a shell
> cat words.txt 
free
we
never
a
the
nation
nevermore
consecrate
 
> gcc -Wall search.c -o search
> ./search words.txt test.txt
test.txt
we 2 3* 
never 1 
the 3   
  

Note: In file test.txt on line number 1, the word "The" is not matched with "the" as the -s option is not specified.

>./search words.txt gettsyburg.txt 
gettsyburg.txt
we 3 6 7* 9 11 12 
never 9 
a 1 3 4* 7 13 
the 2 9 10 11 12 14* 
nation 1 3* 5 13 
consecrate 7 
  
>./search words.txt test.txt gettsyburg.txt 
test.txt
we 2 3* 
never 1 
the 3  
 
gettsyburg.txt
we 3 6 7* 9 11 12 
never 9 
a 1 3 4* 7 13 
the 2 9 10 11 12 14* 
nation 1 3* 5 13 
consecrate 7 
 
Note: The word “consecrated” appears on line 8 of gettysburg.txt, but 8 does not appear as a line number for consecrate as -s option is not specified.
  

Error Handling

There should be at least two command-line arguments: the file name for the words file and one file to search. If -s is present as the first argument, then there should be at least three command-line arguments. Report an error (use stderr stream) in the following case and abort the program (note: if not in main() then use exit(status)):

  • If there are not enough arguments.
  • If fopen on the wordsFile fails.
  • If fopen on any of the file command-line arguments fails, report the error. Do not continue processing command-line arguments in this case. Note that you may have successful output for some of the file names listed. If you reach a file name that does not open, report the error and exit.
  • If the first argument starts with a dash, but is not -s
  • If your program searches the specified file(s), but does not find any matches. Note: If you find at least one match somewhere in the specified file(s), then program should terminate successfully(either reach end of main or exit(0)).

Some suggestions/hints (C libraries)

  • If you running to segfaults use the GDB debugger to determine the cause.
  • When you want to create an array of pointer type and if you not intializing them make sure to give it size. E.g. char * s[20].
  • Useful C functions in stdio.h
    • fgets (instead of fgetc) gets a string from file. For more specifications see the library link. Also fgets reads in newline character so you want to get rid of it while processing the words lines.
    • rewind which will set the file to start reading from the beginning of the file or your you close and open the file again.
  • Useful C functions in string.h
    • strcmp or strncmp (better has we specify the size) to compare strings
    • strtok to break up string into tokens based on delimiter. Below is sample code:
o      
o      char * str = "- This, a sample string."  
o      char * pch;
o      printf ("Splitting string \"%s\" into tokens:\n",str);
o      pch = strtok (str," ,.-");
o      while (pch != NULL)
o      {
o        printf ("%s\n",pch);
o        pch = strtok (NULL, " ,.-");
o      }
o     
o     Splitting string "- This, a sample string." into tokens:
o     This
o     a
o     sample
o     string      
      
    • strstr which takes two strings - one string and other substring. Returns first occurance of substring in the string.
  • Useful function is ctype.h
    • isupper, tolower and toupper for checking case and changing case of a char.

Turn-in

  • Atleast one source file called search.c which will contain the main method. Make sure that you break down your code in functions. Provide a seperate header file for function prototypes and constant definitions.
    • Make sure that code follows coding practices. Especially make sure your put comments in your code.
  • A file named makefile to comile, assemble and link your file. The makefile should support atleast two targets: search (for compilation) and clean for removal of object files or executables.
  • A file named README (is plain textfile) file that describes the usage of the makefile, and describes your approach for the search tool.
  • Use an array of struct’s to hold the matches found for words in a given file. Assume there will be at most 30,000 words in the words file. The specifications for what constitutes a word are the same as above. Assume up to 100 matches per word. The struct should hold the word itself, the matches, and any other information you need to store about the word.
  • Matches will be per page rather than per line. We will define a “page” as being 60 lines in the file. If the number of lines in the file is not divisible by 60, then the lines left over at the end of the file will constitute the last page.
  • Report only whether the word appears on the page. You do not need to include an asterisk for words that appear more than once on a page.
  • The file that contains the main method should be called search2.c. The header file should contain function prototypes and constant definitions. It can contain additional items, as appropriate to your solution.
  • Include a Makefile that supports compilation of your files. The makefile should support atleast two targets: search2 and clean.
  • Also provide README file describe your approach.
  • Turn-in files according to the submission guidelines.

 

相关推荐