这是我的.h文件
#include<string.h>
#include<iostream.h>
#define N 12;
class Candidate{
int votenum;
char name[20];
public:
Candidate(char nam[]);
int add(); //添加一票
int getvotenum();
char * getname();
void setname(char nam[]);
};
class Vote{
Candidate *Can_array[12];
int size; //候选人数
double percent; //百分比
int sump; //投票人数
public:
Vote(char * name[],int n,double std);//初始化,给定候选人和获选标准
void vote1(char *name[],int n);//投票给n个人
int GetResult(char *name[],int snum[]);//返回获选标准,他们的姓名和票数
};
Candidate::Candidate(char nam[]){
strcpy(name,nam);
}
int Candidate::getvotenum(){
return votenum;
}
char * Candidate::getname(){
return name;
}
void Candidate::setname(char nam[]){
strcpy(name,nam);
}
int Candidate::add(){
return votenum++;
}
Vote::Vote(char * name[],int n,double std){
percent=std;
size=0;
if(n>12){
cout<<"候选人已满,不可以再添加!"<<endl;
}
else{
for(int i=0;i<n;i++){
Can_array[size]->setname(name[i]);
size++;
}
}
}
void Vote::vote1(char *name[],int n){
int i,j;
sump=n;
for(i=0;i<n;i++)
for(j=0;j<size;j++){
if(strcmp(Can_array[j]->getname(),name[i])==0)
Can_array[j]->add();
}
}
int Vote::GetResult(char *name[],int snum[]){
int num,j=0;
int n=0;
for(int i=0;i<size;i++){
num=Can_array[i]->getvotenum();
if((num/sump)>=percent){
strcpy(name[j],Can_array[i]->getname());
snum[j]=Can_array[i]->getvotenum();
j++;
n++;
}
}
return n;
}
主函数:
#include"vote.h"
#include<iostream.h>
void main()
{
char *name0[]={"a","b", "c","d","e"};
char *name1[]={"a","a","a","b","c"};
char *name3[5];
int sum[5];
Vote a(name0,5,0.5);
a.vote1(name1,5);
int n=a.GetResult(name3,sum);
cout<<"当选的人有"<<n<<"个"<<endl;
for(int i=0;i<n;i++){
cout<<name3[i]<<" "<<sum[i]<<endl;
}
}
编译没问题,但是在单步调试时 添加候选人姓名Can_array[size]->setname(name[i]);
繁华开满天机
相关分类