您的位置:首页 > 其它

组合练习之投票

2016-07-07 21:16 423 查看
Person类

#ifndef Person_hpp
#define Person_hpp
#include <stdio.h>
#include<iostream>
using namespace std;
class Person
{
protected:
char *_name;
int _age;
int _salary;
int id;
static int _id;
static int _totalPerson;
public:
Person(){}
Person(char *name,int age,int salary);
void SetAge(int newage);
void SetSalary(int newsalary);
char* getName() const;
int getAge() const ;
int getSalary() const;
int getID() const;
~Person();
Person& operator=(const Person& c);

friend ostream& operator<<(ostream& out,const Person& p1);

};
*#endif /* Person_hpp */*


#include "Person.hpp"
#include<string.h>
int Person::_id = 0;
int Person::_totalPerson = 0;
Person::Person(char *name,int age,int salary)
{
_name = new char[strlen(name)+1];
strcpy(_name,name);
_age = age;
_salary = salary;
_id++;
id = _id;

_totalPerson++;
}
void Person::SetAge(int newage)
{
_age = newage;
}
void Person::SetSalary(int newsalary)
{
_salary = newsalary;

}
char* Person::getName() const
{
return _name;
}
int Person::getAge() const
{
return _age;
}
int Person::getSalary() const
{
return _salary;
}
int Person::getID() const
{
return _id;
}
ostream& operator<<(ostream& out,const Person& p1)
{
out<<"姓名:"<<p1._name<<endl;
out<<"年龄:"<<p1._age<<endl;
out<<"工资:"<<p1._salary<<endl;
cout<<"ID:"<<p1.id<<endl;
return out;
}
Person::~Person()
{
if(_name != NULL)
{
delete []_name;
_name = NULL;
}
cout<<"Person类析构函数调用!"<<endl;
}
Person& Person::operator=(const Person& c)
{
if(this != &c)
{
delete []this->_name;
this->_name = new char[strlen(c.getName())+1];
strcpy(this->_name, c.getName());
_age = c.getAge();
_salary = c.getSalary();
}
return *this;
}


PersonSet类

#ifndef PersonSet_hpp
#define PersonSet_hpp
#include "Person.hpp"
class PersonSet
{
public:
PersonSet (int initial_size = 4);
~ PersonSet (void);
void Add(Person* element);
Person & NextElement();
Person & RemoveElement();
Person & RemoveElement( int index );
Person& operator[](int index) const;
Person& operator[](int index);
int Size() const;
void Print() const;
void Reset()
{
_index = 0;
}
private:
Person ** _elements;
int      _capacity;  //volume of the set
int      _size;          //number of elements in the set
int      _index;
};
#endif /* PersonSet_hpp */


include “PersonSet.hpp”

PersonSet::PersonSet (int initial_size)
{
_capacity = initial_size;
_size = 0;
_index = 0;
_elements = new Person*[_capacity];
}
PersonSet::~ PersonSet (void)
{
if(_elements != NULL)
{
delete []_elements;
_elements = NULL;
}
cout<<"PersonSet析构函数"<<endl;
}
void PersonSet::Add(Person* element)
{
if(_size == _capacity)
{
Person **temp = _elements;
_capacity *= 2;
_elements = new Person*[_capacity];
for(int i = 0; i < _size;i++)
{
_elements[i] = temp[i];
}
delete []temp;

}
_elements[_size++] = element;
}
Person & PersonSet::NextElement()
{
if(_size == 0)
{
cout<<"No Person"<<endl;
}
else
{
if(_index >= _size)
{
_index = 0;
}
}
return *_elements[_index++];
}
Person & PersonSet::RemoveElement()
{
_size--;
Person *p = _elements[_size--];
if(_size < _capacity / 2)
{
Person **temp = _elements;
_capacity /= 2;
_elements = new Person*[_capacity];
for(int i = 0; i < _size;i++)
{
_elements[i] = temp[i];
}
delete []temp;
}
return *p;
}
Person & PersonSet::RemoveElement( int index )
{
Person *p = _elements[_index];
for(int i = index ; i < _size; i++)
{
_elements[i] = _elements[i+1];
}
if(_size < _capacity / 2)
{
Person **temp = _elements;
_capacity /= 2;
_elements = new Person*[_capacity];
for(int i = 0; i < _size;i++)
{
_elements[i] = temp[i];
}
delete []temp;
}
return *p;
}
Person& PersonSet::operator[](int index) const
{
return *_elements[index];
}
Person& PersonSet::operator[](int index)
{
return *_elements[index];
}
int PersonSet::Size() const
{
return _size;
}
void PersonSet::Print() const
{
for(int i = 0; i < _size; i++)
{
cout<<*_elements[i];
}
}


Voter类

#ifndef Voter_hpp
#define Voter_hpp
#include"Person.hpp"
#include"PersonSet.hpp"
#include "Candidate.hpp"
#include <stdio.h>
#include<iostream>
using namespace std;
class Candidate;
class Voter:public Person
{
private:
int _polingStation;
static int _totalNumVoters;
public:
Voter(char *name,int age,int salary,int polingStation);
void SetPolingStation(int PolingStation);
void print();
friend bool operator==(const Voter& v1,const Voter& v2);
static int Voters()
{
return _totalNumVoters;
}
Person& SelectCandidate(PersonSet& candidate);
void Vote(Candidate& aCandidate);
};
#endif /* Voter_hpp */


#include "Voter.hpp"
int Voter::_totalNumVoters = 0;
Voter::Voter(char *name,int age,int salary,int polingStation):Person(name,age,salary)
{
_polingStation = polingStation;
_totalNumVoters++;
}
void Voter::SetPolingStation(int PolingStation)
{
_polingStation = PolingStation;
}
void Voter::print()
{
cout<<"投票者姓名:"<<this->_name<<endl;
cout<<"投票站编号:"<<this->_polingStation<<endl;
}
bool operator==(const Voter& v1,const Voter& v2)
{
if(v1.getID() == v2.getID())
{
return true;
}
else{
return false;
}
}
Person& Voter::SelectCandidate(PersonSet& candidate)
{
int r = rand() % candidate.Size();
return candidate[r];
}
void Voter::Vote(Candidate& aCandidate)
{
aCandidate.AddVoter(*this);
}


Candidate 类

#ifndef Candidate_hpp
#define Candidate_hpp
#include"Person.hpp"
#include"PersonSet.hpp"
#include "Voter.hpp"
#include <stdio.h>
#include<iostream>
using namespace std;
class Voter;
class Candidate:public Person
{
private:
Voter ** voter;         //存放每个候选人的投票者
static int _numCandidates;
int voter_size;             //实际投票者人数
int _capacity;          //容量
public:
Candidate(char *name,int age,int salary);
int GetVotesNum();
void AddVoter(Voter& avoter);
friend ostream& operator<<(ostream& out,const Candidate& c);
int GetAverageVotersAge();
int GetAverageVotersSalary();
friend bool operator<(const Candidate& c1,const Candidate& c2);
~Candidate();
static int Candidates()
{
return _numCandidates;
}
void Reset()
{
voter_size = 0;
}
};
#endif /* Candidate_hpp */


#include "Candidate.hpp"
int Candidate::_numCandidates = 0;
Candidate::Candidate(char *name,int age,int salary):Person(name,age,salary)
{
_capacity = 4;
voter_size = 0;
voter = new Voter*[_capacity];
}
int Candidate::GetVotesNum()
{
return voter_size;
}
void Candidate::AddVoter(Voter& avoter)
{
if(voter_size == _capacity)
{
Voter **temp = voter;
voter = new Voter*[_capacity * 2];
for(int i = 0; i < voter_size; i++)
{
voter[i] = temp[i];
}
_capacity *= 2;
delete []temp;
}
voter[voter_size++] = &avoter;
}
ostream& operator<<(ostream& out,const Candidate& c)    //打印候选者的投票者
{
if(c.voter_size == 0)
{
out<<"There are no voters!"<<endl;
}
else
{
for(int i = 0; i < c.voter_size; i++)
{
c.voter[i]->print();
}
}
return out;
}
int Candidate::GetAverageVotersAge()
{
int avg_age = 0;
if(voter_size > 0)
{
for(int i = 0; i < voter_size; i++)
{
avg_age += voter[i]->getAge();
}
return avg_age /= voter_size;
}
else
{
cout<<"该候选人没有投票者!";
return 0;
}
}
int Candidate::GetAverageVotersSalary()
{
int avg_salary = 0;
if(voter_size > 0)
{
for(int i = 0; i < voter_size; i++)
{
avg_salary += voter[i]->getSalary();
}
return avg_salary /= voter_size;
}
else
{
cout<<"该候选人没有投票者!";
return 0;
}
}
bool operator<(const Candidate& c1,const Candidate& c2)
{
if(c1.voter_size < c2.voter_size)
{
return true;
}
else
{
return false;
}
}
Candidate:: ~Candidate()
{
if(voter != NULL)
{
delete []voter;
voter = NULL;
}
cout<<"Candidate类析构函数"<<endl;
}


Main函数

#include <iostream>
#include"Person.hpp"
#include"PersonSet.hpp"
#include"Candidate.hpp"

void Print(PersonSet &candidate)//  输出候选人所得票数和基本信息
{
for ( int i=0; i<candidate.Size(); i++ )
{
cout<<candidate[i].getName()<<"票数:"<<static_cast<Candidate*>(&candidate[i])->GetVotesNum()<<endl;
cout<<candidate[i];
cout<<"投票者平均年龄:"<<static_cast<Candidate*>(&candidate[i])->GetAverageVotersAge()<<endl;
cout<<"投票者平均工资:"<<static_cast<Candidate*>(&candidate[i])->GetAverageVotersSalary()<<endl;
cout<<endl;
}
}
void Vote(PersonSet &voter,PersonSet &candidate)    //投票
{
cout << "voting = ";
cout<<voter.Size()<<endl;
for(int i = 0; i<voter.Size(); i++)
{
Voter& v = static_cast<Voter&>(voter.NextElement());
// 选择一个候选人
Candidate& chosenCandidate = static_cast< Candidate &>
(v.SelectCandidate( candidate ));
v.Vote(chosenCandidate);
}
}
//迭代Finalpk函数,得到获胜者(若有两人票数相同,则重新对这两人投票)
Candidate& Finalpk(PersonSet &voter,PersonSet &candidate)
{
int num = 0;
Candidate* winner = static_cast<Candidate*>(&candidate[0]);
for ( int i=1; i<candidate.Size(); i++ )
{
if ( *winner < *(static_cast<Candidate*>(&candidate[i]) ))
{
winner = static_cast<Candidate*>(&candidate[i]);
}
}
for(int i=0; i<candidate.Size(); i++)
{
if((*winner).GetVotesNum() == static_cast<Candidate*>(&candidate[i])->GetVotesNum())
num++;
}
if(num != 1  )
{
cout<<"终极pk"<<endl;
PersonSet can;
for(int i=0; i<candidate.Size(); i++)
{
if((*winner).GetVotesNum() == static_cast<Candidate*>(&candidate[i])->GetVotesNum())
{
can.Add(&candidate[i]);
}
}
for(int i=0; i<candidate.Size(); i++)
static_cast<Candidate*>(&candidate[i])->Reset();
Vote(voter,can);
Finalpk(voter, can);
Print(can);
}
return *winner;

}
int main(int argc, const char * argv[]) {
Voter *v1 = new Voter((char*)"John", 20, 6000,1);
Voter *v2 = new Voter((char*)"Frank", 26, 30000,2);
Voter *v3 = new Voter((char*)"Anna", 20, 199600,1);
Voter *v4 = new Voter((char*)"James", 67, 9600,2);
Voter *v5 = new Voter((char*)"Jane", 40, 29600,3);
PersonSet voter;
voter.Add(v1);
voter.Add(v2);
voter.Add(v3);
voter.Add(v4);
voter.Add(v5);
voter.Add(v1);
voter.Add(v2);
voter.Add(v3);
voter.Add(v4);
voter.Add(v5);
voter.Add(v4);
voter.Add(v5);
Candidate *c1 = new Candidate((char*)"April", 67, 9600 );
Candidate *c2 = new Candidate((char*)"May", 40, 29600);
Candidate *c3 = new Candidate((char*)"June", 40, 29600);
srand((unsigned int)time(NULL));
PersonSet candidate;
candidate.Add(c1);
candidate.Add(c2);
candidate.Add(c3);
//投票
voter.Reset();
cout << "voting = ";
for(int i = 0; i<voter.Size(); i++)
{
Voter& v = static_cast<Voter&>(voter.NextElement());
// 选择一个候选人
Candidate& chosenCandidate = static_cast< Candidate &>
(v.SelectCandidate( candidate ));
v.Vote(chosenCandidate);
}
// 判断是否所有投票者都进行了投票
cout<<Voter::Voters()<<endl;
if (voter.Size() != Voter::Voters())
{
cout <<" Not all voters voted !!!"<< endl ;
}
else
{
cout <<"Success: 100% voted"<< endl ;
}
Candidate *winner = &Finalpk(voter,candidate);
cout << " the winner is : " << (*winner).getName()<<endl;
cout<<(*winner)<<endl;
delete v1;
delete v2;
delete v3;
delete v4;
delete v5;
delete c1;
delete c2;
delete c3;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: