Description
Construct C++ program that inputs a text file, scans for words and builds a list of the words in the file and a count of the number of occurrences of each word. For example
This is a test It’s only a test
produces
A 2
IS 1
IT’S 1
ONLY 1
TEST 2
THIS 1
Expect no punctuation except apostrophes such as in “it’s”. Convert all alphabetic characters to upper case.
The data structure required for this program is a linked-list of structures. This is the structure:
struct wordstruct {
string word;
short count;
wordstruct *next;
};
You may make it a class if you wish.
The link-list class (CWordList) must insert each word in correct alphabetical order. Do not sort the list but insert into the list at the correct position.
I suggest your write a search method to determine if a word is in the list or not and a separate insert method. You will need a print method as well.
The algorithm is :
- get a word from the file
- determine if it is in the list
- if so, increment the counter for that word (count in wordstruct)
- if not find the correct location in the list and add the word
- repeat until eof
Only after all the file is processed should the program output the list and count.
Turn in the class CWordList to BeachBoard dropbox.
Try you code on this:
To be or not to be that is the question Whether tis nobler in the mind to suffer
The slings and arrows of outrageous fortune Or to take arms against a sea of troubles And by opposing end them To die to sleep No more and by a sleep to say we end The heartache and the thousand natural shocks That flesh is heir to Tis a consummation Devoutly to be wished To die to sleep To sleep perchance to dream ay there’s the rub For in that sleep of death what dreams may come When we have shuffled off this mortal coil Must give us pause There’s the respect That makes calamity of so long life For who would bear the whips and scorns of time The oppressor’s wrong the proud man’s contumely The pangs of despised love the law’s delay The insolence of office and the spurns That patient merit of the unworthy takes When he himself might his quietus make With a bare bodkin Who would fardels bear To grunt and sweat under a weary life But that the dread of something after death The undiscovered country from whose bourn No traveller returns puzzles the will And makes us rather bear those ills we have Than fly to others that we know not of Thus conscience does make cowards of us all And thus the native hue of resolution Is sicklied o’er with the pale cast of thought And enterprise of great pitch and moment With this regard their currents turn awry And lose the name of action
produces:
A 5
ACTION 1
AFTER 1
AGAINST 1
ALL 1
AND 12
ARMS 1
ARROWS 1
AWRY 1
AY 1
BARE 1
BE 3
BEAR 3
BODKIN 1
BOURN 1
BUT 1
BY 2
CALAMITY 1
CAST 1
COIL 1
COME 1
CONSCIENCE 1
CONSUMMATION 1
CONTUMELY 1
COUNTRY 1
COWARDS 1
CURRENTS 1
DEATH 2
DELAY 1
DESPISED 1
DEVOUTLY 1
DIE 2
DOES 1
DREAD 1
DREAM 1
DREAMS 1
END 2
ENTERPRISE 1
FARDELS 1
FLESH 1
FLY 1
FOR 2
FORTUNE 1
FROM 1
GIVE 1
GREAT 1
GRUNT 1
HAVE 2
HE 1
HEARTACHE 1
HEIR 1
HIMSELF 1
HIS 1
HUE 1
ILLS 1
IN 2
INSOLENCE 1
IS 3
KNOW 1
LAW’S 1
LIFE 2
LONG 1
LOSE 1
LOVE 1
MAKE 2
MAKES 2
MAN’S 1
MAY 1
MERIT 1
MIGHT 1
MIND 1
MOMENT 1
MORE 1
MORTAL 1
MUST 1
NAME 1
NATIVE 1
NATURAL 1
NO 2
NOBLER 1
NOT 2
O’ER 1
OF 15
OFF 1
OFFICE 1
OPPOSING 1
OPPRESSOR’S 1
OR 2
OTHERS 1
OUTRAGEOUS 1
PALE 1
PANGS 1
PATIENT 1
PAUSE 1
PERCHANCE 1
PITCH 1
PROUD 1
PUZZLES 1
QUESTION 1
QUIETUS 1
RATHER 1
REGARD 1
RESOLUTION 1
RESPECT 1
RETURNS 1
RUB 1
SAY 1
SCORNS 1
SEA 1
SHOCKS 1
SHUFFLED 1
SICKLIED 1
SLEEP 5
SLINGS 1
SO 1
SOMETHING 1
SPURNS 1
SUFFER 1
SWEAT 1
TAKE 1
TAKES 1
THAN 1
THAT 7
THE 21
THEIR 1
THEM 1
THERE’S 2
THIS 2
THOSE 1
THOUGHT 1
THOUSAND 1
THUS 2
TIME 1
TIS 2
TO 15
TRAVELLER 1
TROUBLES 1
TURN 1
UNDER 1
UNDISCOVERED 1
UNWORTHY 1
US 3
WE 4
WEARY 1
WHAT 1
WHEN 2
WHETHER 1
WHIPS 1
WHO 2
WHOSE 1
WILL 1
WISHED 1
WITH 3
WOULD 2
WRONG 1
Press any key to continue . . .