C++ Creating and Editing Functinons

profileaalaz
GreenEggsHamStarter.zip

CMakeLists.txt

cmake_minimum_required(VERSION 3.15) project(A6) set(CMAKE_CXX_STANDARD 14) add_executable(A6 main.cpp functions.cpp)

input/aliceChapter1.txt

Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, "and what is the use of a book," thought Alice, "without pictures or conversation?" So she was considering in her own mind (as well as she could, for the hot day made her feel very sleepy and stupid), whether the pleasure of making a daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly a White Rabbit with pink eyes ran close by her. There was nothing so VERY remarkable in that; nor did Alice think it so VERY much out of the way to hear the Rabbit say to itself, "Oh dear! Oh dear! I shall be late!" (when she thought it over afterwards, it occurred to her that she ought to have wondered at this, but at the time it all seemed quite natural); but when the Rabbit actually TOOK A WATCH OUT OF ITS WAISTCOAT-POCKET, and looked at it, and then hurried on, Alice started to her feet, for it flashed across her mind that she had never before seen a rabbit with either a waistcoat-pocket, or a watch to take out of it, and burning with curiosity, she ran across the field after it, and fortunately was just in time to see it pop down a large rabbit-hole under the hedge.

input/greeneggsandham.txt

I AM SAM. I AM SAM. SAM I AM. THAT SAM I AM! THAT SAM I AM! I DO NOT LIKE THAT SAM I AM! DO WOULD YOU LIKE GREEN EGGS AND HAM? I DO NOT LIKE THEM, SAM I AM. I DO NOT LIKE GREEN EGGS AND HAM. WOULD YOU LIKE THEM HERE OR THERE? I WOULD NOT LIKE THEM HERE OR THERE. I WOULD NOT LIKE THEM ANYWHERE. I DO NOT LIKE GREEN EGGS AND HAM. I DO NOT LIKE THEM, SAM I AM. WOULD YOU LIKE THEM IN A HOUSE? WOULD YOU LIKE THEM WITH A MOUSE? I DO NOT LIKE THEM IN A HOUSE. I DO NOT LIKE THEM WITH A MOUSE. I DO NOT LIKE THEM HERE OR THERE. I DO NOT LIKE THEM ANYWHERE. I DO NOT LIKE GREEN EGGS AND HAM. I DO NOT LIKE THEM, SAM I AM. WOULD YOU EAT THEM IN A BOX? WOULD YOU EAT THEM WITH A FOX? NOT IN A BOX. NOT WITH A FOX. NOT IN A HOUSE. NOT WITH A MOUSE. I WOULD NOT EAT THEM HERE OR THERE. I WOULD NOT EAT THEM ANYWHERE. I WOULD NOT EAT GREEN EGGS AND HAM. I DO NOT LIKE THEM, SAM I AM. WOULD YOU? COULD YOU? IN A CAR? EAT THEM! EAT THEM! HERE THEY ARE. I WOULD NOT, COULD NOT, IN A CAR. YOU MAY LIKE THEM. YOU WILL SEE. YOU MAY LIKE THEM IN A TREE! I WOULD NOT, COULD NOT IN A TREE. NOT IN A CAR! YOU LET ME BE. I DO NOT LIKE THEM IN A BOX. I DO NOT LIKE THEM WITH A FOX. I DO NOT LIKE THEM IN A HOUSE. I DO NOT LIKE THEM WITH A MOUSE. I DO NOT LIKE THEM HERE OR THERE. I DO NOT LIKE THEM ANYWHERE. I DO NOT LIKE GREEN EGGS AND HAM. I DO NOT LIKE THEM, SAM I AM. A TRAIN! A TRAIN! A TRAIN! A TRAIN! COULD YOU, WOULD YOU ON A TRAIN? NOT ON TRAIN! NOT IN A TREE! NOT IN A CAR! SAM! LET ME BE! I WOULD NOT, COULD NOT, IN A BOX. I WOULD NOT, COULD NOT, WITH A FOX. I WILL NOT EAT THEM IN A HOUSE. I WILL NOT EAT THEM HERE OR THERE. I WILL NOT EAT THEM ANYWHERE. I DO NOT EAT GREEN EGGS AND HAM. I DO NOT LIKE THEM, SAM I AM. SAY! IN THE DARK? HERE IN THE DARK! WOULD YOU, COULD YOU, IN THE DARK? I WOULD NOT, COULD NOT, IN THE DARK. WOULD YOU COULD YOU IN THE RAIN? I WOULD NOT, COULD NOT IN THE RAIN. NOT IN THE DARK. NOT ON A TRAIN. NOT IN A CAR. NOT IN A TREE. I DO NOT LIKE THEM, SAM, YOU SEE. NOT IN A HOUSE. NOT IN A BOX. NOT WITH A MOUSE. NOT WITH A FOX. I WILL NOT EAT THEM HERE OR THERE. I DO NOT LIKE THEM ANYWHERE! YOU DO NOT LIKE GREEN EGGS AND HAM? I DO NOT LIKE THEM, SAM I AM. COULD YOU, WOULD YOU, WITH A GOAT? I WOULD NOT, COULD NOT WITH A GOAT! WOULD YOU, COULD YOU, ON A BOAT? I COULD NOT, WOULD NOT, ON A BOAT. I WILL NOT, WILL NOT, WITH A GOAT. I WILL NOT EAT THEM IN THE RAIN. NOT IN THE DARK! NOT IN A TREE! NOT IN A CAR! YOU LET ME BE! I DO NOT LIKE THEM IN A BOX. I DO NOT LIKE THEM WITH A FOX. I WILL NOT EAT THEM IN A HOUSE. I DO NOT LIKE THEM WITH A MOUSE. I DO NOT LIKE THEM HERE OR THERE. I DO NOT LIKE THEM ANYWHERE! I DO NOT LIKE GREEN EGGS AND HAM! I DO NOT LIKE THEM, SAM I AM. YOU DO NOT LIKE THEM. SO YOU SAY. TRY THEM! TRY THEM! AND YOU MAY. TRY THEM AND YOU MAY, I SAY. SAM! IF YOU LET ME BE, I WILL TRY THEM. YOU WILL SEE. SAY! I LIKE GREEN EGGS AND HAM! I DO! I LIKE THEM, SAM I AM! AND I WOULD EAT THEM IN A BOAT. AND I WOULD EAT THEM WITH A GOAT... AND I WILL EAT THEM, IN THE RAIN. AND IN THE DARK. AND ON A TRAIN. AND IN A CAR. AND IN A TREE. THEY ARE SO GOOD, SO GOOD, YOU SEE! SO I WILL EAT THEM IN A BOX. AND I WILL EAT THEM WITH A FOX. AND I WILL EAT THEM IN A HOUSE. AND I WILL EAT THEM WITH A MOUSE. AND I WILL EAT THEM HERE AND THERE. SAY! I WILL EAT THEM ANYWHERE! I DO SO LIKE GREEN EGGS AND HAM! THANK YOU! THANK YOU, SAM I AM.

__MACOSX/input/._greeneggsandham.txt

main.cpp

#include <fstream> #include <iostream> #include <string> #include <vector> using namespace std; #include "functions.h" int main() { // open file for parsing string filename = promptUserForFilename(); ifstream fileIn; if( !openFile(fileIn, filename) ) { cerr << "Could not open file \"" << filename << "\"" << endl; cerr << "Shutting down" << endl; return -1; } // read all the words in the file vector<string> allWords = readWordsFromFile( fileIn ); fileIn.close(); cout << "Read in " << allWords.size() << " words" << endl; // clean the words to remove punctuation and convert to uppercase removePunctuation(allWords, "?!.,;:\"()_"); capitalizeWords(allWords); // find only the unique words in the file vector<string> uniqueWords = filterUniqueWords(allWords); cout << "Encountered " << uniqueWords.size() << " unique words" << endl; // put the words in to alphabetical order alphabetizeWords( uniqueWords ); // count the number of occurrences of each word vector<unsigned int> uniqueWordCounts = countUniqueWords(allWords, uniqueWords); // pretty print the unique words and their corresponding counts printWordsAndCounts(uniqueWords, uniqueWordCounts); // count the occurrences of every letter in the entire text unsigned int letters[26] = {0}; countLetters(letters, allWords); printLetterCounts(letters); // print statistics on word and letter frequencies printMaxMinWord(uniqueWords, uniqueWordCounts); printMaxMinLetter(letters); return 0; }

output/aliceChapter1.out

Enter filename: Read in 253 words Encountered 136 unique words Word Counts 1 : A : 8 2 : ACROSS : 2 3 : ACTUALLY : 1 4 : AFTER : 1 5 : AFTERWARDS : 1 6 : ALICE : 4 7 : ALL : 1 8 : AND : 8 9 : AS : 2 10 : AT : 3 11 : BANK : 1 12 : BE : 2 13 : BEFORE : 1 14 : BEGINNING : 1 15 : BOOK : 2 16 : BURNING : 1 17 : BUT : 3 18 : BY : 2 19 : CLOSE : 1 20 : CONSIDERING : 1 21 : CONVERSATION : 1 22 : CONVERSATIONS : 1 23 : COULD : 1 24 : CURIOSITY : 1 25 : DAISIES : 1 26 : DAISY-CHAIN : 1 27 : DAY : 1 28 : DEAR : 2 29 : DID : 1 30 : DO : 1 31 : DOWN : 1 32 : EITHER : 1 33 : EYES : 1 34 : FEEL : 1 35 : FEET : 1 36 : FIELD : 1 37 : FLASHED : 1 38 : FOR : 2 39 : FORTUNATELY : 1 40 : GET : 1 41 : GETTING : 1 42 : HAD : 3 43 : HAVE : 1 44 : HAVING : 1 45 : HEAR : 1 46 : HEDGE : 1 47 : HER : 8 48 : HOT : 1 49 : HURRIED : 1 50 : I : 1 51 : IN : 4 52 : INTO : 1 53 : IS : 1 54 : IT : 11 55 : ITS : 1 56 : ITSELF : 1 57 : JUST : 1 58 : LARGE : 1 59 : LATE : 1 60 : LOOKED : 1 61 : MADE : 1 62 : MAKING : 1 63 : MIND : 2 64 : MUCH : 1 65 : NATURAL : 1 66 : NEVER : 1 67 : NO : 1 68 : NOR : 1 69 : NOTHING : 2 70 : OCCURRED : 1 71 : OF : 8 72 : OH : 2 73 : ON : 2 74 : ONCE : 1 75 : OR : 4 76 : OUGHT : 1 77 : OUT : 3 78 : OVER : 1 79 : OWN : 1 80 : PEEPED : 1 81 : PICKING : 1 82 : PICTURES : 2 83 : PINK : 1 84 : PLEASURE : 1 85 : POP : 1 86 : QUITE : 1 87 : RABBIT : 4 88 : RABBIT-HOLE : 1 89 : RAN : 2 90 : READING : 1 91 : REMARKABLE : 1 92 : SAY : 1 93 : SEE : 1 94 : SEEMED : 1 95 : SEEN : 1 96 : SHALL : 1 97 : SHE : 7 98 : SISTER : 2 99 : SITTING : 1 100 : SLEEPY : 1 101 : SO : 3 102 : STARTED : 1 103 : STUPID : 1 104 : SUDDENLY : 1 105 : TAKE : 1 106 : THAT : 3 107 : THE : 13 108 : THEN : 1 109 : THERE : 1 110 : THINK : 1 111 : THIS : 1 112 : THOUGHT : 2 113 : TIME : 2 114 : TIRED : 1 115 : TO : 9 116 : TOOK : 1 117 : TROUBLE : 1 118 : TWICE : 1 119 : UNDER : 1 120 : UP : 1 121 : USE : 1 122 : VERY : 4 123 : WAISTCOAT-POCKET : 2 124 : WAS : 5 125 : WATCH : 2 126 : WAY : 1 127 : WELL : 1 128 : WHAT : 1 129 : WHEN : 3 130 : WHETHER : 1 131 : WHITE : 1 132 : WITH : 3 133 : WITHOUT : 1 134 : WONDERED : 1 135 : WORTH : 1 136 : WOULD : 1 Letter Counts A : 87 B : 25 C : 28 D : 44 E : 125 F : 19 G : 19 H : 73 I : 78 J : 1 K : 13 L : 30 M : 9 N : 60 O : 79 P : 14 Q : 1 R : 66 S : 58 T : 118 U : 31 V : 10 W : 28 X : 0 Y : 16 Z : 0 Least Frequent Word: ACTUALLY 1 ( 0.395%) Most Frequent Word: THE 13 ( 5.138%) Least Frequent Letter: X 0 ( 0.000%) Most Frequent Letter: E 125 ( 12.112%)

output/greeneggsandham.out

Enter filename: Read in 795 words Encountered 50 unique words Word Counts 1 : A : 56 2 : AM : 16 3 : AND : 25 4 : ANYWHERE : 8 5 : ARE : 2 6 : BE : 4 7 : BOAT : 3 8 : BOX : 7 9 : CAR : 7 10 : COULD : 14 11 : DARK : 7 12 : DO : 37 13 : EAT : 23 14 : EGGS : 11 15 : FOX : 7 16 : GOAT : 4 17 : GOOD : 2 18 : GREEN : 11 19 : HAM : 11 20 : HERE : 11 21 : HOUSE : 8 22 : I : 83 23 : IF : 1 24 : IN : 40 25 : LET : 4 26 : LIKE : 44 27 : MAY : 4 28 : ME : 4 29 : MOUSE : 7 30 : NOT : 82 31 : ON : 6 32 : OR : 8 33 : RAIN : 4 34 : SAM : 19 35 : SAY : 5 36 : SEE : 4 37 : SO : 5 38 : THANK : 2 39 : THAT : 3 40 : THE : 11 41 : THEM : 59 42 : THERE : 9 43 : THEY : 2 44 : TRAIN : 8 45 : TREE : 6 46 : TRY : 4 47 : WILL : 18 48 : WITH : 18 49 : WOULD : 27 50 : YOU : 34 Letter Counts A : 207 B : 14 C : 21 D : 112 E : 277 F : 8 G : 39 H : 142 I : 216 J : 0 K : 53 L : 125 M : 120 N : 186 O : 253 P : 0 Q : 0 R : 85 S : 59 T : 241 U : 90 V : 0 W : 71 X : 14 Y : 57 Z : 0 Least Frequent Word: IF 1 ( 0.126%) Most Frequent Word: I 83 ( 10.440%) Least Frequent Letter: J 0 ( 0.000%) Most Frequent Letter: E 277 ( 11.590%)