C++第二个项目实现(八)

2014-11-24 12:08:19 · 作者: · 浏览: 9
nclude "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 {
Voter& voter = static_cast(voters.nextElement());
cout << voter << "\n";
}

voters.reset(); // Set the index zero
cout << "voting = \n";

for(int i = 0; i {
Voter& v = static_cast(voters.nextElement());
// choose a candidate
Candidate& chosenCandidate = static_cast(v.SelectCadidate(candidates));
v.Vote(chosenCandidate);

}

/*
for(int a=0;a for(int b=++a;b Candidate* p,q;
if((p=&static_cast(candidates[a]))->getVotesNum()==(q=&static_cast(candidates[b]))){


}
}

}
*/

cout<<"April:"<getVotesNum()< cout<<"May:"<getVotesNum()< cout<<"June:"<getVotesNum()<

cout<<"voters.size()="< cout<<"Voter::_totalNumVoters="<


if (voters.size() != Voter::_totalNumVoters)
{

cout <<" Not all voters voted !!!!" << endl ;
}
else
{
cout <<" Success: 100% voted" << endl;
}

Candidate* winner = static_cast(&candidates[0]);
for ( int i=0; i {
if ( *winner < *(static_cast(&candidates[i]) ))
{
winner = static_cast(&candidates[i]);
}
}

if(winner->getVotesNum() == 2)
{
PersonSet ps;

for(int i = 0 ; i < candidates.size() ; i++)
{
Candidate * p;
if(( p = &static_cast(candidates[i]))->getVotesNum() == 2)
{
ps.add(*p);
}
}


for(int j=0;j
Voter& v = static_cast(voters.nextElement());
// choose a candidate
Candidate& chosenCandidate = static_cast(v.SelectCadidate(ps));
v.Vote(chosenCandidate);

}


cout<<"twice Vote:"< Candidate* winner = static_cast(&ps[0]);

for ( int i=0; i {
if ( *winner < *(static_cast(&ps[i]) ))
{
winner = static_cast(&ps[i]);
}
}
cout << " the winner is : " << *winner< cout<<"winner:"<getVotesNum()-2<
}else{

cout << " the winner is : " << *winner< }
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 directories for the source files
# with extensions specified in , then compiles the sources
# and finally produces the , 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 = ${