-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathword.cpp
More file actions
148 lines (121 loc) · 3.12 KB
/
word.cpp
File metadata and controls
148 lines (121 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/***************
James Bertel
CS111
Final Lab - word.cpp
12-1-17
This function will know the number of A words, number of B words, etc...
It will also tell the the longest and shortest words for each letter.
***************/
#include <iostream>
#include <fstream>
#include "myStrCharFunc.h"
using namespace std;
//variables
const int SIZE = 26; //letters in alphabet
const int MAX = 29; //max letts in word
typedef char cstr[MAX];
struct let //struct for each letter
{
int count;
cstr shortest;
cstr longest;
};
//prototypes
void initializeArray(let array[]);
void readFile(let array[]);
void processWord(cstr word, let array[]);
void printInfoOnOneLetter(const let array[]);
void printInfoOnAllLetters(const let array[]);
int main()
{
//Declare the array
let array[SIZE];
initializeArray(array);
readFile(array);
//Read each word from the input file and process it
int choice;
do{
cout << "\n1. Show information on all the letters." << endl;
cout << "2. Show information on one letter." << endl;
cout << "3. Quit the program" << endl;
cout << "Enter your choice here: ";
cin >> choice;
switch(choice)
{
case 1: printInfoOnAllLetters(array);
break;
case 2: printInfoOneLetter(array);
break;
case 3:
break;
default: cout << "\nInvalid choice. Please try again." << endl;
}
}while(choice != 3);6
cout << "\nThank you for using the system. Goodbye." << endl;
return 0;
}
void initializeArray(let array[])
{
for(int i = 0; i < SIZE; i++)
{
array[i].count = 0;
myStrcpy(array[i].shortest, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
myStrcpy(array[i].longest, "");
}
}
void readFile(let array[])
{
ifstream fin;
cstr word;
fin.open("project2.dat");
if(!fin)
cout << "Input file not found" << endl;
else
{
fin >> word;
while(fin)
{
processWord(word, array);
fin >> word;
}
}
}
void processWord(cstr word, let array[])
{
int slot = (int)(myToUpper(word[0])) - 65;
array[slot].count++;
if(myStrlen(word) >= myStrlen(array[slot].longest))
myStrcpy(array[slot].longest, word);
else if(myStrlen(word) <= myStrlen(array[slot].shortest))
myStrcpy(array[slot].shortest, word);
}
void printInfoOnOneLetter(const let array[])
{
char letter;
cout << "\nWhich letter are you interested in?: ";
cin >> letter;
int slot = ((int)(myToUpper(letter))) - 65;
cout << "\nFrequency: " << array[slot].count << endl;
if(array[slot].count !=0)
{
cout << "Shortest: " << array[slot].shortest << endl;
cout << "Longest: " << array[slot].longest << endl;
}
}
void printInfoOnAllLetters(const let array[])
{
cout << endl;
for(int i = 0; i < SIZE; i++)
{
cout << (char)('A' + i) << " "; //outputs each letter
cout << array[i].count << ": "; //outputs each letter count
if(array[i].count != 0)
{ //outputs star for each letter count
for(int s = 0; s < array[i].count; s++)
cout << "*";
cout << " " << array[i].longest << " - " << array[i].shortest << endl;
} //^^ outputs longest and shortest words for each letter
else
cout << endl;
}
}