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
|
#include<iostream>
#include<cstring>
#include<fstream>
using namespace std;
void getCandidatesName(ifstream& inp, string cNames[], int noOfRows)
{
for (int i = 0; i < noOfRows; i++)
inp >> cNames[i];
}
void sortCandidatesName(string cNames[], int noOfRows)
{
int firstOutOfOrder, location;
string temp;
for (firstOutOfOrder = 1; firstOutOfOrder < noOfRows;
firstOutOfOrder++)
if (cNames[firstOutOfOrder] < cNames[firstOutOfOrder - 1])
{
temp = cNames[firstOutOfOrder];
location = firstOutOfOrder;
do
{
cNames[location] = cNames[location - 1];
location--;
}
while (location > 0 && cNames[location - 1] > temp);
cNames[location] = temp;
}
}
void initialize(int vbRegion[][NO_OF_REGIONS], int tVotes[],
int noOfRows)
{
for (int i = 0; i < noOfRows; i++)
for (int j = 0; j < NO_OF_REGIONS; j++)
vbRegion[i][j] = 0;
for (int i = 0; i < noOfRows; i++)
tVotes[i] = 0;
}
int binSearch(string cNames[], int noOfRows, string name)
{
int first, last, mid;
bool found;
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 + vbRegion[i][j];
}
void printHeading()
{
cout << " -------------Election Results------------"
<< "----" << endl << endl;
cout << "Candidate Votes" << endl;
cout << "Name Region1 Region3 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;
}
int getTotalVotes;
for(int i = 0; i < 6; i++)
cout << getTotalVotes <<endl;
cout<<"Winner: "<<firstN<<lastN<<","<<"\t"<< "Votes Received: "<< winLoc<<endl;
cout <<"Total votes polled:" << getTotalVotes;
}
int main()
{
ifstream inp;
getCandidatesName(ifstream& inp, string cNames[], int noOfRows);
sortCandidatesName(string cNames[], int noOfRows);
initialize(int vbRegion[][NO_OF_REGIONS], int tVotes[],
int noOfRows);
binSearch(string cNames[], int noOfRows, string name);
processVotes(ifstream& inp, string cNames[],
int vbRegion[][NO_OF_REGIONS], int noOfRows);
addRegionsVote(int vbRegion[][NO_OF_REGIONS],
int tVotes[], int noOfRows);
printHeading();
printResults(candidatesName, voteByRegion,
totalVotes, NO_OF_CANDIDATES);
}
|