A solution to Problem A (5 point)
There are many ways of solve problem A. This program demonstrates several things. This was a question from
the N.Z. 1995 programming competition. There were teams (from Auckland
:-) that could not
do this in five hours.
This is how I would do
it. If you know a better way, let me know, that's how we all learn.
Good things
Not so good things
// A solution to problem A by Stuart Inglis, in C++
#include < iostream.h >
#include < strstream.h >
#include < fstream.h >
#include < assert.h >
#define MAXLEN 60+1
int main(void)
{
ifstream infile("PROBLEMA.DAT", ios::in);
assert(infile);
while(1){
char linebuffer[MAXLEN],word[MAXLEN];
infile.getline(linebuffer,MAXLEN);
if(strcmp(linebuffer,"#")==0) break;
istrstream strbuffer(linebuffer,strlen(linebuffer));
int count=0;
while(strbuffer >> word)
count++;
cout << "The line contains " << count << " words" << endl;
};
return 0;
}
|