C++ Problem Wiil give good rating for following instructions and correct answer.
ID: 3582394 • Letter: C
Question
C++ Problem
Wiil give good rating for following instructions and correct answer.
Write a program that reads from a text file the last names of five candidates in
a local election and the votes received by each candidate. The program should
then output each candidate’s name, votes received by that candidate, and the
percentage of the total votes received by the candidate. Your program should
also output the winner of the election.
1. read the candidate names and the candidate votes received from a file
2. the file contains at least two entries for each candidate
3. the entries are in no particular order
4. use a binary search tree to store the candidates' names and vote totals
5. A candidate node of the tree is a string and an integer
e.g.
struct Candidate
{
string name;
int votes;
}
An input file may look like
Johnson 2000
Miller 1000
Duffy 1000
Robinson 1500
Sam 900
Sam 500
Miller 2000
Duffy 4000
Johnson 2000
Robinson 1000
Sam 400
Johnson 1000
Miller 1000
Duffy 1000
A sample output is as follows:
Candidate Votes Received % of Total Votes
Johnson 5000 25.91
Miller 4000 20.72
Duffy 6000 31.09
Robinson 2500 12.95
Sam 1800 9.33
Total 19300
The Winner of the Election is Duffy
Explanation / Answer
//pls give good rating
#include <iostream>
#include <string>
#include<fstream>
//for setprecission
#include<iomanip>
#define MAX 100
using namespace std;
// Binary Tree node
struct node
{
string name;
int vote;
struct node* left, *right;
};
typedef struct node Node;
//declare head pointer to Node
Node *head = NULL;
void output_file(Node *root, double percent);
void insert(string s, int v,Node **root);
//function to find maximum number of votes
int printMax(Node *root);
bool search(int v);
int searchString(string s[], int v[], string tmp, int vtmp, int size);
int main()
{
/*int key;
struct node *root = newNode(20);
//insert values into tree for testing
root->left = newNode(9);
root->right = newNode(49);
root->left->left = newNode(5);
root->left->right = newNode(12);
root->left->right->right = newNode(20);
root->right->left = newNode(23);
root->right->right = newNode(52);
root->right->right->left = newNode(50);
printf("Print tree BFS: ");
bfs_traverse(root);
//assign the key value to be searched or taken input from user
key = 10;
printf("The number of nodes having key values less than or equal to %d = %d ", key, smallcount(root, key));*/
//input stream object to read from file
ifstream in;
//declare local variables
string s[MAX],tmp;
int v[MAX], vtmp;
int toatal_vote = 0,count=0;
in.open("vote_in.txt");
//check if files can be opend
if (!in )
{
cout << "Files cant be opened" << endl;
return -1;
}
//read input file till EOF
while (!in.eof())
{
in >> tmp>>vtmp;
//serach string if allready presnt ,then add vote to that
if (searchString(s, v, tmp, vtmp, count) < 0)
{
s[count] = tmp;
v[count] = vtmp;
++count;
}
toatal_vote += vtmp;
}
//insert into Binary search tree
for (int i = 0; i < count; i++)
insert(s[i], v[i], &head);
//calculate percentage
double percent = (double)100 / toatal_vote;
output_file(head, percent);
ofstream out;
out.open("votes_out.txt", ios::app);
//append total number of votes
out << "Total " << toatal_vote << endl;
int max = printMax(head);
//seacrh BST with max vote and print to output file
search(max);
}
bool search(int num) //the function belongs to class 'tree'
{
ofstream out;
out.open("votes_out.txt", ios::app);
Node *temp = head; //'head' is pointer to root node
while (temp != NULL)
{
if (temp->vote == num)
{
//append to file
out << "The Winner of the Election is " << temp->name << endl;
break;
}
if (num>temp->vote)
temp = temp->right;
if (num<temp->vote)
temp = temp->left;
}
if (temp == NULL)
return false;
else if (temp->vote == num)
return true;
}
//insert valuevs to BST,stored values based on vote
void insert(string s, int v, Node **root)
{
if (NULL == *root)
{
*root = new Node;
(*root)->vote = v;
(*root)->name = s;
(*root)->left = NULL;
(*root)->right = NULL;
}
else if (v < (*root)->vote)
{
insert(s,v, &(*root)->left);
}
else if (v >(*root)->vote)
{
insert(s,v, &(*root)->right);
}
}
void output_file(Node *root, double percent)
{
ofstream out;
out.open("votes_out.txt",ios::app);
//check if files can be opend
if (!out)
{
cout << "Files cant be opened" << endl;
return ;
}
if (root == NULL)
return;
double Percentage = double(root->vote) * percent;
cout<<"Name : "<<root->name<<endl;
std::setprecision(2);
out << root->name << " " << root->vote << " " << Percentage << endl;
cout<<"vote :"<< root->vote<<endl;
if (root->left != NULL)
{
cout << "Name : " << root->left->name << endl;
cout << "vote :" << root->left->vote << endl;
//std::setprecision(2);
//out << root->left->name << " " << root->left->vote << " " << Percentage << endl;
}
if (root->right != NULL)
{
cout << "Name : " << root->right->name << endl;
cout << "vote :" << root->right->vote << endl;
//std::setprecision(2);
//out << root->right->name << " " << root->right->vote << " " << Percentage << endl;
}
if (root->left != NULL)
{
output_file(root->left, percent);
}
if (root->right != NULL)
output_file(root->right, percent);
}
//get which node has maximum vote
int printMax(Node *root)
{
static int max = root->vote;
if (root == NULL )
return max ; // Only if the tree contains nothing at all
if (root->vote > max)
{
max = root->vote;
}
printMax(root->right);
//printf("max value = %d ", root->vote);
}
int searchString(string s[], int v[],string tmp,int vtmp, int size)
{
for (int i = 0; i < size; i++)
{
if (s[i] == tmp)
{
v[i] += vtmp;
return 0;
}
}
return -1;
}
---------------------------------------------------------------------------------------------------------
output:
Johnson 5000 25.9067
Miller 4000 20.7254
Robinson 2500 12.9534
Sam 1800 9.32642
Duffy 6000 31.0881
Total 19300
The Winner of the Election is Duffy
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.