Jack's Blog

流淌的心,怎能阻拦,吹来的风,又怎能阻挡。

C language programming learning 8

Jacob posted @ Apr 27, 2017 11:26:03 PM in C语言 with tags C语言 Hit 哈工大 小白进阶,sse , 612 阅读

1.编程统计候选人的得票数。设有3个候选人zhang、li、wang(候选人姓名不区分大小写),10个选民,选民每次输入一个得票的候选人的名字,若选民输错候选人姓名,则按废票处理。选民投票结束后程序自动显示各候选人的得票结果和废票信息。要求用结构体数组candidate表示3个候选人的姓名和得票结果。
例如:
Input vote 1:li
Input vote 2:li
Input vote 3:Zhang
Input vote 4:wang
Input vote 5:zhang
Input vote 6:Wang
Input vote 7:Zhang
Input vote 8:wan
Input vote 9:li
Input vote 10:lii
Election results:
      li:3
   zhang:3
    wang:2
Wrong election:2

#include<stdio.h>
 
#include<string.h>
 
#define TheNumberOfTheELECTORATE 10
 
#define TheNumberOfTheCANDIDATE 3
 
struct candidate
 
{
 
char name[20];
 
int count;
 
}CANDIDATE[3]={"li",0,"zhang",0,"wang",0};
 
int main()
 
{
 
int i,j,flag=1,wrong=0;
 
char name[20];
 
for(j=1;j<=TheNumberOfTheELECTORATE;j++)
 
{
 
printf("Input vote %d:",j);
 
scanf("%s",name);
 
strlwr(name);
 
flag=1;
 
for(i=0;i<TheNumberOfTheCANDIDATE;i++)
 
{
 
if(strcmp(name,CANDIDATE[i].name)==0)
 
{
 
CANDIDATE[i].count++;
 
flag=0;
 
}
 
}
 
if(flag)
 
{
 
wrong++;
 
flag=0;
 
}
}
printf("Election results:\n");
for(i=0;i<TheNumberOfTheCANDIDATE;i++)
 
{
 
printf("%8s:%d\n",CANDIDATE[i].name,CANDIDATE[i].count); }
printf("Wrong election:%d\n",wrong);
 
return 0;
 
}

 


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter