C++投票规则如下: 1. 每个投票者只可以投一次票. 投票者通过生成的随机数在第一个到最后一个候选人之间选择一个候选人来进行投票。 2. 每一个候选人都保存着投票者给他们投票的记录。 3. 选举之后, 拥有最高投票数的候选人当选。 ---------------------------Person类的Person.h----------------------------- // Person.h // The second work for c++ // // Created by student on 15-8-6. // Copyright (c) 2015年 com.origin. All rights reserved. // #ifndef __The_second_work_for_c____Person__ #define __The_second_work_for_c____Person__ #include <iostream> using namespace std; class Person { protected: int _id; //标识列 char* _name; int _age; double _salary; static int _totalPersons; //参加这次选举的总人数,包括选民和候选人 public: Person(); ~Person(); Person(Person& p); Person(char* name,int age,double salary); void SetSalary(double salary); void printPerson()const; void setAge(int age); void setSalary(double salary); void setName(char* name); int getId(); char* getName(); int getAge(); double getSalary(); friend ostream& operator<<(ostream& out,const Person& p); }; #endif /* defined(__The_second_work_for_c____Person__) */ ---------------------------Person类的Person.cpp--------------------------- // Person.cpp // The second work for c++ // // Created by student on 15-8-6. // Copyright (c) 2015年 com.origin. All rights reserved. // #include "Person.h" #include <iostream> using namespace std; //参与投票者总人数 int Person::_totalPersons =0; Person::Person(){ _name = new char[4]; strcpy(_name,"Tom"); _age = 0; _salary = 0; _id = 0; } Person::~Person(){ if(_name != NULL){ delete []_name; _name = NULL; } } Person::Person(Person& p){ ++_totalPersons; _id = _totalPersons; _age = p._age; _salary = p._salary; strcpy(_name, p._name); } Person::Person(char* name,int age,double salary):_id(++_totalPersons){ //_id呈递增增长 _name = new char[strlen(name)+1]; strcpy(_name,name); _age = age; _salary = salary; } void Person::SetSalary(double salary){ _salary = salary; } ostream& operator<<(ostream& out,const Person& p){ out<<p._id<<" "; out<<p._name<<" "; out<<p._age<<" "; out<<p._salary<<" "; //out<<p._totalPersons<<endl; return out; } void Person::printPerson()const{ cout<<_id<<" "; cout<<_name<<" "; cout<<_age<<" "; cout<<_salary<<endl; } /*****set and get methods*****/ void Person::setAge(int age){ _age = age; } void Person::setSalary(double salary){ _salary = salary; } void Person::setName(char* name){ strcpy(_name, name); } int Person::getId(){ return _id; } char* Person::getName(){ return _name; } int Person::getAge(){ return _age; } double Person::getSalary(){ return _salary; } --------------------------PersonSet容器Voter.h--------------------------- // PersonSet.h // The second work for c++ // // Created by student on 15-8-6. // Copyright (c) 2015年 com.origin. All rights reserved. // #ifndef __The_second_work_for_c____PersonSet__ #define __The_second_work_for_c____PersonSet__ #include <iostream> #include "Person.h" using namespace std; class PersonSet { private: Person** _element; int _capacity; int _size; int _index; public: PersonSet(); ~PersonSet(); PersonSet(PersonSet& p); public: void AddPerson(Person& p); Person& nextElement(); Person& removeElement(); Person& removeElement(int index); int Size()const; void print(); void Reset(); public: Person& operator[](int x); //重载下标运算符 const Person& operator[](int x)const; //重载不可修改的下标运算符 const PersonSet& operator=(const PersonSet& p); }; #endif /* defined(__The_second_work_for_c____PersonSet__) */ --------------------------PersonSet容器Voter.cpp--------------------------- // PersonSet.cpp // The second work for c++ // // Created by student on 15-8-6. // Copyright (c) 2015年 com.origin. All rights reserved. // #include "PersonSet.h" #include <iostream> using namespace std; PersonSet::PersonSet(){ _capacity = 4; _size = 0; _index = 0; _element = new Person*[_capacity]; } PersonSet::~PersonSet(){ if(_element != NULL){ delete []_element; _element = NULL; } } void PersonSet::AddPerson(Person& p){ if(_size == _capacity){ Person** temp = _element; _element = new Person*[_capacity*2]; for(int i =0;i<_size;i++){ _element[i] = temp[i]; } _capacity*=2; delete []temp; temp = NULL; } _element[_size++] = &p; } Person& PersonSet::nextElement(){ //对容器里元素进行遍历 if(_index>_size){ _index=0; } return *(_element[_index++]); } Person& PersonSet::removeElement(){ //delete the last element if(_size == 0){ cout<<"object is null, don‘t delete"<<endl; exit(1); } _size--; Person* p =_element[_size]; if(_size<_capacity/2){ cout<<"有一半以上空间未被使用,开辟新空间,释放多余空间\n"; Person** temp = _element; _element = new Person*[_capacity/2]; for(int i=0;i<_size;i++){ _element[i]=temp[i]; } _capacity/=2; delete [] temp; temp = NULL; } return *p; } Person& PersonSet::removeElement(int index){ if(index >_size){ cout<<"input error !"<<endl; exit(1); } _size--; Person* p = _element[index-1]; for(int i =index-1;i<_size ;i++){ _element[i] = _element[i+1]; } if(_size<_capacity/2){ cout<<"有一半以上空间未被使用,开辟新空间,释放多余空间\n"; Person** temp = _element; _element = new Person*[_capacity/2]; for(int i=0;i<_size;i++){ _element[i]=temp[i]; } _capacity/=2; delete [] temp; temp = NULL; } return *p; } int PersonSet::Size()const{ return _size ; } void PersonSet::print(){ cout<<"当前元素个数:"<<_size<<" ,"<<"容器大小:"<<_capacity<<endl; for(int i=0;i<_size;i++){ (*_element[i]).printPerson(); cout<<endl; } } void PersonSet::Reset(){ _index =0; } Person& PersonSet::operator[](int x){ //重载下标运算符 return *(_element[x]); } const Person& PersonSet::operator[](int x)const{ //重载不可修改的下标运算符 return *(_element[x]); } PersonSet::PersonSet(PersonSet& p){ _capacity=p._capacity; _size=p._size; _index=p._index; _element=new Person*[_size]; for(int i=0;i<_size;i++){ _element[i]=p._element[i]; } } const PersonSet& PersonSet::operator=(const PersonSet& p){ if(this != &p){ delete [] _element; _capacity = p._capacity; _size = p._size; _index = p._index; _element = NULL; } _element = new Person*[p._size]; for(int i =0;i<_size;i++){ _element[i] = p._element[i]; } return *this; } ------------------------------选民类的Voter.h---------------------------- // Voter.h // The second work for c++ // // Created by student on 15-8-6. // Copyright (c) 2015年 com.origin. All rights reserved. // #ifndef __The_second_work_for_c____Voter__ #define __The_second_work_for_c____Voter__ //voter选举人 #include <iostream> #include "Candidate.h" #include "PersonSet.h" #include "Person.h" class Candidate; using namespace std; class Voter:public Person { protected: int _polingStation;//站台号 public: static int _totalNumVoters; //参加选举的人数 public: Voter(char*,int,double,int); Voter(const Voter& avoter); //riend bool operator == (Voter& a,Voter& b); //判断两个对象是否相同.==是比较运算符。 friend ostream& operator<<(ostream& os,const Voter& v); public: void setPolingStation(int PolingStation); int getPolingStation()const; void print() const; static int Voters(); //静态方法,返回选民人数 Person& SelectCadidate(PersonSet& cadidates); //产生随机获取候选人 void Vote(Candidate& aCandidate ); //开始选举 }; #endif /* defined(__The_second_work_for_c____Voter__) */ // // Voter.cpp // The second work for c++ // // Created by student on 15-8-6. // Copyright (c) 2015年 com.origin. All rights reserved. // ------------------------------选民类的Voter.cpp---------------------------- #include "Voter.h" #include <iostream> using namespace std; int Voter::_totalNumVoters =0; Voter::Voter(char* name,int age,double salary,int polingStation):Person(name,age,salary){ _polingStation = polingStation; _totalNumVoters++; //总参加选举人数++ } Voter::Voter(const Voter& avoter){ _polingStation = avoter._polingStation; } //bool operator == (Voter& a,Voter& b){ //判断两个对象是否相同 // bool aa = true; // if(a._id != b._id){ // aa = false; // } // return aa; //} void Voter::setPolingStation(int PolingStation){ _polingStation = PolingStation; } int Voter::getPolingStation()const{ return _polingStation; } void Voter::print() const{ Person::printPerson(); cout<<_polingStation<<endl; } //静态方法,返回选举人个数 int Voter::Voters(){ return _totalNumVoters; } Person& Voter::SelectCadidate(PersonSet& candidates){ //候选人容器对象 int s = candidates.Size(); //获取候选人容器中候选人的人数 int r = rand()%s; // 产生随机数0-(s-1),使用rand()函数会产生一个0~32767之间的数字 return candidates[r]; //返回候选人容器里面的一个随机候选人对象 } void Voter::Vote(Candidate& aCandidate ){ //开始选举 aCandidate.AddVoter(*this); //把当前对象添加到候选人对象的选民容器 //这个函数的作用是对候选人进行选举。首先上面return返回的对象是一个Person类型的引用,通过静态转换static_cast< Candidate &>,将person类型的引用转化为Candidate类型的引用,然后将这个引用传给aCandidate,然后这个对象调用AddVoter方法将当前这个候选人作为实参传递给形参avoter, } ostream& operator<<(ostream& out,const Voter& v){ out<<v._name<<" "; out<<v._age<<" "; out<<v._salary<<" "; out<<v._polingStation; return out; } ---------------------------候选人类的Candidate.h-------------------------- // Candidate.h // The second work for c++ // // Created by student on 15-8-6. // Copyright (c) 2015年 com.origin. All rights reserved. // #ifndef __The_second_work_for_c____Candidate__ #define __The_second_work_for_c____Candidate__ #include <iostream> #include "Voter.h" #include "Person.h" #include "PersonSet.h" using namespace std; class Voter; class Candidate:public Person { protected: PersonSet _ps; //.定义一个PersonSet对象,用于存储选民 double average_age;//平均年龄 double average_salary;//平均薪水 static int _numCandidates;//.统计比赛中有多少候选人参加 public: PersonSet& getPersonSet(); public: Candidate(); Candidate(char*,int,double); //Candidate(char* name,int age,double salary); int getVotesNum();//得到每个候选人的票数 void AddVoter(Voter& aVoter); //.得到选举人所给的票,记住给当前候选人投票人的信息 double getAverageVotersAge(); double getAverageVotersSalary(); static int Candidates(); //.获取所有的候选人个数 public: friend ostream& operator<<(ostream& out,Candidate& c); //.输出姓名 friend bool operator<(Candidate&,Candidate&); //.重载运算符,判断是否a<b }; #endif /* defined(__The_second_work_for_c____Candidate__) */ ---------------------------候选人类的Candidate.cpp-------------------------- // Candidate.cpp // The second work for c++ // // Created by student on 15-8-6. // Copyright (c) 2015年 com.origin. All rights reserved. // #include "Candidate.h" #include <iostream> using namespace std; int Candidate:: _numCandidates =0; // 对静态成员候选总人数进行初始化 PersonSet& Candidate::getPersonSet(){ //PersonSet对象的get方法 return _ps; } Candidate::Candidate():Person(){ } Candidate::Candidate(char* name,int age,double salary):Person(name,age,salary){ _numCandidates++; } int Candidate::getVotesNum(){ //得到每个候选人的票数 int size = _ps.Size(); return size; } void Candidate::AddVoter(Voter& aVoter){ //得到选举人所给的票,记住给当前候选人投票人的信息 _ps.AddPerson(aVoter); //把选举人所投给候选人的信息添加到候选人容器里面。 //然后通过personSet定义的对象_ps(_ps是用来存贮选民的),调用AddPerson方法来实现将当前这个选民信息添加到候选人容器里面。 } double Candidate::getAverageVotersAge(){ int sumAge = 0; int numvoters = Voter::_totalNumVoters; //参加选举的人数 sumAge+= _ps.nextElement().getAge(); return sumAge/numvoters; } double Candidate::getAverageVotersSalary(){ double sumSalary = 0.0; int numvoters =Voter::_totalNumVoters; sumSalary +=_ps.nextElement().getSalary(); return sumSalary/numvoters; } int Candidate::Candidates(){ //获取所有的候选人个数 return _numCandidates; } ostream& operator<<(ostream& out,Candidate& c){ out<<c.getName()<<" "; if(c.getVotesNum()==0){ cout<<"没有投票者 "; } return out; } bool operator<(Candidate& c1,Candidate& c2){ //关系运算符"<"重载,判断是否a<b bool a = true; if(c1.getVotesNum()>c2.getVotesNum()){ a = false; } return a; } --------------------------------下面是主函数------------------------------- // // main.cpp // The second work for c++ // // Created by student on 15-8-6. // Copyright (c) 2015年 com.origin. All rights reserved. // #include <iostream> #include "Person.h" #include "Voter.h" #include "Candidate.h" #include "PersonSet.h" using namespace std; int main(int argc, const char * argv[]) { //创建选举人 Voter *v1 = new Voter((char*)"选民1", 20, 6000.0,1); Voter *v2 = new Voter((char*)"选民2", 26, 3000.0,2); Voter *v3 = new Voter((char*)"选民3", 20, 1990.0,1); Voter *v4 = new Voter((char*)"选民4", 67, 9600.0,2); Voter *v5 = new Voter((char*)"选民5", 40, 2960.0,3); Voter *v6 = new Voter((char*)"选民6", 40, 2960.0,3); Voter *v7 = new Voter((char*)"选民7", 40, 2960.0,3); Voter *v8 = new Voter((char*)"选民7", 40, 2960.0,3); //定义三个候选人 Candidate *c1 = new Candidate((char*)"候选者1", 67, 9600 ); Candidate *c2 = new Candidate((char*)"候选者2", 40, 29600); Candidate *c3 = new Candidate((char*)"候选者3", 40, 29600); PersonSet voters; //PersonSet容器创建一个选民对象voters voters.AddPerson(*v1); voters.AddPerson(*v2); voters.AddPerson(*v3); voters.AddPerson(*v4); voters.AddPerson(*v5); voters.AddPerson(*v6); voters.AddPerson(*v7); voters.AddPerson(*v8); PersonSet candidates; candidates.AddPerson(*c1); candidates.AddPerson(*c2); candidates.AddPerson(*c3); srand((unsigned int)time(NULL)); //时间参数作为种子。 int numVoters = voters.Size(); cout<<"------------选民人数------------\n"; cout<<numVoters<<"人"<<endl; cout<<"------------选民信息------------"<<endl; for(int i =0;i<numVoters;i++){ //如果投票者被正确插入到列表中,输出投票者的人数 voters.nextElement().printPerson(); //输出投票者信息 } //voters.Reset(); int numCandidates = candidates.Size(); cout<<"------------候选人人数------------\n"; //如果候选者被正确 cout<<numCandidates<<"人"<<endl; cout<<"------------候选人信息------------"<<endl; for(int i =0;i<numCandidates;i++){ candidates.nextElement().printPerson(); //输出候选人信息 } cout << "------------开始投票选举 ------------ \n"; // voters.Reset();// 重置容器指针 // for(int i =0;i<numVoters;i++){ // Voter& v = static_cast<Voter&>(voters.nextElement()); // // 通过遍历选择选民 // Candidate& chosenCandidate = static_cast< Candidate &>(v.SelectCadidate(candidates)); // // 选民选出候选人 // v.Vote(chosenCandidate); // //选民将自己添加到候选人的选民容器 // } cout<<"&&&&&&&&&&&&&&&&&&&&"<<endl; cout<<Voter::_totalNumVoters<<endl; // 判断是否所有投票者都进行了投票 if (voters.Size() != Voter::_totalNumVoters) { cout <<"有人未投票或弃权 !!!!" << endl ; } else { cout <<"成功: 所用人均已投票!"<< endl ; } // 选择获胜者 // Candidate* winner = static_cast<Candidate*>(&candidates[0]); // for ( int i=0;i<candidates.Size();i++) // { // cout<<*static_cast<Candidate*>(&candidates[i]); // cout<<static_cast<Candidate*>(&candidates[i])->getVotesNum()<<endl; // if ( *winner < *(static_cast<Candidate*>(&candidates[i]) )) // { // winner = static_cast<Candidate*>(&candidates[i]); // } // // // } // cout << "the winner is : "<< *winner<<endl; //如果有两人选票一样,进行第二次选票 /* // if(winner-> getVotesNum() == 2) // { // PersonSet ps; // // for(int i = 0 ; i < candidates.Size() ; i++) // { // Candidate * p; // p = &static_cast<Candidate&>(candidates[i]); // if(p->getVotesNum() == 2) // { // ps.AddPerson(*p); // } // } // for(int j=0;j<numVoters;j++){ // // Voter& v = static_cast<Voter&>(voters.nextElement()); // //投票者随机选择候选人 // Candidate& chosenCandidate = static_cast<Candidate&>(v.SelectCadidate(ps)); // v.Vote(chosenCandidate); // } // cout<<"twice Vote:"<<endl; // Candidate* winner = static_cast<Candidate*>(&ps[0]); // // for ( int i=0; i<ps.Size(); i++ ) // { // if ( *winner < *(static_cast<Candidate*>(&ps[i]) )) // { // winner = static_cast<Candidate*>(&ps[i]); // } // } // cout <<"the winner is : " << *winner<<" "; // cout<<"票数:"<<winner->getVotesNum()-2<<endl; // // }else{ // cout << "the winner is : " << *winner<<" "; // cout<<"票数:"<<winner->getVotesNum()<<endl; // } */ int num1=1; // 获取投票次数 int maxvotesnum =0; //获取投票后最高的票数 while(1){ cout<<"-----第"<<num1<<"次投票-----"<<endl; voters.Reset();// 重置容器指针 for(int i =0;i<numVoters;i++){ Voter& v = static_cast<Voter&>(voters.nextElement()); // 通过遍历选择选民 Candidate& chosenCandidate = static_cast< Candidate &>(v.SelectCadidate(candidates)); // 选民选出候选人 v.Vote(chosenCandidate); //选民将自己添加到候选人的选民容器 } // 选择获胜者 Candidate* winner = static_cast<Candidate*>(&candidates[0]); for ( int i=0;i<candidates.Size();i++) { cout<<*static_cast<Candidate*>(&candidates[i]); cout<<static_cast<Candidate*>(&candidates[i])->getVotesNum()-maxvotesnum<<endl; if ( *winner < *(static_cast<Candidate*>(&candidates[i]) )) { winner = static_cast<Candidate*>(&candidates[i]); } } maxvotesnum = winner->getVotesNum();//获取最高票数 PersonSet winnerCandidates;//选票相同的胜者容器 Candidate* _winner1 = static_cast<Candidate*>(&candidates[0]); for (int i=0; i<candidates.Size(); i++) { _winner1 = static_cast<Candidate*>(&candidates[i]); if (_winner1->getVotesNum() == maxvotesnum) { winnerCandidates.AddPerson(*_winner1); } } if (winnerCandidates.Size()==1) { cout<<"================"<<endl; cout<<"winner:"<< winner->getName()<<endl; cout<<"================"<<endl; break; }else{ candidates = winnerCandidates; //用新的容器替换旧的容器 num1++; } } delete v1; delete v2; delete v3; delete v4; delete v5; delete c1; delete c2; delete c3; return 0; }
时间: 2024-10-17 20:43:47