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);
}
*/ 


原文链接: http://blog.csdn.net/t0nsha/article/details/1666196

你可能感兴趣的:(Election方法2)