The End
118 2006-11-07
$ cat getword.cpp
#include <string>
#include <stdio.h>
#include <iostream>
string getword(FILE * f)
{ int c=fgetc(f);
int newlines=0;
while (c==' ' || c=='\t' || c=='\n' || c=='-')
{ if (c=='\n')
{ newlines+=1;
if (newlines==2)
return "*end of paragraph*"; }
c=fgetc(f); }
string word="";
while (c!=' ' & c!='\t' & c!='\n' & c!='-' & c!=EOF)
{ word+=(char)c;
c=fgetc(f); }
ungetc(c, f);
if (c==EOF)
return "*end of file*";
return word; }
const int maxsize = 1000;
int read_paragraph(string words[], FILE * phil)
{ int num=0;
while (num<maxsize)
{ string s=getword(phil);
if (s=="*end of paragraph*")
return num;
if (s=="*end of file*")
return -1;
words[num]=s;
num+=1; } }
struct solution
{ int lastword;
int characters_used;
bool ended; };
solution set_line(string words[], int numwords, int firstword, int max_chars)
{ int len=0;
int word;
for (word=firstword; word<numwords; word+=1)
{ int to_add = words[word].length();
if (word!=firstword)
to_add+=1;
if (len+to_add>max_chars)
break;
len+=to_add; }
solution s;
s.lastword=word-1;
s.characters_used=len;
if (word>=numwords)
s.ended=true;
else
s.ended=false;
return s; }
const int page_width=40;
void main(void)
{ FILE * thefile=fopen("sample.txt", "r");
if (thefile==NULL)
{ cout < "Can't read sample.txt\n";
exit(1); }
cout < " 11111111112222222222333333333344444444445555555555\n";
cout < "12345678901234567890123456789012345678901234567890123456789\n\n";
string wordlist[maxsize];
while (1)
{ int numwords = read_paragraph(wordlist, thefile);
if (numwords == -1)
{ cout < "\nThe End.\n";
break; }
int start_word=0;
while (true)
{ solution sol = set_line(wordlist, numwords, start_word, page_width);
for (int i=start_word; i<=sol.lastword; i+=1)
{ cout < wordlist[i];
cout < " "; }
cout < "\n";
if (sol.ended)
break;
start_word=sol.lastword+1; }
cout < "\n"; }
fclose(thefile); }
$ cat sample.txt
It was a dark and stormy night when Zeke Nergfruner, a
heavy-set immigrant from Belgium finally got home after
a double shift as cashier at the horse shop. At
first he didn't notice the lifeless body draped over the lower
limbs of his second-favourite lemon tree.
As he groped clumsily in the front left pocket of the heavy
woollen rain-coat that had been left to him after the timely death of his
slightly
short sighted uncle only three years ago, and which he was now wearing, lightning
flashed sullenly in the lowering black night sky.
A glint of light reflected from highly polished
bronze of the antique anvil which had crushed the head
of Bat Zumbleflat, his college room-mate from their
long-gone days at Valhalla State, and now sat
glowering intracranially in the dusk.
"Oh dear," cursed Zeke in his thick guttural
accent still heavily redolent of the Walloon
countryside of his youth. "That's the end of old Bat, I guess."
So he called the police and they all lived happily ever after.
$ CC getword.cpp
$ a.out
11111111112222222222333333333344444444445555555555
12345678901234567890123456789012345678901234567890123456789
It was a dark and stormy night when Zeke
Nergfruner, a heavy set immigrant from
Belgium finally got home after a double
shift as cashier at the horse shop. At
first he didn't notice the lifeless body
draped over the lower limbs of his
second favourite lemon tree.
As he groped clumsily in the front left
pocket of the heavy woollen rain coat
that had been left to him after the
timely death of his slightly short
sighted uncle only three years ago, and
which he was now wearing, lightning
flashed sullenly in the lowering black
night sky.
A glint of light reflected from highly
polished bronze of the antique anvil
which had crushed the head of Bat
Zumbleflat, his college room mate from
their long gone days at Valhalla State,
and now sat glowering intracranially in
the dusk.
"Oh dear," cursed Zeke in his thick
guttural accent still heavily redolent
of the Walloon countryside of his youth.
"That's the end of old Bat, I guess." So
he called the police and they all lived
happily ever after.
The End.
$ cat getword.cpp
...
... all the same as before, except:
...
void main(void)
{ FILE * thefile=fopen("sample.txt", "r");
if (thefile==NULL)
{ cout < "Can't read sample.txt\n";
exit(1); }
cout < " 11111111112222222222333333333344444444445555555555\n";
cout < "12345678901234567890123456789012345678901234567890123456789\n\n";
string wordlist[maxsize];
while (1)
{ int numwords = read_paragraph(wordlist, thefile);
if (numwords == -1)
{ cout < "\nThe End.\n";
break; }
int start_word=0;
while (true)
{ solution sol = set_line(wordlist, numwords, start_word, page_width);
int extra_spaces=0;
if (!sol.ended)
extra_spaces=page_width-sol.characters_used;
for (int i=start_word; i<=sol.lastword; i+=1)
{ cout < wordlist[i];
if (extra_spaces>0)
{ cout < " ";
extra_spaces-=1; }
cout < " "; }
cout < "\n";
if (sol.ended)
break;
start_word=sol.lastword+1; }
cout < "\n"; }
fclose(thefile); }
$ CC getword.cpp
$ a.out
11111111112222222222333333333344444444445555555555
12345678901234567890123456789012345678901234567890123456789
It was a dark and stormy night when Zeke
Nergfruner, a heavy set immigrant from
Belgium finally got home after a double
shift as cashier at the horse shop. At
first he didn't notice the lifeless body
draped over the lower limbs of his
second favourite lemon tree.
As he groped clumsily in the front left
pocket of the heavy woollen rain coat
that had been left to him after the
timely death of his slightly short
sighted uncle only three years ago, and
which he was now wearing, lightning
flashed sullenly in the lowering black
night sky.
A glint of light reflected from highly
polished bronze of the antique anvil
which had crushed the head of Bat
Zumbleflat, his college room mate from
their long gone days at Valhalla State,
and now sat glowering intracranially in
the dusk.
"Oh dear," cursed Zeke in his thick
guttural accent still heavily redolent
of the Walloon countryside of his youth.
"That's the end of old Bat, I guess." So
he called the police and they all lived
happily ever after.
The End.