博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Election方法2
阅读量:6989 次
发布时间:2019-06-27

本文共 3169 字,大约阅读时间需要 10 分钟。

  hot3.png

// 工程名: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);
}
*/ 

原文链接:

转载于:https://my.oschina.net/dtec/blog/46718

你可能感兴趣的文章
关于selenium&amp;nbsp;IDE找不到元素
查看>>
[WCF安全系列]认证与凭证:用户名/密码认证与Windows认证
查看>>
戴尔科技集团调研表明 数字危机迫在眉睫
查看>>
英特尔Atom推16内核芯片更新至强单片机
查看>>
思科警告:旗下某些产品可能存在无法修补的WannaCrypt漏洞
查看>>
《中国人工智能学会通讯》——9.14 从多标记学习到标记分布学习
查看>>
Verizon PCI报告:防火墙合规性、安全测试是主要问题
查看>>
镖狮网裴向宇谈互联网营销的创业之路
查看>>
构建物联网云平台 “搞活”数据价值
查看>>
国家命脉产业涉密数据 需制度技术双保险
查看>>
硬盘灾后价格依旧:两年内恐难降价
查看>>
子域名枚举、探测工具AQUATONE 使用指南
查看>>
后Hadoop时代的大数据架构
查看>>
《数据虚拟化:商务智能系统的数据架构与管理》一 1.4 什么是数据虚拟化
查看>>
《逻辑与计算机设计基础(原书第5版)》——1.9 习题
查看>>
Joomla 对象注入漏洞分析报告
查看>>
停止去人性化吧 SOC应找回人的元素
查看>>
合力亿捷云客服3.0 开启“全员客服”新时代
查看>>
2016年全球网络空间安全大预测
查看>>
你知道国家教育部是如何实现全国数据大集中的吗?
查看>>