c语言编写一个选票统计程序4个候选人分别用ABCD表示,现有20人进行投票,每人只能选ABCD4个人中的一个
3个回答
展开全部
#include <stdio.h>
void main()
{
int i=1;
char j;
int num[4]={0,0,0,0};
printf("共4位候选人,分别为A,B,C,D,开始投票\n");
while(i<=20)
{
printf("请输入第%d个的选票",i);
scanf("%c",&j);
switch(j)
{
case 'A':num[0]++;i++;break;
case 'B':num[1]++;i++;break;
case 'C':num[2]++;i++;break;
case 'D':num[3]++;i++;break;
default:printf("选票无效!请重新输入\n");break;
}
getchar();
}
printf("A获得%d张选票\n",num[0]);
printf("B获得%d张选票\n",num[1]);
printf("C获得%d张选票\n",num[2]);
printf("D获得%d张选票\n",num[3]);
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
main()
{
int i,a=0,b=0,c=0,d=0;
char a[20];
printf("");
for(i=0;i<20;i++)
{
scanf("%c",&a[i]);
switch(a[i])
{
case 'A':a++;i++;break;
case 'B':b++;i++;break;
case 'C':c++;i++;break;
case 'D':d++;i++;break;
default:printf("选票无效!请重新输入\n");break;
}
}
printf("A获得%d张选票\n",a);
printf("B获得%d张选票\n",b);
printf("C获得%d张选票\n",c);
printf("D获得%d张选票\n",d);
}
{
int i,a=0,b=0,c=0,d=0;
char a[20];
printf("");
for(i=0;i<20;i++)
{
scanf("%c",&a[i]);
switch(a[i])
{
case 'A':a++;i++;break;
case 'B':b++;i++;break;
case 'C':c++;i++;break;
case 'D':d++;i++;break;
default:printf("选票无效!请重新输入\n");break;
}
}
printf("A获得%d张选票\n",a);
printf("B获得%d张选票\n",b);
printf("C获得%d张选票\n",c);
printf("D获得%d张选票\n",d);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
void count(char *point)
{
int first,second,third,forth;
first = second = third = forth = 0;
for(i = 0; i < 20; i++)
{
if(*point == "A")
{
first ++;
}
else if(*point == "B")
{
second ++;
}
else if(*point == "C")
{
third ++;
}
else
{
forth ++;
}
point ++;
}
printf("A = %d",first );
printf("B = %d",second );
printf("C = %d",third );
printf("D = %d",forth );
}
{
int first,second,third,forth;
first = second = third = forth = 0;
for(i = 0; i < 20; i++)
{
if(*point == "A")
{
first ++;
}
else if(*point == "B")
{
second ++;
}
else if(*point == "C")
{
third ++;
}
else
{
forth ++;
}
point ++;
}
printf("A = %d",first );
printf("B = %d",second );
printf("C = %d",third );
printf("D = %d",forth );
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询