Chinaunix首页 | 论坛 | 博客
  • 博客访问: 627851
  • 博文数量: 603
  • 博客积分: 4000
  • 博客等级: 上校
  • 技术积分: 4940
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-17 11:04
文章分类

全部博文(603)

文章存档

2011年(1)

2008年(602)

我的朋友

分类:

2008-09-17 11:14:37

作业不交我就死了,请大虾帮帮忙(英文)

作业不交这科就要挂了,请大家帮帮忙啊,谢谢,第一个作业我作好了,第2个在下面.附件里面是原文件.
In this assignment, you will implement your first class. The purpose of this
class is to store clean text in such a way that it can be extracted a line
at a time without splitting words, if possible.

This specification will use the phrase "clean text" to mean a sequence of
characters which has no leading nor trailing whitespace, and within which
the only whitespace that does occur is single spaces. Additionally, clean
text will also mean that any non-null, non-whitespace characters which are
not regular printable characters have been replaced with underscore (_)
characters. (The printable characters are those between the space
(' ' ascii value 32) and the tilde ('~' ascii value 126).

This specification will also use the term "word" in the same way it was used
in the previous assignment: any consecutive series of non-null,
non-whitespace characters.

In writing this class, you will utilize the functions that you developed in
the first assignment, although you will want to change the name of that
assignment's source file so that it ends with ".cpp" rather than ".c".

Coding Specifications
---------------------
The class should be called "WwText" (Word-wrapping Text). All its data
members should be private, and it should have at least the following public
member functions. (If you have more public member functions than these, it
should not be necessary for those other functions to be called in order for
these functions to work properly.)

int set(char s[ ]) - causes the object to store a clean text version of
the text passed in the string, s. Note that the array s is not
changed in any way. If the object already stores some text, that
text will be replaced with the new text.
You may assume that the following limitations are acceptable:
+ if the string, s, is longer than 300 characters, then you may
ignore all but the first 300.
+ if, after converting the contents of the first 300 characters
of s to clean text, the resulting text is itself longer than
200 characters, then you may ignore all but the first 200
characters.
(Note that these length restrictions will be removed in the next
assignment).
This function returns a true value if all the data in the string was
able to be stored, and a false value if any truncation occured.

int set(int width) - causes the object to consider the maximum width of
a line to be the value passed in the variable, width. Note that the
only functions which need to know the width of a line are
findline( ), getline( ) and fwriteln( ), described further below.
This function sets the line width to be 80 if the variable, width,
contains a number that is below 1, and sets it to 200 if the
variable, width, is above 200.
This function returns a true value if the requested width was set,
and a false value otherwise.
Note that this function does not affect the text data stored by
object (as set, say, by the first version of set( ) above), just as
the first version of set( ), above, does not affect the width
setting.

int set(char s[ ], int width) - causes the object to store the text
passed in the string, s, as described in the first version of set( )
above, and also to consider the width of a line to be the value
passed in the variable, width, as described in the second version of
set( ) above.
This function returns a true value if both the entire string and the
requested width were able to be used, and a false value otherwise.

int append(char s[ ]) - causes the clean text version of the string s to
be appended to (i.e. added to the end of) the text already stored in
the object. (Note that one of the two set( ) functions which store
text in the string, or the fread( ) function described below, must
have already been called in order for this to be able to work
correctly).
The first word of the string, s, is to be interpreted as a different
word from the last word already stored in the object (rather than as
an extension of that word).
As with set( ), you may assume that if the string, s, is longer than
300 characters, then you may ignore all but the first 300, and that
if the clean text version of the new data stored by the object would
exceed 200 characters, then you may ignore all but the first 200.
Also, as with set( ), this function does not change the array s in
any way.
This function returns a true value if the entire string was able to
be used, and a false value if some truncation occured.

int findline(int n) - arranges things so that the next time a line is
obtained from the object (using getline( ) or fwriteln( ) described
below), it will be line number n of the object that is obtained,
where line number 0 is the first line.
This function assumes that every line has a maximum width as
specified by the current line width setting (set by one of the two
set( ) functions which allow setting of the width). Read the
specification for getline( ), below, for more details on what
constitutes a line.
This function returns the index, in the clean text version of the
string stored by the object, of the first character in line number
n.
If there is no line number n because n is too large, then things are
arranged so that an empty string would be obtained as the next line,
and the function returns index just after the last character of the
clean text stored by the object. If n is less than zero, then this
function works the same way it would work if n were zero.
Note that if this function is not called after a new string is
stored in the object using set( ) (or fread( ) described below), then
the effect will be the same as if findline(0) had been called after
the set( ) or fread( ).

int getline(char s[ ]) - copies the next line of clean text, stored in
the object, into the character array, s, as a null-terminated
string. A line always begins with a non-space character, and ends
within the current line width without splitting a word, if possible.
The only situation in which a word will need to be split is the case
where a line starts with a word that is too long to fit on the line.
In this case, as much of the word as will fit is copied.
Note that the spaces separating the end of most lines from the
beginning of the next are never copied.
This function also arranges things so that the next time it (or the
similar function fwriteln( ) described below) is called, then it will
obtain the next line after the line it just obtained.
This function returns the index, in the clean text stored by the
object, of the first character in the next line to be output.
This function assumes that the array, s, is large enough to hold the
line.
If the object is already positioned past its last line, then this
function copies an empty string into s, and returns the index just
after the last character of the clean text stored by the object.
Note that this function will not work properly unless the string and
the width have been set (using set( ) or fread( )). Note also that the
width of a line can be changed between calls to getline( ) (or
fwriteln( )), in order to extract lines of different lengths as you
move through the data.

int fwriteln(FILE *fp) - this function works like getline( ), except that
the characters of the line are written to the already opened file
referenced by the file handle, fp, rather than being stored in an
array. Note that this function does not write a newline to the file.
It returns the same thing that getline( ) would have, and affects
future calls of itself and getline( ) in the same way that getline( )
does.
When this function is finished, the file is left opened, positioned
so that any further writes would be placed immediately after the
data written by the function.

void getstring(char s[ ]) - copies the entire clean text, stored by the
object, into the array, s, as a null-terminated string. This
function assumes that the array is large enough to hold the string.
For this function to work properly, a string must have been stored
in the object (using set( ) or fread( )).

int length( ) - returns the number of characters in the clean text stored
by the object. For this to work properly, a string must have been
stored in the object (using set( ) or fread( )).

int position( ) - returns the index, in the clean text stored by the
object, of the next character to be obtained by getline( ) or
fwriteln(). For this to work properly, a string must have been
stored in the object (using set( ) or fread( )).

char position(int i) - returns the character at index, i, of the clean
text stored by the object. If i doesn't contain a valid index into
the text, then a null byte is returned. For this to work properly, a
string must have been stored in the object (using set( ) or fread( )).

void fwrite(FILE *fp) - writes the clean text stored by the object to
the already opened file, fp, using the following format:
+ integer storing the length of the clean text data
+ a space character as a separator
+ the clean text data itself (which will be exactly as many
characters as specified by the first field above)
When this function is finished, the file is left opened, positioned
so that any further writes would be placed immediately after the
data written by the function.
Note that neither line width information nor the current position in
the data is stored. For this to work properly, a string must have
been stored in the object (using set( ) or fread( )).

int fread(FILE *fp) - reads clean text data in from the the already
opened file, fp, assuming the same format descibed in fwrite( ),
above, replacing any text data that may be stored y the object. This
function can assume that the integer specifying the length is
correct, but cannot assume that the text data read from the file is
clean text, and so should process it using the rules for the set( )
function.
This function returns the length of the newly read clean text.
If data cannot be read (e.g. there isn't an integer there, or the
designated number of characters cannot be read from the file) then
the object is made to store an empty string and zero is returned.
After fread( ) is called, the object will act as if findline(0) had
been called. The line width setting is not affected by calling
fread( ).
When this function is finished, the file is left opened, positioned
so that any further reads would read data immediately after the
data read by the function.

A Simple Example
----------------
The code fragment

WwText txt;
txt.set("This is a\nlittle\t\t\ttest", 15);
txt.append("of the assignment 2");
txt.append("specification ");
char outline[31];
int pos;
do {
pos = txt.getline(outline);
puts(outline);
} while (pos < txt.length());
txt.findline(1);
txt.set(20);
do {
pos = txt.getline(outline);
puts(outline);
} while (pos < txt.length());

would output

This is a
little test of
the assignment
2 specification
little test of the
assignment 2
specification

Assignment submission
---------------------
As with the previous assignment, shortly before the due date, a main which
tests your object will be made available on this web site.
[This source file will assume that there is a header file named "wwtext.h"
containing the WwText class declaration. To match this, you should place the
member function definitions for the WwText class in a file named
"wwtext.cpp". As mentioned earlier, you should also rename your assignment 1
source code to be "a1.cpp".]

When your code is compiled with this file, the resulting program will simply
tell you whether your code works well enough to be submitted or not. If it
tells you that you may submit your assignment, you should then prepare a
session run that can be printed out and submitted:

1. Enter the command:
script
to start capturing your session.

2. Enter the commands:
cp a1.c a1.cpp (copying assignment #1 to "a1.cpp")
cat a1.cpp
cat wwtext.h
cat wwtext.cpp
to display your source code, which should be well documented.

3. Enter the commands:
c++ a2main.cpp a1.cpp wwtext.cpp (a2main.cpp copied from the website)
to show that your code compiles properly with the required main program.

4. Enter the command:
a.out
to run your program, showing that it works.

5. Enter the command:
exit
to terminate the session capturing. This produces a file named
"typescript" which can be printed out and submitted.

Your instructor may have additional submission requirements that you are
also responsible for fulfilling.


The due date for this assignment is February 18, 2005 @ 4:00 p.m.
(all sections)
Assignments not submitted by the due date will receive a penalty of 10%
per business day to a maximum penalty of 50%.
If you submit an assignment that does not pass all of the tests, you will
need to fix and resubmit it, in which case you will receive a mark no
higher than 50%.
Regardless of how long it takes you to do this assignment, it still needs to
be completed in order to pass the subject. Keep in mind that future
assignments are going to build on what you have produced in this assignment.

Note:
Testing your assignment by writing your own test main( ) initially is
important and necessary work. If you wait for the official assignment
main( ) program to be released before testing your work, you may find yourself
not having sufficient time to fix the mistakes before the due date. So get
into the habit of writing your own test main, besides you can always compare
your test main( ) with the one that is posted before the due date and
learn a little more in the process.


--------------------next---------------------
这是第一个作业,已经作好了

#include
#include

void oneSpace(char s[]){ /* s[] holds the string that was passed from main */
int sLength,x,y; /* variable holds the length of the string passed */
/* find the length of the string by using the strlen function */
sLength = strlen(s);
/* this loop will replace any linefeed or taps into a single space */
for(x=0;x if(s[x]=='\n' || s[x]=='\t') s[x]=' ';
}
/* check to see if the beginning of the string has white spaces */
for(x=0;x /*
// checks to see if there are white spaces before the first
// acceptable character, when an acceptable character is hit,
// it checks to see if the character is the first character
// in the string. If the first acceptable character is hit
// and it is not the first character in the string then it will
// know there are white spaces at the begining of the string.
*/
if(s[x]!=' ' && x>=0){
/* using y+x-1 so that it will add the null terminating character */
for(y=0;s[y+x-1]!='\0';y++){
/* copy the rest of the string starting at the first
acceptable character. */
s[y]=s[y+x];
}/* end of the for loop */
x=sLength;
}
}
/* this loop will look for 2 consecutive white spaces */
for( x=0;x /* if s@x is a space look to see if the next character
is a white space also. */
if(s[x]==' ' && s[x+1]==' '){
/* if it is then run a sequence to move the characters forward
one space until a null terminating character is found */
for(y=x;s[y]!='\0';y++){
s[y]=s[y+1];
}
/* reset counter so that it will start over and look
for more 2 consecutive white spaces. */
x=-1;
}
}
/* Check to see if there is any space at the end, if so then remove it
by putting a null terminating character */
sLength=strlen(s);
if(s[sLength-1]==' '){
s[sLength-1]='\0';
}
}

int findEndOfLine(char s[], int startindex, int width){
/* Variable to hold the index of the last word that fits */
int Index=0, SnW, testnum=0, sLength, x, y;
sLength = strlen(s);
/* If the user specified startindex + width is outside of the string
that is passed, make the changes accordingly */
SnW = startindex + width;

/* If at location from where startindex and where width ends
is not a white space */
if(s[SnW-1]==' ' || s[SnW-1]=='\t'){
for(x=1;x /* If its a space or tab and not at the first array */
if((SnW-x)!=startindex && (s[SnW-x]==' ' || s[SnW-x]=='\t'))testnum=0;
/* if its not a space or tab */
if(s[SnW-x]!=' ' && s[SnW-x]!='\t')testnum=1;
/* if its at the start and only space and tabs have been found */
if(SnW-x==startindex)testnum=2;

switch(testnum){
/* Case one handles situations where there are alot of spaces
after an acceptable character */
case 1:
Index=SnW-x+1;
x=SnW+1;
break;
/* Case two handles situatioin where there are alot of spaces
at the startindex */
case 2:
Index=x;
x=SnW+1;
break;
}
}
}
/* If it is an acceptable character */
else{
/* if the SnW is at the end of a word */
if((s[SnW-1]!=' ' || s[SnW]!='\t') && (s[SnW]==' ' || s[SnW]=='\t')){
Index=SnW;
}
else{
/* This for loop will compare the indexes before it
// every non white space it finds, it will look at the index
// before it, to see if there is a white space, if there is, then
// it is the last word to fit.
// =========================================================
// x starts at 2 because going back 1 space doesnt make a different
// because even if going back 1 space is a space u will still need to
// go back another space to see if its a word */
for(x=2;x /* Go back 2 spaces to see if its a character if its a character
then look at the space ahead of that to see if its a white space */
if((s[SnW-x-1]!=' ' || s[SnW-x-1]!='\t') &&
(s[SnW-x]==' ' || s[SnW-x]=='\t')){
/* If a linefeed character is found, then the index is
where the line feed character is.
Add one to x to make x the location of the linefeed */
if(s[SnW-x-1]=='\n') x++;
/* If there is a white space then that index is the
index of the last word */
Index=SnW-x;
/* end the loop by making x = to width+1 */
x=width+1;
}/* End of If */
else{
if((SnW-x-1)==startindex){
Index=SnW;
}
}
/* This routine is to test if there is no continuous
white spaces between the startindex and the width */
if((SnW-x-1)==startindex && (s[startindex]==' ' || s[startindex]=='\t')){
/* Look for the first character after the white space */
for(y=startindex; y<(SnW+1);y++){
if((s[y]==' ' || s[y]=='\t') && (s[y+1]!=' ' || s[y]!='\t')){
Index=y+1;
y=SnW+1;
}
}
/* ending the loop */
x=width+1;
}
}/* End of For Loop */
}/* End of If */
/* If the length of the string passed is less then the startindex & width stretch
then the index is the slength */
if(sLength < SnW){
Index = sLength;
}
}
return Index;
}


--------------------next---------------------

阅读(320) | 评论(0) | 转发(0) |
0

上一篇:下一主题

下一篇:下一主题

给主人留下些什么吧!~~