-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast01.cpp
More file actions
264 lines (205 loc) · 7.14 KB
/
ast01.cpp
File metadata and controls
264 lines (205 loc) · 7.14 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/* Name: Kristy Nguyen, NSHE ID 5006243601, CS 202-1002, Assignment 1
* Description: Processes a file containing formatted data for local
election
* Input: elections1.txt and elections2.txt
* Output: list of candidates, sorted list by votes, lowest scoring
candidate, highest scoring candidate, rounded pScore list */
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <iomanip>
#include <cmath>
using namespace std;
const int ARR_SIZE = 100;
// Declare array of structs for Candidate
struct Candidate {
string first; // first name
string last; // last name
int votes; // votes or score
double pScore; // pScore (percentage)
};
// Function prototypes
void readFile(string, Candidate[]);
void displayList(Candidate[]);
void sortByVotes(Candidate[]);
void displayCandidate(Candidate);
Candidate getWinner(Candidate[]);
Candidate getLast(Candidate[]);
void calculateScores(Candidate[]);
void roundScores(Candidate[]);
int main(int argc, char *argv[]) {
Candidate candidates[ARR_SIZE]; // declare array
// execute if arg count is greater than 1 (>./a.out)
if (argc>1) {
readFile(argv[1], candidates); // reads file specified in argv[1]
}
//calculateScores(candidates); RETURNS SEG FAULT
displayList(candidates);
//sortByVotes(candidates); RETURNS SEG FAULT
return 0;
}
/* function_identifier: reads input file, fills candidates[] array
* parameters: string inFileName and candidates array
* return value: none */
void readFile(string inFileName, Candidate candidates[]) {
ifstream inFile;
Candidate candidate; // define variable named candidate
int valid = 0; // counter to check valid
string electionLine; // to store line from txt file
inFile.open(inFileName.c_str()); // open file
if (!inFile) { // error for opening file
cout << endl;
cout << "Error - Can't open the file named " << inFileName;
cout << endl << endl;
return;
}
getline(inFile, electionLine); // read line from file to string
while (inFile) { // while file is open...
while (1) {
// If the checks are not found, break out of loop
if (electionLine.find("F=") == string::npos)
break;
if (electionLine.find(",L=") == string::npos)
break;
if (electionLine.find(",V=") == string::npos) {
if (electionLine.find(", V=") == string::npos)
break;
}
// Returns first name part of string
candidates[valid].first = electionLine.substr(
electionLine.find("F=")+2,
electionLine.find(",L=")-(electionLine.find("F=")+2) );
// Returns last name and votes of string if no space found
if (electionLine.find(",V") != string::npos) {
// Returns last name of string
candidates[valid].last = electionLine.substr(
electionLine.find(",L=")+3,
electionLine.find(",V=")-(electionLine.find(",L=")+3) );
// Returns votes of string
candidates[valid].votes = stoi( electionLine.substr(
electionLine.find(",V=")+3, 4 ) );
}
// Returns last name and votes of string if space found
if (electionLine.find(", V") != string::npos) {
// Returns last name of string
candidates[valid].last = electionLine.substr(
electionLine.find(",L=")+3,
electionLine.find(", V=")-(electionLine.find(",L=")+3) );
// Returns votes of string
candidates[valid].votes = stoi( electionLine.substr(
electionLine.find(", V=")+4, 4 ) );
}
valid++; // increment valid count for next candidate
break; // break out of loop
}
getline(inFile, electionLine); // read next line into string
}
}
/* function_identifier: prints array of Candidate structs
* parameters: candidates array
* return value: none */
void displayList(Candidate candidates[]) {
int recordTotal; // counter for candidates
// Candidate list output message
cout << "ALL CANDIDATES:" << endl;
cout << setw(10) << "First";
cout << setw(12) << "Last";
cout << setw(12) << "Votes";
cout << setw(12) << "% Score" << endl;
cout << "---------- ---------- ---------- ----------" << endl;
// For loop iterates through the entire array to count candidates
for (int i = 0; i < ARR_SIZE; i++) {
if (candidates[i].first != "")
recordTotal++;
}
// Prints first name, last name, votes, and pScore for each candidate
for (int j = 0; j < recordTotal; j++) {
cout << setw(10) << candidates[j].first;
cout << setw(12) << candidates[j].last;
cout << setw(12) << candidates[j].votes;
//cout << setw(12) << setprecision(2) << candidates[j].pScore;
cout << endl;
}
cout << endl << endl;
}
/* function_identifier: sorts candidates array by number of votes
* parameters: candidates array
* return value: none */
void sortByVotes(Candidate candidates[]) {
int recordTotal; // counter for candidates
// Output message for sorted candidates
cout << "ALL CANDIDATES:" << endl;
cout << setw(10) << "First";
cout << setw(12) << "Last";
cout << setw(12) << "Votes";
cout << setw(12) << "% Score" << endl;
cout << "---------- ---------- ---------- ----------" << endl;
// For loop to count candidates
for (int i = 0; i < ARR_SIZE; i++) {
if (candidates[i].first != "")
recordTotal++;
}
// Attempt to bubble sort
for (int x = 0; x < recordTotal; x++) {
for (int y = 0; y < recordTotal-x-1; y++) {
if (candidates[y+1].votes > candidates[y].votes) {
swap(candidates[y].votes, candidates[y+1].votes);
}
}
}
// Attempt to print out sorted list
for (int j = 0; j < recordTotal; j++) {
cout << setw(10) << candidates[j].first;
cout << setw(12) << candidates[j].last;
cout << setw(12) << candidates[j].votes;
//cout << setw(12) << setprecision(2) << candidates[j].pScore;
cout << endl;
}
cout << endl << endl;
}
/* function_identifier: prints complete info about candidate
* parameters: cand variable
* return value: none
void displayCandidate(Candidate cand) {
}
*/
/* function_identifier: returns candidate with highest score
* parameters: candidates array
* return value: ?
Candidate getWinner(Candidate candidates[]) {
}
*/
/* function_identifier: returns candidate with lowest score
* parameters: candidates array
* return value: ?
Candidate getLast(Candidate candidates[]) {
}
*/
/* function_identifier: calculates percentage score
* parameters: candidates array
* return value: none */
void calculateScores(Candidate candidates[]) {
int recordTotal; // counter for candidates
double sum = 0; // to calculate sum
// for loop to count candidates
for (int i = 0; i < ARR_SIZE; i++) {
if (candidates[i].first != "")
recordTotal++;
}
// (Attempt) For loop to find sum of votes
for (int j = 0; j < recordTotal; j++) {
sum += candidates[j].votes;
}
// (Attempt) For loop to calculate pScore
for (int n = 0; n < recordTotal; n++) {
candidates[n].pScore = (candidates[n].votes / sum)*.100;
}
}
/* function_identifier: rounds or updates pScore
* parameters: candidates array
* return value: none
void roundScores(Candidate candidates[]) {
}
*/