/***************************************************
文件名称:Person.h
作 者:zz
备 注:人的头文件
创建时间:2012-4-1
修改历史:2012-4-5
版权声明:CSDN
***************************************************/
#ifndef _Person_H
#define _Person_H
#include <string>
#include <iostream>
using namespace std;
class Person{
/*******************************************
函数名称:ostream& operator<<(ostream& ,Person&)
函数功能:输出运算符重载函数
传入参数:N/A
返回 值 :N/A
********************************************/
friend ostream& operator<<(ostream& ,Person&);
protected:
//名字
string _name;
//年龄
int _age;
//薪资
double _salary;
//不可修改的id号
const int _id;
//总人数
static int _totalPersons;
public:
/*******************************************
函数名称:Person(string name="zhangsan",int age=0,int salary=0);
函数功能:带参构造函数
传入参数:N/A
返回 值 :N/A
********************************************/
Person(string name="zhangsan",int age=0,double salary=0);
/*******************************************
函数名称:void setAge(int newAge)
函数功能:_age的set方法
传入参数:N/A
返回 值 :N/A
********************************************/
void setAge(int newAge);
/*******************************************
函数名称:void setSalary(int salary)
函数功能:_salary的set方法
传入参数:N/A
返回 值 :N/A
********************************************/
void setSalary(double salary);
/*******************************************
函数名称:void setName(int name)
函数功能:_name的set方法
传入参数:N/A
返回 值 :N/A
********************************************/
void setName(string name);
/*******************************************
函数名称:string getName();
函数功能:_name的get方法
传入参数:N/A
返回 值 :N/A
********************************************/
string getName()const;
/*******************************************
函数名称:int getAge();
函数功能:_age的get方法
传入参数:N/A
返回 值 :N/A
********************************************/
int getAge()const;
/*******************************************
函数名称:int getSalary();
函数功能:_salary的get方法
传入参数:N/A
返回 值 :N/A
********************************************/
double getSalary()const;
/*******************************************
函数名称:void print();
函数功能:print方法
传入参数:N/A
返回 值 :N/A
********************************************/
void print()const;
};
#endif
/***************************************************
文件名称:Person.cpp
作 者:zz
备 注:人的实现文件
创建时间:2012-4-1
修改历史:2012-4-5
版权声明:CSDN
***************************************************/
#include "Person.h"
#include <iostream>
#include <string>
using namespace std;
//初始化总人数
int Person::_totalPersons=0;
/*******************************************
函数名称:Person(string name,int age,int salary)
函数功能:带参构造函数
传入参数:N/A
返回 值 :N/A
********************************************/
Person::Person(string name,int age,double salary):
_name(name),_age(age),_salary(salary),_id(_totalPersons ){
_totalPersons++;
}
/*******************************************
函数名称:void setAge(int newAge)
函数功能:_age的set方法
传入参数:N/A
返回 值 :N/A
********************************************/
void Person::setAge(int newAge){
_age=newAge;
}
/*******************************************
函数名称:void setSalary(int salary)
函数功能:_salary的set方法
传入参数:N/A
返回 值 :N/A
********************************************/
void Person::setSalary(double salary){
_salary=salary;
}
/*******************************************
函数名称:void print();
函数功能:print方法
传入参数:N/A
返回 值 :N/A
********************************************/
void Person::print()const{
cout<<"Person name is"<<_name<<endl;
cout<<"Perosn id is"<<_id<<endl;
}
/*******************************************
函数名称:ostream& operator<<(ostream& ,Person&)
函数功能:输出运算符重载函数
传入参数:N/A
返回 值 :N/A
********************************************/
ostream& operator<<(ostream& out ,Person& person){
out<<"Person name:"<<person._name<<endl;
out<<"Person age:"<<person._age<<endl;
out<<"Person salary:"<<person._salary<<endl;
out<<"Person id:"<<person._id<<endl;
return out;
}
/*******************************************
函数名称:void setName(int name)
函数功能:_name的set方法
传入参数:N/A
返回 值 :N/A
********************************************/
void Person::setName(string name){
_name= name;
}
/*******************************************
函数名称:string getName();
函数功能:_name的get方法
传入参数:N/A
返回 值 :N/A
********************************************/
string Person::getName()const{
return _name;
}
/*******************************************
函数名称:int getAge();
函数功能:_age的get方法
传入参数:N/A
返回 值 :N/A
********************************************/
int Person::getAge()const{
return _age;
}
/*******************************************
函数名称:int getSalary();
函数功能:_salary的get方法
传入参数:N/A
返回 值 :N/A
********************************************/
double Person::getSalary()const{
return _salary;
}
/***************************************************
文件名称:Voter.h
作 者:zz
备 注:选举人的头文件
创建时间:2012-4-1
修改历史:2012-4-5
版权声明:CSDN
***************************************************/
#ifndef _Voter_H
#define _Voter_H
#include "Person.h"
#include <string>
#include <iostream>
using namespace std;
class PersonSet;
class Candidate;
class Voter:public Person{
/*******************************************
函数名称:friend bool operator==(Voter& a,Voter& b)
函数功能:判断两个对象是否相同
传入参数:N/A
返回 值 :N/A
********************************************/
friend bool operator==(Voter& a,Voter& b);
protected:
int _polingStation;//站台号
public:
static int _totalNumVoters; //参加选举的人数
/*******************************************
函数名称:Voter(string name="zhangsan",int age=0,int salary=0,int polingStation=0);
函数功能:带参构造函数
传入参数:N/A
返回 值 :N/A
********************************************/
Voter(string name="zhangsan",int age=0,double salary=0,int polingStation=0);
/*******************************************
函数名称:void Vote( Candidate& aCandidate );
函数功能:开始选举
传入参数:N/A
返回 值 :N/A
********************************************/
void Vote( Candidate& aCandidate );
/*******************************************
函数名称: void set_polingStation(int newPolingStation);
函数功能:_polingStation的set方法
传入参数:N/A
返回 值 :N/A
********************************************/
void set_polingStation(int newPolingStation);
/*******************************************
函数名称: int get_PolingStation()const;
函数功能:_polingStation的get方法
传入参数:N/A
返回 值 :N/A
********************************************/
int get_PolingStation()const;
/*******************************************
函数名称:void print();
函数功能:print方法
传入参数:N/A
返回 值 :N/A
********************************************/
void print()const;
/*******************************************
函数名称:static int Voters();
函数功能:静态的方法,返回所有选举人的个数
传入参数:N/A
返回 值 :N/A
********************************************/
static int Voters();
/*******************************************
函数名称:Person& SelectCadidate(PersonSet& cadidates);
函数功能:随机获取候选人
传入参数:N/A
返回 值 :N/A
********************************************/
Person& SelectCadidate(PersonSet& cadidates);
};
#endif
/***************************************************
文件名称:Voter.cpp
作 者:zz
备 注:选举人的头文件
创建时间:2012-4-1
修改历史:2012-4-5
版权声明:CSDN
***************************************************/
#include "Voter.h"
#include <iostream>
#include <string>
#include "Candidate.h"
#include "PersonSet.h"
#include "Person.h"
using namespace std;
//初始化选举人的个数
int Voter::_totalNumVoters=0;
/*******************************************
函数名称: Voter::Voter(string name,int age,int salary,int polingStation)
函数功能:带参构造函数
传入参数:N/A
返回 值 :N/A
********************************************/
Voter::Voter(string name,int age,double salary,int polingStation):
Person(name,age,salary),_polingStation(polingStation){
_totalNumVoters++;
}
/*******************************************
函数名称:void Vote( Candidate& aCandidate );
函数功能:开始选举
传入参数:N/A
返回 值 :N/A
********************************************/
void Voter::Vote( Candidate& aCandidate ){
aCandidate.AddVoter(*this);
}
/*******************************************
函数名称: void set_polingStation(int newPolingStation);
函数功能:_polingStation的set方法
传入参数:N/A
返回 值 :N/A
********************************************/
void Voter::set_polingStation(int newPolingStation){
_polingStation=newPolingStation;
}
/*******************************************
函数名称:void print();
函数功能:print方法
传入参数:N/A
返回 值 :N/A
********************************************/
void Voter::print()const{
cout<<"voter name is:"<<_name<<endl;
cout<<"voter _polingStation id is"<<_polingStation<<endl;
}
/*******************************************
函数名称:friend bool operator==(Voter& a,Voter& b)
函数功能:判断两个对象是否相同
传入参数:N/A
返回 值 :N/A
********************************************/
bool operator==(Voter& a,Voter& b){
bool flag=false;
if(a._id==b._id){
flag=true;
return flag;
}
return flag;
}
/*******************************************
函数名称:static int Voters();
函数功能:静态的方法,返回所有选举人的个数
传入参数:N/A
返回 值 :N/A
********************************************/
int Voter::Voters(){
//返回所给候选人投票的人数
return _totalNumVoters;
}
/*******************************************
函数名称:Person& SelectCadidate(PersonSet& cadidates);
函数功能:随机获取候选人
传入参数:N/A
返回 值 :N/A
********************************************/
Person& Voter::SelectCadidate(PersonSet& candidates){
int x=candidates.size();
int r=rand()%x;
return candidates[r];
}
/*******************************************
函数名称: int get_PolingStation()const;
函数功能:_polingStation的get方法
传入参数:N/A
返回 值 :N/A
********************************************/
int Voter::get_PolingStation()const{
return _polingStation;
}
/***************************************************
文件名称:Candidate.h
作 者:zz
备 注:候选人的头文件
创建时间:2012-4-1
修改历史:2012-4-5
版权声明:CSDN
***************************************************/
#ifndef _Candidate_H
#define _Candidate_H
#include "Person.h"
#include "PersonSet.h"
#include <string>
#include <iostream>
using namespace std;
class PersonSet;
class Voter;
class Candidate:public Person{
/*******************************************
函数名称:ostream& operator<<(ostream& ,Person&)
函数功能:输出运算符重载函数
传入参数:N/A
返回 值 :N/A
********************************************/
friend ostream& operator<<(ostream&,Candidate&);
/*******************************************
函数名称:bool operator<(Candidate& a,Candidate& b);
函数功能:重载运算符,判断是否a<b
传入参数:N/A
返回 值 :N/A
********************************************/
friend bool operator<(Candidate& a,Candidate& b);
protected:
//定义一个PersonSet 对象
PersonSet _votorSet;
double average_age;//平均年龄
double average_salary;//平均薪水
static int _numCandidates;//统计比赛中有多少候选人参加
public:
/*******************************************
函数名称:PersonSet& Candidate::getPersonSet()
函数功能:PersonSet对象的get方法
传入参数:N/A
返回 值 :N/A
********************************************/
PersonSet& getPersonSet();
/*******************************************
函数名称: Candidate(string name="lisi",int age=5,int salary=1222);
函数功能:带参构造函数
传入参数:N/A
返回 值 :N/A
********************************************/
Candidate(string name="lisi",int age=5,double salary=0);
/*******************************************
函数名称: int getVotesNum();
函数功能:当前选举人所得到的票数
传入参数:N/A
返回 值 :N/A
********************************************/
int getVotesNum();//得到每个候选人的票数
/*******************************************
函数名称: void AddVoter(Voter& aVoter);
函数功能:得到选举人所给的票,记住给当前候选人投票人的信息
传入参数:N/A
返回 值 :N/A
********************************************/
void AddVoter(Voter& aVoter);
/*******************************************
函数名称: int getAverageVotersAge();
函数功能:获取所以给当前候选人投票的所有选举人的平均年龄
传入参数:N/A
返回 值 :N/A
********************************************/
double getAverageVotersAge();
/*******************************************
函数名称: int getAverageVotersSalary();
函数功能:获取所以给当前候选人投票的所有选举人的平均薪资
传入参数:N/A
返回 值 :N/A
********************************************/
double getAverageVotersSalary();
/*******************************************
函数名称: static int Candidates();
函数功能:获取所有的候选人个数
传入参数:N/A
返回 值 :N/A
********************************************/
static int Candidates();
};
#endif
/***************************************************
文件名称:Candidate.cpp
作 者:zz
备 注:候选人的实现文件
创建时间:2012-4-1
修改历史:2012-4-5
版权声明:CSDN
***************************************************/
#include "Candidate.h"
#include "Voter.h"
#include "PersonSet.h"
#include "Person.h"
#include <ostream>
#include <string>
using namespace std;
//初始化 总候选人的个数
int Candidate::_numCandidates=0;
/*******************************************
函数名称:PersonSet& Candidate::getPersonSet()
函数功能:PersonSet对象的get方法
传入参数:N/A
返回 值 :N/A
********************************************/
PersonSet& Candidate::getPersonSet(){
return _votorSet;
}
/*******************************************
函数名称: Candidate(string name="lisi",int age=5,int salary=1222);
函数功能:带参构造函数
传入参数:N/A
返回 值 :N/A
********************************************/
Candidate:: Candidate(string name,int age,double salary):Person(name,age,salary)
{
_numCandidates++;
}
/*******************************************
函数名称: int getVotesNum();
函数功能:当前选举人所得到的票数
传入参数:N/A
返回 值 :N/A
********************************************/
int Candidate:: getVotesNum(){
//static int s=0;
int size= (_votorSet).size();
return size;
}
/*******************************************
函数名称: void AddVoter(Voter& aVoter);
函数功能:得到选举人所给的票,记住给当前候选人投票人的信息
传入参数:N/A
返回 值 :N/A
********************************************/
void Candidate:: AddVoter(Voter& aVoter){
(_votorSet).add(aVoter);
}
/*******************************************
函数名称: int getAverageVotersAge();
函数功能:获取所以给当前候选人投票的所有选举人的平均年龄
传入参数:N/A
返回 值 :N/A
********************************************/
double Candidate:: getAverageVotersAge(){
int sumAge=0;
int sum=Voter::_totalNumVoters;//每位候选人得到的总票数
for(int i=0;i<sum;i++){
//Voter& v = static_cast<Voter&>(_votorSet.nextElement());
//sumAge+=v.nextElement().getAge();
sumAge+=_votorSet.nextElement().getAge();
}
average_age=sumAge/sum;
return average_age;
}
/*******************************************
函数名称: int getAverageVotersSalary();
函数功能:获取所以给当前候选人投票的所有选举人的平均薪资
传入参数:N/A
返回 值 :N/A
********************************************/
double Candidate:: getAverageVotersSalary(){
double sumSalary=0;
int sum=Voter::_totalNumVoters;//每位候选人得到的总票数
for(int i=0;i<sum;i++){
PersonSet _votorSet;
//Voter& v = static_cast<Voter&>(_votorSet.nextElement());
//sumSalary+=v.getSalary();
sumSalary+=_votorSet.nextElement().getSalary();
}
average_salary = sumSalary/sum;
return average_salary;
}
/*******************************************
函数名称: static int Candidates();
函数功能:获取所有的候选人个数
传入参数:N/A
返回 值 :N/A
********************************************/
//得到参加比赛的候选人的数量
int Candidate:: Candidates()
{
return _numCandidates;
}
/*******************************************
函数名称:bool operator<(Candidate& a,Candidate& b);
函数功能:重载运算符,判断是否a<b
传入参数:N/A
返回 值 :N/A
********************************************/
bool operator<(Candidate& a,Candidate& b){
bool flag=true;
if(a.getVotesNum()>b.getVotesNum()){
flag=false;
}
return flag;
}
/*******************************************
函数名称:ostream& operator<<(ostream& ,Person&)
函数功能:输出运算符重载函数
传入参数:N/A
返回 值 :N/A
********************************************/
ostream& operator<<(ostream& out,Candidate& c){
if(c.getVotesNum()==0){
cout<<"There are no voters !!!!"<<endl;
}
out<<c.getName()<<endl;
return out;
}
/***************************************************
文件名称:PersonSet.h
作 者:zz
备 注:PersonSet的头文件
创建时间:2012-4-1
修改历史:2012-4-5
版权声明:CSDN
***************************************************/
#ifndef _PersonSet_H
#define _PersonSet_H
#include "Person.h"
class Voter;
class Candidate;
class PersonSet{
protected:
//定义Person二级指针
Person** _elements;
//容量的大小
int _capacity;
//实际大小
int _size;
//指向
int _index;
public:
/*******************************************
函数名称:PersonSet();
函数功能:默认构造函数
传入参数:N/A
返回 值 :N/A
********************************************/
PersonSet();
// PersonSet(Person& p);
/*******************************************
函数名称: PersonSet(PersonSet& p);
函数功能:拷贝构造函数
传入参数:N/A
返回 值 :N/A
********************************************/
PersonSet(const PersonSet& p);
/*******************************************
函数名称: const PersonSet& operator=(const PersonSet& p);
函数功能:赋值运算符重载
传入参数:N/A
返回 值 :N/A
********************************************/
const PersonSet& operator=(const PersonSet& p);
/*******************************************
函数名称: void add(Person& element);
函数功能:添加元素
传入参数:N/A
返回 值 :N/A
********************************************/
void add(Person& element);
/*******************************************
函数名称:Person& nextElement();
函数功能:遍历元素
传入参数:N/A
返回 值 :N/A
********************************************/
Person& nextElement();
/*******************************************
函数名称:Person& removeElement();
函数功能:删除元素,从后向前,依次删除
传入参数:N/A
返回 值 :N/A
********************************************/
Person& removeElement();
/*******************************************
函数名称:Person& removeElement(int index);
函数功能:删除指定元素
传入参数:N/A
返回 值 :N/A
********************************************/
Person& removeElement(int index);
/*******************************************
函数名称:int size();
函数功能:得到实际的个数
传入参数:N/A
返回 值 :N/A
********************************************/
int size();
/*******************************************
函数名称: void print();
函数功能:print功能
传入参数:N/A
返回 值 :N/A
********************************************/
void print()const;
/*******************************************
函数名称: void reset();
函数功能:重置_index=0
传入参数:N/A
返回 值 :N/A
********************************************/
void reset();
/*******************************************
函数名称: ~PersonSet();
函数功能:析构函数
传入参数:N/A
返回 值 :N/A
********************************************/
~PersonSet();
/*******************************************
函数名称: Person& operator[](int x);
函数功能:重载下标运算符
传入参数:N/A
返回 值 :N/A
********************************************/
Person& operator[](int x);
/*******************************************
函数名称: const Person& operator[](int x)const;
函数功能:重载不可修改的下标运算符
传入参数:N/A
返回 值 :N/A
********************************************/
const Person& operator[](int x)const;
};
#endif
/***************************************************
文件名称:PersonSet.cpp
作 者:zz
备 注:PersonSet的实现文件
创建时间:2012-4-1
修改历史:2012-4-5
版权声明:CSDN
***************************************************/
#include "PersonSet.h"
using namespace std;
#include <iostream>
#include <string>
#include "Person.h"
/*******************************************
函数名称:PersonSet();
函数功能:默认构造函数
传入参数:N/A
返回 值 :N/A
********************************************/
PersonSet::PersonSet(){
_size=0;
_capacity=4;
_index=0;
_elements=new Person*[_capacity];
}
// PersonSet:: PersonSet(Person& p){
// }
/*******************************************
函数名称: void add(Person& element);
函数功能:添加元素
传入参数:N/A
返回 值 :N/A
********************************************/
void PersonSet::add(Person& element){
if(_size==_capacity){
Person** temp= _elements;
_elements=new Person*[_capacity*2];
for(int i=0;i<_size;i++){
_elements[i]=temp[i];
}
_capacity*=2;
delete []temp;
}
_elements[_size++]=&element;
}
/*******************************************
函数名称:Person& nextElement();
函数功能:遍历元素
传入参数:N/A
返回 值 :N/A
********************************************/
Person& PersonSet::nextElement(){
if(_index>_size){
_index=0;
}
return *(_elements[_index++]);
}
/*******************************************
函数名称:Person& removeElement();
函数功能:删除元素,从后向前,依次删除
传入参数:N/A
返回 值 :N/A
********************************************/
Person& PersonSet::removeElement(){
if(_size==0){
cout<<"对象为空,不能删除!"<<endl;
exit(1);
}
_size--;
Person* p = _elements[_size];
if(_size<_capacity/2){
Person** temp=_elements;
_elements=new Person*[_capacity/2];
for(int i=0;i<_size;i++){
_elements[i]=temp[i];
}
_capacity/=2;
delete [] temp;
}
return *p;
}
/*******************************************
函数名称:Person& removeElement(int index);
函数功能:删除指定元素
传入参数:N/A
返回 值 :N/A
********************************************/
Person& PersonSet::removeElement(int index){
if(index>=_size){
cout<<"输入有错!"<<endl;
}
_size--;
Person* p = _elements[index];
if(_size<_capacity/2){
Person** temp=_elements;
_elements=new Person*[_capacity/2];
for(int i=0;i<_size;i++){
_elements[i]=temp[i];
}
_capacity/=2;
delete [] temp;
}
return *p;
}
/*******************************************
函数名称:int size();
函数功能:得到实际的个数
传入参数:N/A
返回 值 :N/A
********************************************/
int PersonSet::size(){
return _size;
}
/*******************************************
函数名称: void print();
函数功能:print功能
传入参数:N/A
返回 值 :N/A
********************************************/
void PersonSet::print()const{
for(int i=0;i<_size;i++){
(*_elements[i]).print();
printf("\n");
}
}
/*******************************************
函数名称: void reset();
函数功能:重置_index=0
传入参数:N/A
返回 值 :N/A
********************************************/
void PersonSet::reset(){
_index=0;
}
/*******************************************
函数名称: ~PersonSet();
函数功能:析构函数
传入参数:N/A
返回 值 :N/A
********************************************/
PersonSet::~PersonSet(){
delete [] _elements;
}
/*******************************************
函数名称: Person& operator[](int x);
函数功能:重载下标运算符
传入参数:N/A
返回 值 :N/A
********************************************/
Person& PersonSet::operator[](int x){
return *(_elements[x]);
}
/*******************************************
函数名称: const Person& operator[](int x)const;
函数功能:重载不可修改的下标运算符
传入参数:N/A
返回 值 :N/A
********************************************/
const Person& PersonSet::operator[](int x)const{
return *(_elements[x]);
}
/*******************************************
函数名称: PersonSet(PersonSet& p);
函数功能:拷贝构造函数
传入参数:N/A
返回 值 :N/A
********************************************/
PersonSet::PersonSet(const PersonSet& p){
_capacity=p._capacity;
_size=p._size;
_index=p._index;
_elements=new Person*[_size];
for(int i=0;i<_size;i++){
_elements[i]=p._elements[i];
}
}
/*******************************************
函数名称: const PersonSet& operator=(const PersonSet& p);
函数功能:赋值运算符重载
传入参数:N/A
返回 值 :N/A
********************************************/
const PersonSet& PersonSet::operator=(const PersonSet& p){
if(this!=&p){
_capacity=p._capacity;
_size=p._size;
_index=p._index;
delete [] _elements;
_elements=new Person*[_size];
for(int i=0;i<_size;i++)
{
_elements[i]=p._elements[i];
}
}
return *this;
}
Main.cpp
#include <iostream>
using namespace std;
#include "Voter.h"
#include "Candidate.h"
#include "PersonSet.h"
#include "Person.h"
int main(int argc, char *argv[])
{
//创建选举人
Voter *v1 = new Voter("John", 20, 6000.0,1);
Voter *v2 = new Voter("Frank", 26, 30000.0,2);
Voter *v3 = new Voter("Anna", 20, 199600.0,1);
Voter *v4 = new Voter("James", 67, 9600.0,2);
Voter *v5 = new Voter("Jane", 40, 29600.0,3);
//创建候选人
Candidate *c1 = new Candidate("April", 67, 9600.0 );
Candidate *c2 = new Candidate("May", 40, 29600.0);
Candidate *c3 = new Candidate("June", 40, 29600.0);
srand(time(NULL));
PersonSet voters;
voters.add(*v1);
voters.add(*v2);
voters.add(*v3);
voters.add(*v4);
voters.add(*v5);
PersonSet candidates;
candidates.add(*c1);
candidates.add(*c2);
candidates.add(*c3);
int numVoters = voters.size();
voters.reset();
cout << "number of voters = " << numVoters << "\n";
for(int i = 0; i<numVoters; i++)
{
Voter& voter = static_cast<Voter&>(voters.nextElement());
cout << voter << "\n";
}
voters.reset(); // Set the index zero
cout << "voting = \n";
for(int i = 0; i<numVoters; i++)
{
Voter& v = static_cast<Voter&>(voters.nextElement());
// choose a candidate
Candidate& chosenCandidate = static_cast<Candidate&>(v.SelectCadidate(candidates));
v.Vote(chosenCandidate);
}
/*
for(int a=0;a<candidates.size();a++){
for(int b=++a;b<candidates.size();b++){
Candidate* p,q;
if((p=&static_cast<Candidate&>(candidates[a]))->getVotesNum()==(q=&static_cast<Candidate&>(candidates[b]))){
}
}
}
*/
cout<<"April:"<<c1->getVotesNum()<<endl;
cout<<"May:"<<c2->getVotesNum()<<endl;
cout<<"June:"<<c3->getVotesNum()<<endl;
cout<<"voters.size()="<<voters.size()<<endl;
cout<<"Voter::_totalNumVoters="<<Voter::_totalNumVoters<<endl;
if (voters.size() != Voter::_totalNumVoters)
{
cout <<" Not all voters voted !!!!" << endl ;
}
else
{
cout <<" Success: 100% voted" << endl;
}
Candidate* winner = static_cast<Candidate*>(&candidates[0]);
for ( int i=0; i<candidates.size(); i++ )
{
if ( *winner < *(static_cast<Candidate*>(&candidates[i]) ))
{
winner = static_cast<Candidate*>(&candidates[i]);
}
}
if(winner->getVotesNum() == 2)
{
PersonSet ps;
for(int i = 0 ; i < candidates.size() ; i++)
{
Candidate * p;
if(( p = &static_cast<Candidate&>(candidates[i]))->getVotesNum() == 2)
{
ps.add(*p);
}
}
for(int j=0;j<numVoters;j++){
Voter& v = static_cast<Voter&>(voters.nextElement());
// choose a candidate
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<<endl;
cout<<"winner:"<<winner->getVotesNum()-2<<endl;
}else{
cout << " the winner is : " << *winner<<endl;
}
delete v1;
delete v2;
delete v3;
delete v4;
delete v5;
delete c1;
delete c2;
delete c3;
return 0;
}
linux环境编译时所用到的文件
makefile 文件
###############################################################################
#
# Generic Makefile for C/C++ Program
#
# Author:
# Date:
# Description:
# The makefile searches in <SRCDIRS> directories for the source files
# with extensions specified in <SOURCE_EXT>, then compiles the sources
# and finally produces the <PROGRAM>, the executable file, by linking
# the objectives.
# Usage:
# $ make compile and link the program.
# $ make objs compile only (no linking. Rarely used).
# $ make clean clean the objectives and dependencies.
# $ make cleanall clean the objectives, dependencies and executable.
# $ make rebuild rebuild the program. The same as make clean && make all.
#==============================================================================
CFLAGS = -g
EXE = a.out
.SUFFIXES = .c .o .cpp
.c.o:
gcc -c $(CFLAGS) $<
.cpp.o:
g++ -c $(CFLAGS) $<
SOURCE = Person.h Person.cpp Voter.h Voter.cpp Candidate.h Candidate.cpp PersonSet.h PersonSet.cpp main.cpp
OBJECTS = ${SOURCE:.cpp=.o}
$(EXE): ${OBJECTS}
g++ $(CFLAGS) -o $@ $(OBJECTS)
all: $(EXE)
clean:
rm $(EXE) *.o
### End of the Makefile ## Suggestions are welcome ## All rights reserved ###
###############################################################################