|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 cui666 于 2021-6-27 23:38 编辑
/******************一个投票的小程序****************************/
#include <stdio.h>
#include <string.h>
#include <conio.h>
struct ticketlibrary //结构体
{
char name[10];
int num;
}
a = {"小c",0},
b = {"小j",0},
c = {"小d",0};
void main()
{
int i ;
char str[10];
char ch;
int m;
char mn[10];
/***********************投票模块*************************/
for(i=1; i<=5; i++) //循环投票
{
printf("请输入票选人(第%d票,还有%d票):",i, 5-i);
scanf("%s",&str);
if(0==strcmp(str,a.name)) //strcmp比较字符串是否相同
{
(a.num)++;
}
else if(0==strcmp(str,b.name))
{
(b.num)++;
}
else if(0==strcmp(str,c.name))
{
(c.num)++;
}
/****************检错*********************/
else
{
printf("输入错误!\n");
printf("重新输入请输入a\n");
printf("其它视作弃票\n");
if(getch()=='a'); //*错误位置* 输不输入a都会i--,导致死循环不能弃票(去掉分号)
{
i--;
}
}
}
/********************排名模块*********************/
if( (b.num) > (a.num) )
{
m = (a.num); strcpy(mn , a.name);
(a.num) = (b.num); strcpy(a.name , b.name);
(b.num) = m; strcpy(b.name , mn);
}
if( (c.num) > (a.num) )
{
m = (a.num); strcpy(mn , a.name);
(a.num) = (c.num); strcpy(a.name , c.name);
(c.num) = m; strcpy(c.name , mn);
}
if( (b.num) > (c.num) )
{
m = (c.num); strcpy(mn , c.name);
(c.num) = (b.num); strcpy(c.name , b.name);
(b.num) = m; strcpy(c.name, mn);
}
/***************输出模块*******************/
printf("【1】%s\t%d\n",a.name,a.num);
printf("【2】%s\t%d\n",b.name,b.num);
printf("【3】%s\t%d\n",c.name,c.num);
}
if(getch()=='a');
别跟分号啊 ,空语句了
|
|