Election方法2


//工程名:Election
//文件名:Election.cpp
//功能:统计候选人的投票数
//依赖文件:无

#include <string.h>
#include <iostream.h>

const int n=3;
const int LEN=10;

struct Person{
char *name;
int count;
}Leader[n];

Person *Create(const char *name,int num)
{
Person *p = new Person;
int mn = strlen(name);
p->name = new char[mn+1];
strcpy(p->name,name);
p->name[mn]='/0';
p->count = num;
return p;
}

void Free(Person *p)
{
if(p){
delete []p->name;
// delete p;
}
}

Person *Copy(Person *o,Person *s)
{
if(o&&s){
int mn;
char *c = new char[mn=strlen(s->name)+1];
if(c==0)return o;
strcpy(c,s->name);
c[mn]='/0';
delete []o->name;
o->name = c;}
return o;
}

bool Init(Person *p,int n)
{
if(!p)return false;
int count=0;
char *name = new char[LEN+1];
do{
cout<<"请输入候选人名(3个):";
cin>>name;
name[LEN]=0;
if(strstr(name,"#"))break;
Person *t = Create(name,0);
Copy(&(p[count++]),t);
delete t;
}while(count<n);
delete []name;
return count>0;
}

void Election(Person Leader[],int mn)
{
char* name = new char[LEN];
cout<<"请为候选人名投票,以#号结束:";
cin>>name;
while(!strstr(name,"#")/*name!="#"*/){
for(int i=0;i<n;i++)
if(strcmp(Leader[i].name,name)==0)Leader[i].count++;
cout<<"请为候选人名投票,以#号结束:";
cin>>name;}
delete []name; //--------------
for(int i=0;i<mn;i++)
cout<<Leader[i].name<<"得票数为,"<<Leader[i].count<<endl;
}

int main()
{
if(Init(Leader,n))
Election(Leader,n);
for(int i=0;i<n;i++)
Free(&Leader[i]);
return 0;
}

/*
#include <string.h>
#include <iostream.h>

const int n=3;
const int LEN=10;

struct Person
{
char *name;
int count;
}Leader[n];

Person *Create(const char *name,int num)
{
Person *p = new Person;
int n = strlen(name);
p->name = new char[n+1];
strcpy(p->name,name);
p->name[n]='/0';
p->count = num;
return p;
}

void Free(Person *p)
{
if(p)
{
delete []p->name;
delete p;
p=0;
}
}

Person *Copy(Person *o,Person *s)
{
if(o&&s)
{
int n;
char *c = new char[n=strlen(s->name)+1];
if(c==0)return o;
strcpy(c,s->name);
c[n]='/0';
delete []o->name;
o->name = c;
}
return o;
}

bool Init(Person *p,int n)
{
if(!p)return false;
int count=0;
char *name = new char[LEN];
do{
cout<<"请输入候选人名,输入#号结束:";
cin>>name;
if(strstr(name,"#"))break;
Person *t = Create(name,0);
Copy(&p[count++],t);
delete t;
}while(count<n);
delete []name;
return count>0;
}
void Election(Person Leader[],int n)
{
char* name = new char[LEN];
cout<<"请为候选人名投票,以#号结束:";
cin>>name;
while(!strstr(name,"#"))//name!="#"
{
for(int i=0;i<n;i++)
if(strcmp(Leader[i].name,name)==0)Leader[i].count++;
cout<<"请为候选人名投票,以#号结束:";
cin>>name;
}
delete []name; //--------------
for(int i=0;i<n;i++)
cout<<Leader[i].name<<"得票数为,"<<Leader[i].count<<endl;
}
int main()
{
if(Init(Leader,n))
Election(Leader,n);
for(int i=0;i<n;i++)
Free(&Leader[i]);
return 0;
}
*/

/*
#include <iostream>
#include <string>
using namespace std;
*/

/*
void Election(Person Leader[],int n)
{
char *name=NULL;
cout<<"请输入候选人名:";
cin>>name;
while(name!="#")
{
for(int i=0;i<n;i++)
if(strcmp(Leader[i].name,name)==0)Leader[i].count++;
cout<<"请输入:";
cin>>name;
}
for(int i=0;i<n;i++)
cout<<Leader[i].name<<"得票数为,"<<Leader[i].count<<endl;
}
void main()
{
//struct *Leader=new Person;
//Person *p=Create("aaa",0);
Election(Leader,n);
}
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值