标题:求助!本人写的投票系统,候选人都跑到选举人中啦,选举人还出现重复等问题 ...
取消只看楼主
忆如风
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2014-7-27
结帖率:100%
已结贴  问题点数:10 回复次数:0 
求助!本人写的投票系统,候选人都跑到选举人中啦,选举人还出现重复等问题。希望大神帮忙改正。
以下附上我的代码:运行环境(Xcode)
代码有点乱  希望大神能耐心点
#ifndef __xiangmu2__Person__
#define __xiangmu2__Person__

#include <iostream>
using namespace std;
class Person
{
public:
    Person(const char* name="jiang",int age=28,double salary=0);//带参构造函数
    ~Person();
    Person(const Person& p);
    const Person& operator=(const Person& p);
   
    void SetAge(int newAge);//_age的set方法
    void SetSalary(double newSalary);//_salary的set方法
    int getAge();
    char *get_name();
    double getSalary();
    void print();//print方法
   
    friend istream& operator>>(istream& in,Person& p);
    friend ostream& operator<<(ostream& out,const Person& p);//输出运算符重载函数

protected:
    char* _name;//姓名
    int _age;//年龄
    double _salary;//薪水
    int _id;//身份编码
    static int _totalPersons;//总人数
};
#endif /* defined(__xiangmu2__Person__) */
#include "Person.h"
int Person::_totalPersons=1;
Person::Person(const char* name,int age,double salary)
:_name(new char[strlen(name)+1])
,_age(age)
,_salary(salary)
,_id(_totalPersons)
{
    _totalPersons++;
    strcpy(_name, name);
}
Person::~Person()
{
//    if (_name)
//    {
//        delete _name;
//    }
    //cout<<"Person::~Person()\n";
    if(_name!=NULL)
    delete  _name;
    _name=nullptr;
}
Person:: Person(const Person& p)
:_id(p._totalPersons)
{
    _name=new char[strlen(p._name)+1];
    strcpy(_name, p._name);
}
const Person& Person:: operator=(const Person& p)
{
    if (this!=&p)
    {
        delete _name;
    }
   
    _name=new char[strlen(p._name)+1];
        strcpy(_name, p._name);
   
    return *this;
}

void Person:: SetAge(int newAge)
{
    _age=newAge;
}
void Person:: SetSalary(double newSalary)
{
    _salary=newSalary;
}
int Person::getAge()
{
    return _age;
}
double Person::getSalary()
{
    return _salary;
}
void Person:: print()
{
    cout<<"姓名:"<<_name<<endl;
    cout<<"年龄:"<<_age<<endl;
    cout<<"薪水:"<<_salary<<endl;
    cout<<"身份编码:"<<_id<<endl;
}

istream&  operator>>(istream& in,Person& p)
{
    cout<<"姓名:";
    in>>p._name;
    cout<<"年龄:";
    in>>p._age;
    cout<<"薪水:";
    in>>p._salary;
    return in;
}
ostream& operator<<(ostream& out,const Person& p)
{
    out<<"姓名:"<<p._name<<endl;
    out<<"年龄:"<<p._age<<endl;
    out<<"薪水:"<<p._salary<<endl;
   
    out<<"身份编码:"<<p._id<<endl;
    return out;
}
char *Person::get_name()
{
    return _name;
}
#ifndef __xiangmu2__Voter__
#define __xiangmu2__Voter__

#include "Person.h"
#include "PersonSet.h"
#include "Candidate.h"
#include <iostream>
using namespace std;
class Candidate;
class PersonSet;
class Voter:public Person
{
public:
    Voter(const char* name="jiang",int age=28,double salary=0,int polingStation=0);//带参构造函数
    ~Voter();
    Voter(const Voter& v);
    const Voter& operator=(const Voter& v);
   
    void SetPolingStation(int newPolingStation);//_polingStation的set方法
    void print();//print方法
    friend bool operator==(Voter& a,Voter& b);//判断两个对象是否相同
    static int Voters();//静态的方法,返回所有选举人的个数
    Person& SelectCadidate( PersonSet& candidates );//随机获取候选人
    void Vote( Candidate& aCandidate );//开始选举
   
    friend istream& operator>>(istream& in,Voter& v);
    friend ostream& operator<<(ostream& out,Voter& v);
protected:
    int _polingStation;//投票号
public:
    static int _totalNumVoters;//参加选举的人数
};
#endif /* defined(__xiangmu2__Voter__) */
//选举人的实现文件

#include "Voter.h"
int Voter::_totalNumVoters=0;
Voter:: Voter(const char* name,int age,double salary,int polingStation)
:Person(name,age,salary)
,_polingStation(polingStation)
{
    _totalNumVoters++;
}
Voter::~Voter()
{
    //cout<<"Voter::~Voter()\n";
    //delete _name;
}
Voter:: Voter(const Voter& v)
:Person(v)
,_polingStation(v._polingStation)
{
}
const Voter& Voter:: operator=(const Voter& v)
{
    if (this!=&v)
    {
//        delete _name;
//        _name=new char[strlen(v._name)+1];
//        strcpy(_name, v._name);
        Person::operator=(v);
        _polingStation=v._polingStation;
        //_totalNumVoters=v._totalNumVoters;
    }
    return *this;
}

void Voter:: SetPolingStation(int newPolingStation)
{
    _polingStation=newPolingStation;
}
void Voter:: print()
{
    cout<<"voter name is:"<<_name<<endl;
    cout<<"voter polingStation id is:"<<_polingStation<<endl;
}
bool operator==(Voter& a,Voter& b)
{
    bool flag=false;
    if (a._id==b._id)
    {
        flag=true;
        return flag;
    }
    return flag;
}
int Voter::  Voters()
{
    return _totalNumVoters;
}
Person& Voter:: SelectCadidate( PersonSet& candidates )
{
    int x=candidates.size();
    int r=rand()%x;
    return candidates[r];
}
void Voter:: Vote( Candidate& aCandidate )
{
     aCandidate.AddVoter(*this);
}

//istream& operator>>(istream& in,Voter& v)
//{
//   
//}
ostream& operator<<(ostream& out,Voter& v)
{
    Person& voter = static_cast<Person&>(v);
    out<<voter<<endl;
//    out<<v._name<<v._id<<v._age<<v._salary<<endl;
    out<<"选举人投票号:"<<v._polingStation<<endl;
    return out;
}
//候选人的头文件

#ifndef __xiangmu2__Candidate__
#define __xiangmu2__Candidate__

#include "Person.h"
#include "PersonSet.h"
#include <iostream>
using namespace std;
class PersonSet;
class Voter;
class Candidate:public Person
{
public:
    Candidate(const char* name="jiang",int age=28,double salary=0);//带参构造函数
    ~Candidate();
    Candidate(const Candidate& c);
    const Candidate& operator=(const Candidate& c);
   
    int GetVotesNum();//当前候选人所得到的票数
    void AddVoter(Voter& aVoter);//得到选举人所给的票,记住给当前候选人投票人的信息
    friend ostream& operator<<(ostream& out, Candidate& c);//输出运算符重载函数
    double GetAverageVotersAge();//获取所有给当前候选人投票的所有选举人的平均年龄
    double GetAverageVotersSalary();//获取所有给当前候选人投票的所有选举人的平均薪资
    friend bool operator<(Candidate& a,Candidate& b);//重载运算符,判断是否a<b
    static int Candidates();//获取所有的候选人个数
protected:
    PersonSet _voterSet;//定义一个PersonSet 对象
    double average_age;//平均年龄
    double average_salary;//平均薪水
    static int _numCandidates;//统计有多少候选人
};
#endif /* defined(__xiangmu2__Candidate__) */
//候选人的实现文件


#include "Candidate.h"
#include "Voter.h"
int Candidate::_numCandidates=1;
Candidate::Candidate(const char* name,int age,double salary)//带参构造函数
:Person(name,age,salary)
{
    _numCandidates++;
}
Candidate::~Candidate()
{
    //cout<<"Candidate::~Candidate()\n";
    //delete _name;
}
Candidate::Candidate(const Candidate& c)
:Person(c)
,average_age(c.average_age)
,average_salary(c.average_salary)

{
//    _name=new char[strlen(c._name)+1];
//    strcpy(_name, c._name);
}
const Candidate& Candidate::operator=(const Candidate& c)
{
    if (this!=&c)
    {
//        delete _name;
//        _name=new char[strlen(c._name)+1];
//        strcpy(_name, c._name);
        Person::operator=(c);
        //_voterSet=c._voterSet;
        average_age=c.average_age;
        average_salary=c.average_salary;
        //_numCandidates=c._numCandidates;
    }
    return *this;

}

int Candidate::GetVotesNum()//当前候选人所得到的票数
{
    int size=(_voterSet).size();
    return size;
}
void Candidate::AddVoter(Voter& aVoter)//得到选举人所给的票,记住给当前候选人投票人的信息
{
    (_voterSet).add(aVoter);
}
ostream& operator<<(ostream& out, Candidate& c)//输出运算符重载函数
{
    if (c.GetVotesNum()==0)
    {
        cout<<"没有人投票!!!"<<endl;
    }
    //out<<c.GetVotesNum()<<endl;
    Person& candidate = static_cast<Person&>(c);
    out<<candidate<<endl;
   

//    out<<"平均年龄:"<<c.GetAverageVotersAge()<<endl;
//    out<<"平均薪水:"<<c.GetAverageVotersSalary()<<endl;
    return out;
}
double Candidate::GetAverageVotersAge()//获取所有给当前候选人投票的所有选举人的平均年龄
{
    double sumAge=0;
    int sum=Voter::_totalNumVoters;
    for (int i=0; i<sum; i++)
    {
        sumAge+=_voterSet.nextElement().getAge();
    }
    average_age=sumAge/sum;
    return average_age;
}
double Candidate::GetAverageVotersSalary()//获取所有给当前候选人投票的所有选举人的平均薪资
{
    double sumSalary=0;
    int sum=Voter::_totalNumVoters;
    for (int i=0; i<sum; i++)
    {
        sumSalary+=_voterSet.nextElement().getSalary();
    }
    average_salary=sumSalary/sum;
    return average_salary;
}
bool operator<(Candidate& a,Candidate& b)//重载运算符,判断是否a<b
{
    bool flag=false;
    if (a.GetVotesNum()<b.GetVotesNum())
    {
        flag=true;
    }
    return flag;
}
int Candidate::Candidates()//获取所有的候选人个数
{
    return _numCandidates;
}
#ifndef __xiangmu2__PersonSet__
#define __xiangmu2__PersonSet__

#include "Person.h"
#include <iostream>
using namespace std;
class Voter;
class Candidate;
class PersonSet
{
public:
    PersonSet();//默认构造函数
    ~PersonSet();//析构函数
    PersonSet(PersonSet& p);//拷贝构造函数
    const PersonSet& operator=(const PersonSet& p);//赋值运算符重载
    void add(Person &element);//添加元素
    Person& nextElement();//遍历元素
    Person& removeElement();//删除元素,从后向前,依次删除
    Person& removeElement(int index);//删除指定元素
    int size();//得到实际的个数
    void print()const;//print功能
    void reset();//重置_index=0
    Person& operator[](int x);//重载下标运算符
    const Person& operator[](int x)const;//重载不可修改的下标运算符
protected:
    Person** _elements;//定义Person二级指针
    int _capacity;//容量的大小
    int _size;//实际大小
    int _index;//指向
};
#endif /* defined(__xiangmu2__PersonSet__) */
#include "PersonSet.h"
PersonSet:: PersonSet()//默认构造函数
:_elements(new Person*[_capacity])
,_capacity(4)
,_size(0)
,_index(0)
{
   
}
PersonSet::~PersonSet()//析构函数
{
    delete []_elements;
}
PersonSet::PersonSet(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& 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;
}
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& PersonSet:: nextElement()//遍历元素
{
    if (_index>_size)
    {
        _index=0;
    }
    return *(_elements[_index++]);
}
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& 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 PersonSet:: size()//得到实际的个数
{
    return _size;
}
void PersonSet:: print()const//print功能
{
    for (int i=0; i<_size; i++)
    {
        (*_elements[i]).print();
        printf("\n");
    }
}
void PersonSet:: reset()//重置_index=0
{
    _index=0;
}
Person& PersonSet:: operator[](int x)//重载下标运算符
{
    return *(_elements[x]);
}
const Person& PersonSet:: operator[](int x)const//重载不可修改的下标运算符
{
    return *(_elements[x]);
}

#include "Person.h"
#include "Voter.h"
#include "Candidate.h"
#include "PersonSet.h"
#include <iostream>

int main(int argc, const char * argv[])
{
//
//   Person s("yi",20,28);
//    s.print();
//    cout<<s<<endl;
//    Person a("ru",28,28);
//    cout<<a<<endl;
//    a.print();
//    Person b;
//    cin>>b;
//    cout<<b;
    //定义一些固定(常量)的投票者
    Voter *v1 = new Voter("John", 20, 6000,1);
    Voter *v2 = new Voter("Frank",26, 30000,2);
    Voter *v3 = new Voter("Anna", 20, 199600,1);
    Voter *v4 = new Voter("James", 67, 9600,2);
    Voter *v5 = new Voter("Jane", 40, 29600,3);
     
    // 定义三个候选人
    Candidate *c1 = new Candidate("April", 67, 9600 );
    Candidate *c2 = new Candidate("May", 40, 29600);
    Candidate *c3 = new Candidate("June", 40, 29600);
    //创建一个随机数种子
    srand((unsigned)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();  // 设置index为0
    cout << "选举人数量 = " << numVoters << "\n";
    for(int i = 0; i<numVoters; i++)
    {
        
        Voter& voter1 = static_cast<Voter&>(voters.nextElement());
        cout << voter1 << "\n";
    }
    // 允许投票者进行投票
    voters.reset();  // 设置index为0
    cout << "voting = \n";
    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);
    }
    // check if all voters were added to the candidates
    // 判断是否所有投票者都进行了投票
    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++ )
    {
        if ( *winner < *(static_cast<Candidate*>(&candidates[i]) ))
        {
            winner =(static_cast<Candidate*>( &candidates[i]));
        }
    }
    cout << "当选人信息如下: " <<endl;
    cout<< *winner<<endl;
    cout<<"该当选人支持者的平均年龄:"<<winner->GetAverageVotersAge()<<endl;
    cout<<"该当选人支持者的平均薪水:"<<winner->GetAverageVotersSalary()<<endl;
   
    delete v1;
    delete v2;
    delete v3;
    delete v4;
    delete v5;
    delete c1;
    delete c2;
    delete c3;
   return 0;
}
搜索更多相关主题的帖子: 运行环境 include public double 候选人 
2014-08-16 11:03



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-435055-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.178074 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved