Saturday , 18 January 2025

CS304 assignment no 03 Spring 2013 Solution has been uploaded

IDEA Solution

 

#include <iostream>
#include <string.h>
using namespace std;

//Sign Class
class Sign
{
private:
//variable for holding sign
char* name;
public:
//Constructor
Sign()
{
name = NULL;
}
void setName(char* n)
{
if(this->name != NULL)
delete name;
name = new char[strlen(n) + 1];
strcpy(name, n);
}
char* getName()
{
return this->name;
}

};

class Candidate
{
private:
//variables for holding name, total votes and Sign of candidate
char* name;
int votes_obtain;
Sign* sign;

public:
Candidate()
{
name = NULL;
sign = NULL;
votes_obtain = 0;

}
Candidate(const char* n, int votes, Sign* sign)
{

setName(n);

setSign(sign);
setVotesObtain(0);
}
void setName(const char* n)
{
if(this->name != NULL)
delete name;
name = new char[strlen(n) + 1];
strcpy(name, n);
//this->index = 0;
}
const char* getName()
{
return this->name;
}
//method for setting the total votes of candidate
void setVotesObtain(int votes)
{
votes_obtain += votes;

}
int getVotesObtain()
{
return votes_obtain;
}
int Count_Votes()
{
return votes_obtain;
}
void setSign(Sign* s)
{
this->sign = new Sign();
this->sign->setName(s->getName());
}
Sign* getSign()
{
return this->sign;
}
};

class Result
{
private:
//varible for holding winner name
char* result;
public:
Result()
{
result = NULL;
}
void setResult(const char* res)
{
if(result != NULL)
delete result;
result = new char[strlen(res) + 1];
strcpy(result, res);
}
const char* getResult()
{
return this->result;
}
//This method will get the winner based on total votes cast to candidate
void compileResult(Candidate* candidates[], int totalCandidates)
{
int currentWinner = 0, currenthigherVotes = 0;
for(int i = 0; i < totalCandidates; i++)
{
//if the previous higher votes is lesser then this candidate, then this is current higher voter candidate
if(currenthigherVotes < candidates[i]->Count_Votes())
{
currenthigherVotes = candidates[i]->Count_Votes();
//saving the index of winner candidate
currentWinner = i;

}
//If the current higher votes ties with this candidate flag the current higher votes to 0 or in case no vote cast
else if(currenthigherVotes == candidates[i]->Count_Votes())
{
currenthigherVotes = 0;
}
}
//if current higher votes is not 0 means we have a winner
if(currenthigherVotes != 0)
{
this->setResult(candidates[currentWinner]->getName());
}
//else means the result is tie or no vote cast
else
{
this->setResult(“No Winner”);
}

}

};
class VB_Paper
{
private:
//variables for holding candidates and result
Candidate* candidate[2];
Result result;

public:
VB_Paper()
{
for(int i = 0; i < 2; i++)
{
candidate[i] = NULL;
}

}
VB_Paper(Candidate* can[], int totalCandidates)
{
setCandidates(can, totalCandidates);
}
Result getResult()
{
return this->result;
}

void setCandidates(Candidate* can[], int totalCandidates)
{
for(int i = 0; i < totalCandidates; i++)
{
this->candidate[i] = can[i];
}

}

//method for assigning the vote to candidate of given sign
void setVoteForSign(char* sign)
{
for(int i = 0; i < 2; i++)
{
if(!strcmp(this->candidate[i]->getSign()->getName(), sign))
{
this->candidate[i]->setVotesObtain(1);
break;
}

}
}
//method for displaying the winner and how much vote each candidate got
void GetResult()
{
//compile the result of all candidates
result.compileResult(this->candidate, 2);
cout << “\nWinner is ” << result.getResult();
cout << “\n====================================\n”;
cout << “\nCandidate Name \t Votes Obtained”;

for (int i = 0; i < 2; i++)
{
cout << “\n” << candidate[i]->getName() << ” \t ” << candidate[i]->Count_Votes();
}
}
};
class Contituency
{
private:
//variables for holding contituency no, candidates and VB_Paper
char* contituencyNumber;
Candidate* candidates[5];
VB_Paper* paper;

public:
Contituency()
{
contituencyNumber = NULL;
for(int i = 0; i < 5; i++)
{
candidates[i] = NULL;
}
paper = NULL;
}
Contituency(char* conNo, Candidate* can[], int totalCandidates)
{
setContituencyNumber(conNo);
setCandidates(can, totalCandidates);

}
void setVB_Paper(Candidate* can[], int totalCandidates)
{
paper = new VB_Paper(can, totalCandidates);
}
void setContituencyNumber(char* conNo)
{
if(this->contituencyNumber && this->contituencyNumber != NULL)
delete contituencyNumber;
contituencyNumber = new char[strlen(conNo) + 1];
strcpy(this->contituencyNumber, conNo);
}
const char* getContituencyNumber()
{
return this->contituencyNumber;
}
void setCandidates(Candidate* can[], int totalCandidates)
{
for(int i = 0; i < totalCandidates; i++)
{
this->candidates[i] = can[i];
}
}
Candidate** getCandidates()
{
return this->candidates;
}
//Method for taking input and setting candidates
void start()
{

Candidate* can[2] = {new Candidate(), new Candidate()};

Sign* sign1 = new Sign();
sign1->setName(“Hockey”);
can[0]->setName(“Anwer Abbasi”);
can[0]->setSign(sign1);

Sign* sign2 = new Sign();
sign2->setName(“Ball”);
can[1]->setName(“Eshan Malik”);
can[1]->setSign(sign2);

char input[50];
cout << “\nEnter Contituency Number/Name: “;
cin >> input;

this->setContituencyNumber(input);
this->setCandidates(can, 2);
this->setVB_Paper(can, 2);

cout << “\n\n=========== Contituency: “<<this->contituencyNumber << ” Election” << ” =============” << endl;
cout << “\n\nContesting Candidates are: ” << endl;

cout << “\n====================================================”;
cout << “\nCandidate Name \tSign”;
for (int i = 0; i < 2; i++)
{
cout << “\n” << candidates[i]->getName() << ” \t” << candidates[i]->getSign()->getName();
}
char voteInput[1];
char* signName1 = candidates[0]->getSign()->getName();
char* signName2 = candidates[1]->getSign()->getName();
while(true)
{
cout <<“\n Do you want to cast vote:(Y/N): “;
cin >> voteInput;
if(!strcmp(voteInput, “Y”))
{

cout << “\nSelect your option: ” << ” (1 for ” << signName1 << ” and 2 for ” << signName2 << “)” <<endl;
cout << “\n 1. ” << signName1;
cout << “\n 2. ” << signName2;
cout << endl;
cin >> input;
if(!strcmp(input, “1”))
{
//assign the vote from paper to candidate have a given sign
this->paper->setVoteForSign(signName1);
}
else
{
this->paper->setVoteForSign(signName2);
}
}
else
{
cout << “\n Do you want to view result (Y/N): “;
cin >> input;
if(!strcmp(input, “Y”))
{ //get the result
paper->GetResult();
}
cout << “\n\n Thanks for using program.\n\n”;
break;
}
}

}

};
int main()
{
//Create contituency and start flow
Contituency* con = new Contituency();
con->start();
system(“pause”);
return 0;
}

 

Just copy the above code in dev C++ and press F9 to compile………………..

Check Also

CS304 Unsolved Final Term Paper Spring 2010

Question No: 1    ( Marks: 1 )    – Please choose one  Classes like TwoDimensionalShape and …

Leave a Reply

Your email address will not be published. Required fields are marked *

*