马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* 小甲鱼的投票系统,经过我修过后的版本!
* 小甲鱼的C语言教程。
*/
#define CAND 4
#define VOTE CAND * 2
#define __SAVE_FILENAME__ "WINNER.txt"
struct VOTESYSTEM
{
char name[20]; // 姓名
int vNum; // 票数
}vNum[CAND];
char *winner();
int main ( int argc,char *argv [ ] )
{
int i, j;
FILE *fileopen = NULL;
char select[20];
for ( i = 0; i < CAND; i++)
{
printf("请输入候选人的姓名: ");
scanf("%s", vNum[i].name);
}
system("pause");
system("cls");
printf("候选人姓名: ");
for ( j = 0; j < CAND; j++)
{
printf("%-2s ", vNum[j].name );
}
putchar('\n');
for ( i = 0; i < VOTE; i++)
{
printf("请输入要投票的候选人: ");
scanf("%s", select);
for ( j = 0; j < CAND; j++)
{
if (strcmp(vNum[j].name, select) == 0)
{
vNum[j].vNum++;
break;
}
}
}
struct VOTESYSTEM vTemp;
for (i = 0; i < CAND - 1; i++)
{
for (j = 0; j < CAND - i - 1; j++)
{
if (vNum[j].vNum < vNum[j + 1].vNum)
{
vTemp = vNum[j];
vNum[j] = vNum[j + 1];
vNum[j + 1] = vTemp;
}
}
}
system("cls");
fileopen = fopen(__SAVE_FILENAME__, "w+");
printf("投票结果\n");
for ( i = 0; i < CAND; i++)
{
fprintf(fileopen, "候选人: %s\t票数: %d\n", vNum[i].name, vNum[i].vNum);
printf("候选人: %s\t票数: %d\n", vNum[i].name, vNum[i].vNum);
}
printf("获胜者: %s\n", winner());
fprintf(fileopen, "获胜者: %s\n", winner());
return 0;
}
char *winner()
{
int i = 0,
Winner = i;
for (int i = 1; i < CAND; i++)
{
if (vNum[Winner].vNum < vNum[i].vNum)
{
Winner = i;
}
}
return vNum[Winner].name;
}
|