c语言编程:设计一个投票程序,每输入一个候选人名字,其票数自增一。最后输出各自得票结果

发布时间:2024-06-26 10:19 发布:上海旅游网

问题描述:

例如,候选人为张,王,李,赵。输入张,张,李,李,赵。则最终输出得票结果为张:2 王:0 李:2 赵:1
是高手就露一小手,忽忽!

问题解答:

你先等几天…

方法一:
#include <stdio.h>
#include <string.h>
void main()
{
char *name[]={"张","王","李","赵"};
char *substring;
int count[4]={0,0,0,0};
char tp[100];
char sep[]=",";//注意此分隔符是中文输入法中的逗号
int i=0;

gets(tp);
printf("%s\n",tp);
substring=strtok(tp,sep);//以分隔符取字串
while(substring!=NULL)
{
printf("%s\n",substring);
for(i=0;i<4;i++)
{
if(!strcmp(substring,name[i]))//比较字符串,匹配时
count[i]++;
}

substring=strtok(NULL,sep);//继续下一轮截取
}
for(i=0;i<4;i++)
printf("%s%d\n",name[i],count[i]);
}

/*为了方便VC和TC兼容,不用中文,用拼音.另外方便投票,改用选择,不是输姓名. */
#include <stdio.h>
struct houxieren{
char name[10];
int piaoshu;
}ren[5]={{"zhang_san",0},{"li_shi",0},{"wang_wu",0},{"zhao_liu",0},{"qian_qi",0}};

main(){
char ch,j;
int i;

while(1){
system("cls");

printf("\n\n\n\n\n **********Tou piao chen xu*********");
printf("\n\n........1.kai shi tou piao");
printf("\n\n........2.xie shi tou piao jie guo");
printf("\n\n........3.tui chu chen xu");
while(1){
printf("\n\nqing xian zhe(1-3):");
fflush(stdin);
ch=getche();

if(ch=='1'||ch=='2'||ch=='3')
break;
}

system("cls");

switch(ch){

case '1':

while(1){
printf("\n\n\n.....");
printf("%d.tou piao jie shu\n\n\n.....",0);
for(i=0;i<5;i++){
printf("%d.%s ",i+1,ren[i].name);
}
fflush(stdin);
while(1){

printf("\n\nqing xie zhe(0-5):");

j=getche();
fflush(stdin);
if(j>='0'&&j<'6')
break;
}

if(j=='0')
break;
ren[j-49].piaoshu++;
printf("%s \n",ren[j-49].name);
}

break;
case '2':
for(i=0;i<5;i++)
printf("\n%s %d piao.",ren[i].name,ren[i].piaoshu);
getch();
break;
case '3':
printf("\n\nxie xie shi yong! press any key....");
getch();
exit(0);
}

}
}

#include <stdio.h>
#include <stdlib.h>
void main()
{int i;int j;int k;int p;int z;int y;
i=0;j=0;k=0;p=0;
start:
printf("******请投票*****\n");
printf("*投1选择后先人张*\n");
printf("*投2选择后先人王*\n");
printf("*投3选择后先人李*\n");
printf("*投4选择后先人赵*\n");
printf("*投5结束投票*****\n");
printf("*****************\n");

scanf("%d",&z);
switch(z)
{case(1):printf("投票成功\n");system("cls");i++;goto start;break;
case(2):printf("投票成功\n");system("cls");j++;goto start;break;
case(3):printf("投票成功\n");system("cls");k++;goto start;break;
case(4):printf("投票成功\n");system("cls");p++;goto start;break;
case(5):
{system("cls");
printf("是否要退出投票\n");
printf("1:确定\n");
printf("2:按错了");
scanf("%d",&y);
if(y==1)
{system("cls");
break;}
else
{system("cls");
goto start;}
}
default:printf("投票有误请重新输入");system("cls");goto start;break;
}
printf("张的票数为%d\n",i);
printf("王的票数为%d\n",j);
printf("李的票数为%d\n",k);
printf("赵的票数为%d",p);
}

热点新闻