Introduction
很基础的C程序,实现统计输入文件中的单词总数,此外还需要写一个计算多边形的小程序,以及一个打印行尾的小程序。
Requirement 1
Write a program called wcount.c that counts all the words in a given file
- Name your executable wcount.out
- The name of the file to count the words in will be passed on the command line
- For this problem we will define a word as a series of 1 or more non-whitespace
characters
- Use isspace to determine if a character is white space or not
Examples
Assume we have file named fun.txt that contains the following:
Computer sciences classes are great!
Even though I spend all my time doing homework :(
1 | ../wcount fun.txt |
Requirement 2
Write a program called perimeter.c that calculates the perimeter of a polygon.
- Name your executable perimeter.out
- The points of the polygon will be stored in a file and this file will be passed on the command line arguments
- The file itself will be a binary file containing integers
The first integer in the file is the number of points contained in the file
The remaining integers are the points, with the first integer being the x coordinate and the second integer being the y coordinate.
There is an edge between each adjacent point and between the first point and the last point
Each file contains at least 3 points - The perimeter of a polygon is the sum of the lengths of all of its edges
- Use a double to store your perimeter and report the perimeter to the nearest 2 decimal points.
- You MUST store your points in a struct to receive credit for this problem.
- As an aside the example tests do not form actual polygons but assume that they do.
Example. Assume that there is a file called example.txt. It will store the following information but in binary form. This example is just to give you a visualization of the data.
3
287 422
283 -981
781 647
1 | ./perimeter example.txt |
Requirement 3
Write a program called tail.c that prints out the last N lines of a given file
- Name your executable tail.out
- Arguments to your program will be passed on the command line in the following order
Name of the file
N - N will always be at least 1
- If N is greater than the number of lines in the file, all of the lines in the file should be displayed
- You may assume that no line is longer than 100 characters
For an additional challenge try to solve the problem where there is no limit on the length of a line
My solution to dealing with any length lines involved using the functions ftell and fseek. - There is no limit to the number of lines in a file
- Some lines may only contain the newline character, these still count as a line
- My editor automatically added 1 newline character to the end of most of the test files but does not display them. Your editor may or may not display them but do be aware that they are there.
Example. Assume the file meme.txt contains the following
It
is
over 9000!!!
1 | ../tail.out meme.txt 1 |
1 | ../tail.out meme.txt 2 |
1 | ../tail.out meme.txt 10 |