The StatePrimary class uses a vector to store a list of candidate information. H
ID: 3568544 • Letter: T
Question
The StatePrimary class uses a vector to store a list of candidate information. How can I store this info in a linked list instead?
=========================
candicateCollections.cpp
=========================
#include "candidateCollections.h"
#include "states.h"
#include <iostream>
using namespace std;
void CandidateCollection::addCandidate (const Candidate& cand)
{
candidates.push_back(cand);
}
/**
* Find the candidate with the indicated name. Returns the array
* index for the candidate if found, -1 if it cannot be found.
*/
int CandidateCollection::findCandidate (std::string name)
{
int result = -1;
for (int i = 0; i < candidates.size() && result == -1; ++i)
if (candidates[i].getName() == name)
result = i;
return result;
}
void CandidateCollection::read(istream& in)
{
// Read the candidate names
string line;
getline (in, line);
while (line != "")
{
Candidate c (line);
addCandidate (c);
getline (in, line);
}
}
=========================
candidates.cpp
=========================
#include "candidates.h"
#include "states.h"
#include <iostream>
using namespace std;
Candidate::Candidate (string candidateName)
{
name = candidateName;
delegatesWon = 0;
}
/**
* Print the report line for the indicated candidate
*/
void Candidate::report (ostream& out, int totalDelegates)
{
int requiredToWin = (2 * totalDelegates + 2) / 3; // Note: the +2 rounds up
if (delegatesWon >= requiredToWin)
out << "* ";
else
out << " ";
out << delegatesWon << " " << name << endl;
}
=========================
primaries.cpp
=========================
#include "candidates.h"
#include "candidateCollections.h"
#include "states.h"
#include <fstream>
using namespace std;
void primaryElection (istream& input, ostream& output);
int main(int argc, char** argv)
{
// Main routine - if command parameters give two filenames,
// read from the first and write to the second. If not,
// read from standard input and write to standard output
if (argc == 3)
{
ifstream in (argv[1]);
ofstream out (argv[2]);
primaryElection (in, out);
}
else
primaryElection (cin, cout);
return 0;
}
// Your code goes below here. I don't recommend that you make any changes
// above these lines except possibly to the list of #include's
void readAndProcessStates (istream& in,
CandidateCollection& candidates,
int& totalDelegates)
{
totalDelegates = 0;
// Read states one at a time
int nDelegatesInState;
while (in >> nDelegatesInState)
{
StatePrimary state (nDelegatesInState);
// Read all votes in this state
int nVotes;
in >> nVotes;
while (nVotes >= 0)
{
string name, line;
in >> name;
getline (in, line);
name = name + line;
state.addCandidate (name, nVotes);
in >> nVotes;
}
// Add state's delegates to each candidate
state.assignDelegatesToCandidates();
for (int i = 0; i < candidates.numberOfCandidates(); ++i)
{
int nDel = state.getNumberOfDelegates(candidates.get(i).getName());
candidates.get(i).addDelegatesWon(nDel);
totalDelegates += nDel;
}
}
}
void generateReport (ostream& out,
CandidateCollection& candidates,
int totalDelegates)
{
// All states have been processed, generate the report.
for (int i = 0; i < candidates.numberOfCandidates(); ++i)
{
candidates.get(i).report (out, totalDelegates);
}
}
void primaryElection (istream& input, ostream& output)
{
CandidateCollection candidates;
candidates.read (input);
int totalDelegates;
readAndProcessStates(input, candidates, totalDelegates);
generateReport (output, candidates, totalDelegates);
}
=========================
candidateCollections.h
=========================
#ifndef CANDIDATECOLLECTIONS_H
#define CANDIDATECOLLECTIONS_H
#include <iostream>
#include <vector>
#include "candidates.h"
class CandidateCollection
{
std::vector<Candidate> candidates;
public:
void addCandidate (const Candidate& candidate);
int numberOfCandidates() const {return candidates.size();}
Candidate& get (int index) {return candidates[index];}
/**
* Find the candidate with the indicated name. Returns the array index for
* the candidate if found, -1 if it cannot be found.
*/
int findCandidate (std::string name);
/**
* Read a list of candidate names from an input stream,
* one per line, stopping at an empty line
*/
void read (std::istream& input);
};
#endif
=========================
candicates.h
=========================
#ifndef CANDIDATES_H
#define CANDIDATES_H
#include <iostream>
#include <string>
/**
* Information on the political candidates in the primaries.
*/
class Candidate {
std::string name;
int delegatesWon;
public:
Candidate (std::string candidateName);
std::string getName() {return name;}
void setName(std::string nam) {name = nam;}
int getNumberOfDelegatesWon() {return delegatesWon;}
void setNumberOfDelegatesWon(int nDelegates) {delegatesWon = nDelegates;}
void addDelegatesWon(int nDelegates) {delegatesWon += nDelegates;}
/**
* Print the report line for the indicated candidate
*/
void report (std::ostream& out, int totalDelegatesAllStates);
};
#endif
=========================
states.cpp
=========================
#include "candidates.h"
#include "states.h"
#include <iostream>
using namespace std;
/**
* Information on the state primaries
*/
StatePrimary::StatePrimary(int numDelegatesForState)
{
numDelegates = numDelegatesForState;
}
// Record the fact that this candidate participated
// in the election and how many votes he/she received.
void StatePrimary::addCandidate (std::string name, int nVotes)
{
CandidateInfo info = {name, nVotes, 0};
stateCandidates.push_back (info);
}
/**
* For the most recently read primary, determine how many delegates have
* been won by each candidate.
*/
void StatePrimary::assignDelegatesToCandidates ()
{
int totalVotes = 0;
for (int i = 0; i < stateCandidates.size(); ++i)
totalVotes += stateCandidates[i].votesInThisState;
sortByVotes();
int remainingDelegates = numDelegates;
for (int i = 0; i < stateCandidates.size(); ++i)
{
int nDel = (numDelegates * stateCandidates[i].votesInThisState + (totalVotes-1)) / totalVotes;
if (nDel > remainingDelegates)
nDel = remainingDelegates;
stateCandidates[i].delegatesWonInThisState = nDel;
remainingDelegates -= nDel;
}
}
// get number of delegates won by this candidate. (If the candidate
// did not participate, return 0.)
int StatePrimary::getNumberOfDelegates (std::string name)
{
for (int i = 0; i < stateCandidates.size(); ++i)
if (stateCandidates[i].name == name)
return stateCandidates[i].delegatesWonInThisState;
return 0;
}
void StatePrimary::sortByVotes()
{
for (int firstOutOfOrder = 1; firstOutOfOrder < stateCandidates.size();
++firstOutOfOrder)
{
CandidateInfo value = stateCandidates[firstOutOfOrder];
int toBeMoved = firstOutOfOrder - 1;
while (toBeMoved >= 0
&& value.votesInThisState > stateCandidates[toBeMoved].votesInThisState) {
stateCandidates[toBeMoved+1] = stateCandidates[toBeMoved];
--toBeMoved;
}
// Insert the new value
stateCandidates[toBeMoved+1] = value;
}
}
=========================
states.h
=========================
#ifndef STATES_H
#define STATES_H
#include <iostream>
#include <string>
#include <vector>
#include "candidates.h"
/**
* Information on the state primaries
*/
class StatePrimary {
public:
StatePrimary(int numDelegatesForState);
// Record the fact that this candidate participated
// in the election and how many votes he/she received.
void addCandidate (std::string name, int nVotes);
// Compute the candidates won by each candidate in this state.
// (Call this after all candidates have been added,
// and before requesting the delegates won by any candidate.)
void assignDelegatesToCandidates();
// get number of delegates won by this candidate. (If the candidate
// did not participate, return 0.)
int getNumberOfDelegates (std::string name);
// ==============================================
// All information below this line is hidden
private:
int numDelegates;
struct CandidateInfo {
std::string name;
int votesInThisState;
int delegatesWonInThisState;
};
std::vector<CandidateInfo> stateCandidates;
void sortByVotes();
};
#endif
Explanation / Answer
Nicely Stated Question. (y)
So all you want to do is use link list instead of vector to store your CandidateInfo type.
Hence you can start by declaring a structure then use that structure to create link list which can hold this.Below is how you will declare that structure.
//==========================================
struct linklistBlackBox
{
CandidateInfo data;
linklistBlackBox *next;
};
//======================
//Now we hav declared structure. we will see how to use it in linklist
#include <list> //use this header file to use in build link list
linkList.push_back(); //use this method similarly to what you were using for vector .
//But now you have link list .keep that in mind
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.