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
|
//*************************************************************
// Author: D.S. Malik
//
// This program processes voting data for student council
// president's post. It outputs each candidate's name and the
// votes they received. The name of the winner is also printed.
//*************************************************************
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
const int NO_OF_CANDIDATES = 6;
const int NO_OF_REGIONS = 4;
void printHeading();
void initialize(int vbRegion[][NO_OF_REGIONS], int tVotes[],
int noOfRows);
void getCandidatesName(ifstream& inp, string cNames[],
int noOfRows);
void sortCandidatesName(string cNames[], int noOfRows);
int binSearch(string cNames[], int noOfRows, string name);
void processVotes(ifstream& inp, string cNames[],
int vbRegion[][NO_OF_REGIONS],
int noOfRows);
void addRegionsVote(int vbRegion[][NO_OF_REGIONS],
int tVotes[], int noOfRows);
void printResults(string cNames[],
int vbRegion[][NO_OF_REGIONS],
int tVotes[], int noOfRows);
int main()
{
//Declare variables; Step 1
string candidatesName[NO_OF_CANDIDATES];
int votesByRegion[NO_OF_CANDIDATES][NO_OF_REGIONS];
int totalVotes[NO_OF_CANDIDATES];
ifstream inp;
inp.open("candData.txt"); //Step 2
if (!inp) //Step 3
{
cout << "Input file (candData.txt) does "
<< "not exist." << endl;
return 1;
}
getCandidatesName(inp, candidatesName,
NO_OF_CANDIDATES); //Step 4
sortCandidatesName(candidatesName,
NO_OF_CANDIDATES); //Step 5
inp.close(); //Step 6
inp.clear(); //Step 6
inp.open("voteData.txt"); //Step 7
if (!inp) //Step 8
{
cout << "Input file (voteData.txt) does "
<< "not exist." << endl;
return 1;
}
initialize(votesByRegion, totalVotes,
NO_OF_CANDIDATES); //Step 9
void getCandidatesName(ifstream& inp, string cNames[],
int noOfRows);
void sortCandidatesName(string cNames[], int noOfRows);
int binSearch(string cNames[], int noOfRows, string name);
processVotes(inp, candidatesName,
votesByRegion, NO_OF_CANDIDATES); //Step 10
addRegionsVote(votesByRegion, totalVotes,
NO_OF_CANDIDATES); //Step 11
printHeading(); //Step 12
printResults(candidatesName,votesByRegion,
totalVotes, NO_OF_CANDIDATES); //Step 13
return 0;
}
//Place the definitions of the functions initialize,
//getCandidatesName, sortCandidatesName, binSearch,
//processVotes, addRegionsVote, printHeading, and
//printResults here.
void initialize(int vbRegion[][NO_OF_REGIONS], int tVotes[],
int noOfRows)
{
int i, j;
for (i = 0; i < noOfRows; i++)
for (j = 0; j < NO_OF_REGIONS; j++)
vbRegion[i][j] = 0;
for (i = 0; i < noOfRows; i++)
tVotes[i] = 0;
}
void getCandidatesName(ifstream& inp, string cNames[],
int noOfRows)
{
int i;
for (i = 0; i < noOfRows; i++)
inp >> cNames[i];
}
void sortCandidatesName(string cNames[], int noOfRows)
{
int i, j;
int min;
//selection sort
for (i = 0; i < noOfRows - 1; i++)
{
min = i;
for (j = i + 1; j < noOfRows; j++)
if (cNames[j] < cNames[min])
min = j;
cNames[i].swap(cNames[min]);
}
}
int binSearch(string cNames[], int noOfRows, string name)
{
int first, last, mid;
bool found;
first = 0;
last = noOfRows - 1;
found = false;
while (!found && first <= last)
{
mid = (first + last) / 2;
if (cNames[mid] == name)
found = true;
else if (cNames[mid] > name)
last = mid - 1;
else
first = mid + 1;
}
if (found)
return mid;
else
return -1;
}
void processVotes(ifstream& inp, string cNames[],
int vbRegion[][NO_OF_REGIONS],
int noOfRows)
{
string candName;
int region;
int noOfVotes;
int loc;
inp >> candName >> region >> noOfVotes;
while (inp)
{
loc = binSearch(cNames, noOfRows, candName);
if (loc != -1)
vbRegion[loc][region - 1] = vbRegion[loc][region - 1]
+ noOfVotes;
inp >> candName >> region >> noOfVotes;
}
}
void addRegionsVote(int vbRegion[][NO_OF_REGIONS],
int tVotes[], int noOfRows)
{
int i, j;
for (i = 0; i < noOfRows; i ++)
for (j = 0; j < NO_OF_REGIONS; j++)
tVotes[i] = tVotes[i] + vbRegion[i][j];
}
void printHeading()
{
cout << " -------------Election Results----------"
<< "----" << endl << endl;
cout << "Candidate Votes" << endl;
cout << "Name Region1 Region2 Region3 "
<< " Region4 Total" << endl;
cout << "----- ------- ------- ------- "
<< " ------- -------" << endl;
}
void printResults(string cNames[],
int vbRegion[][NO_OF_REGIONS],
int tVotes[], int noOfRows)
{
int i, j;
int largestVotes = 0;
int winLoc = 0;
int sumVotes = 0;
for (i = 0; i < noOfRows; i++)
{
if (largestVotes < tVotes[i])
{
largestVotes = tVotes[i];
winLoc = i;
}
sumVotes = sumVotes + tVotes[i];
cout << left;
cout << setw(9) << cNames[i] << " ";
cout << right;
for (j = 0; j < NO_OF_REGIONS; j++)
cout << setw(8) << vbRegion[i][j] << " ";
cout << setw(6) << tVotes[i] << endl;
}
cout << endl << endl << "Winner: " << cNames[winLoc]
<< ", Votes Received: " << tVotes[winLoc]
<< endl << endl;
cout << "Total votes polled: " << sumVotes << endl;
}
|