-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDriver.cpp
More file actions
229 lines (203 loc) · 6.84 KB
/
Driver.cpp
File metadata and controls
229 lines (203 loc) · 6.84 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Created 30 November 2009
#include "Driver.h"
#include "CipherList.h"
#include <iostream>
#include <iomanip>
int const NUM = 100;
int const TOP = 5;
//constructor and destructor defined in header
/* "programMenu":
* center of activity for int-based ciphertext */
void Driver::programMenu(const int param[], const int SIZE)
{
int userInput = -1; //user input for switch
int textNumbers[NUM]; //00 through 99 //user for frequency analysis
//set array to zeros by default
for (int i = 0; i < NUM; i++)
textNumbers[i] = 0;
//set number frequency
for (int i = 0; i < SIZE; i++)
textNumbers[param[i]] += 1;
//switch menu
while (userInput != 0)
{
cout << " Select action for list\n" << endl;
cout << " (1) Display text block " << endl;
cout << " (2) Display list (list order) " << endl;
cout << " (3) Display list (freq order) " << endl;
cout << " (4) Display digraphs " << endl;
cout << " (5) Display trigraphs " << endl;
cout << " (0) Quit\n" << endl;
cout << " Enter choice: ";
cin >> userInput;
switch (userInput)
{
case 0: //exit driver
break;
case 1:
displayBlock(param, SIZE);
break;
case 2:
displayByOrder(textNumbers, SIZE);
break;
case 3:
displayByFreq(textNumbers, SIZE);
break;
case 4:
//displayDigraphs(param, SIZE);
break;
case 5:
//displayTrigraphs(param, SIZE);
break;
case 88:
//debugging
displayFreqArray(textNumbers);
break;
default:
cout << " invalid input " << endl;
break;
}
}
}
/* "displayBlock":
* will display ciphertext block 25 integers per line */
void Driver::displayBlock(const int param[], const int SIZE) const
{
for (int i = 0; i < SIZE; i++)
{
if (i == 0) //adds space at beginning of block (for formatting)
cout << " ";
if (i != 0 && i % 5 == 0) //adds tab between sets of five (for formatting)
cout << " ";
if (i != 0 && i % 20 == 0) //skip to next line (for formatting)
cout << "\n" << " ";
if (param[i] < 10) //puts leading zero on single digits (for formatting)
cout << "0" << param[i] << " ";
else
cout << param[i] << " ";
}
cout << "\n";
}
/* "displayByOrder":
* will display two columns of integers and one column with doubles. The first
* column represents the ciphertext digits, the second column represents the
* number of times the ciphertext digit appears in the block, and the third
* column is the ciphertext digit's percentile frequency. */
void Driver::displayByOrder(const int param[], const int SIZE) const
{
float freq;
cout << "\n Ciphertext | Number | Frequency " << endl;
for (int i = 0; i < NUM; i++)
{
freq = 0.0;
if (param[i] != 0)
{
freq = ((float)param[i] / (float)SIZE) * 100;
if(i < 10)
{
cout << setw(6) << "0" << i << setw(10) << param[i] << setw(14)
<< setprecision(3) << freq << "%" << endl;
}
else
{
cout << setw(7) << i << setw(10) << param[i] << setw(14)
<< setprecision(3) << freq << "%" << endl;
}
}
}//end 'for' loop
}
/* "displayByFreq":
* will display two columns of integers and one column with doubles. The first
* column represents the ciphertext digits, the second column represents the
* number of times the ciphertext digit appears in the block, and the third
* column is the ciphertext digit's percentile frequency. */
void Driver::displayByFreq(const int param[], const int SIZE)
{
/* 1) Search through "frequency" to find highest frequency integer
* 2) set matching index of "selected" to false
* 3) Display ciphertext number, frequency, and result of:
cout << setprecision(4) << (frequency/SIZE) */
float freq;
int indexHolder = 0; //holds index that currently contains highest frequency
bool selected[NUM]; //index values align with param index values
setToFalse(selected);
cout << "\n Ciphertext | Number | Frequency " << endl;
for (int j = 0; j < NUM; j++)
{
//reset indexHolder and freq
indexHolder = 0;
freq = 0.0;
//Part 1: find highest frequency among remaining frequencies
for (int k = 1; k < NUM; k++)
{
if (param[k] > param[indexHolder] && selected[k] == false)
indexHolder = k;
}
if (param[indexHolder] > 0)
{
//highest frequency found
selected[indexHolder] = true;
freq = ((float)param[indexHolder] / (float)SIZE) * 100;
//Part 2: display ciphertext number, frequency, & frequency percentage
if (indexHolder < 10) //adds leading zero (for formatting)
{
cout << setw(6) << "0" << indexHolder << setw(10) << param[indexHolder]
<< setw(14) << setprecision(3) << freq << "%" << endl;
}
else
{
cout << setw(7) << indexHolder << setw(10) << param[indexHolder]
<< setw(14) << setprecision(3) << freq << "%" << endl;
}
}
}//end 'for' loop
}
void Driver::displayDigraphs(const int param[], const int SIZE)
{
/* 1) Display top 5 most frequent numbers
* 2) Prompt user for number to associate with digraphs
* 2.5) Check if valid
* 3) Walk through 'param' array searching for user number
* 4) When found, make two nodes one for left, one for right
* 5) Search list for similiar nodes
* 5.5) If similar node found, link to it's "down" pointer
* 5.5.5) This will be used with a counter while printing
* 6) When finished, hand execution to private printer
* 7) Delete linked list */
bool selected[NUM];
setToFalse(selected);
int topFive[TOP];
int indexHolder = 0;
for (int i = 0; i < TOP; i++)
{
indexHolder = 0;
for (int k = 1; k < NUM; k++)
{
if (param[k] > param[indexHolder] && selected[k] == false)
indexHolder = k;
}
}
}
void Driver::displayTrigraphs(const int param[], const int SIZE)
{
}
void Driver::setToFalse(bool param[])
{
for (int i = 0; i < NUM; i++)
param[i] = false;
}
/*** for debugging //remove when complete ***/
void Driver::displayFreqArray(const int param[])
{
cout << " Index | Frequency " << endl;
for (int i = 0; i < NUM; i++)
{
if (param[i] != 0)
{
if (param[i] < 10) //add space for formatting
cout << " " << i << " " << param[i] << endl;
else
cout << " " << i << " " << param[i] << endl;
}
}
}