本文目录一览:
c语言选票程序怎么编写,用while写法?
首先需求分析:
投票人票数多少,一对一投或一对多。
由于你问题需求不明确,我按照投票人每人1票,只给1个候选人投票或弃权。
函数功能分三个,分别:
一、初始化(这个函数改成输入就是信息登记模块)。
二、投票函数(我用随机数实现投票过程)。
三、统计投票结果。
下面是代码:
#include stdio.h
#include stdlib.h
#include string.h
#include time.h
#define MCDE 5//候选人数量
#define MVR 100//投票人数量
typedef struct candidate
{
char name[20];//候选人名称
int vote;//得票数
}CDE;
typedef struct voter
{
char name[20];//投票人名称
int vote;//票数
}VTR;
void init(CDE **cdep,VTR **vtrp);//初始化候选人及投票人信息
void tp(CDE *cdes,VTR *vtrs);//开始投票(随机)
void tj(CDE *cdes,VTR *vtrs);//统计投票结果
int main()
{
CDE *cdes=NULL;
VTR *vtrs=NULL;
init(cdes,vtrs);
tp(cdes,vtrs);
tj(cdes,vtrs);
return 0;
}
void tj(CDE *cdes,VTR *vtrs)
{
int i=100,index,waiver=0,max=0,inxSave;
while(i0)
{
index=MVR-i;
if(vtrs[index].vote==-1)
waiver++;
i--;
}
printf("\n\n共%d人参与投票,%d人弃权\n",MVR-waiver,waiver);
i=MCDE;
while(i0)
{
index=MCDE-i;
printf("候选人%s得票%d张\n",cdes[index].name,cdes[index].vote);
if(maxcdes[index].vote)
max=cdes[index].vote,inxSave=index;
i--;
}
printf("得票最多的候选人为:%s,%d票\n",cdes[inxSave].name,cdes[inxSave].vote);
}
void tp(CDE *cdes,VTR *vtrs)
{
int i=MVR,index,n,n2;
srand(time(NULL));
while(i0)
{
index=MVR-i;
if(vtrs[index].vote0)
{
n=rand()%MCDE;//随机0~MCDE-1,给对应候选人投票
n2=rand()%2;//随机0~1,1投票,0弃权
if(n2)
{
printf("%s给候选人:%s投了%d票\n",vtrs[index].name,cdes[n].name,vtrs[index].vote);
cdes[n].vote+=vtrs[index].vote;
vtrs[index].vote=0;
}
else
{
printf("%s选择弃权\n",vtrs[index].name);
vtrs[index].vote=-1;
}
}
i--;
}
}
void init(CDE **cdep,VTR **vtrp)
{
static CDE cdes[MCDE]={{"小王",0},{"小陈",0},{"小李",0},{"小丽",0},{"小艾",0}};
static VTR vtrs[MVR];
int i=MVR,index;
char strN[20];
while(i0)
{
index=MVR-i;
strcpy(vtrs[index].name,"投票人");
itoa(index+1,strN,10);
strcat(vtrs[index].name,strN);
vtrs[index].vote=1;
i--;
}
*cdep=cdes,*vtrp=vtrs;
}
c语言 选票
#include stdio.h
#include stdlib.h
#include memory.h
#include malloc.h
#include string.h
#include conio.h
/* 存储运动员信息的文件 */
#define DATA1 "data1.txt"
/* 存储选票信息的文件 */
#define DATA2 "data2.txt"
typedef struct sportsman
{
int number;
char name[21];
int tickets;
struct sportsman *next;
} sportsman;
typedef struct ticket
{
char ticknum[8];
char votername[21];
char voteraddr[51];
int sportsman;
int scores;
struct ticket *next;
} ticket;
int SIZE1, SIZE2;
sportsman *head1, *tail1;
ticket *head2, *tail2;
void init();
void cleanup();
void create();
void process();
sportsman *sortlist1();
ticket *sortlist2();
void showdetail();
void showtop10();
void clearlist();
void init()
{
SIZE1 = sizeof(sportsman);
SIZE2 = sizeof(ticket);
head1 = tail1 = (sportsman *)malloc(SIZE1);
head2 = tail2 = (ticket *)malloc(SIZE2);
memset(head1, 0, SIZE1);
memset(head2, 0, SIZE2);
}
void create()
{
FILE *infile = fopen(DATA1, "r");
if (infile == NULL)
{
printf("无法打开文件1\n");
cleanup();
exit(1);
}
while (1)
{
char nm[21];
int num;
memset(nm, 0, 21);
num = 0;
fscanf(infile, "%d", num);
if (num == 0)
break;
sportsman *node = (sportsman *)malloc(SIZE1);
node-number = num;
node-tickets = 0;
fscanf(infile, "%s", node-name);
tail1-next = node;
tail1 = node;
}
tail1-next = NULL;
fclose(infile);
infile = fopen(DATA2, "r");
if (infile == NULL)
{
printf("无法打开文件2\n");
cleanup();
exit(1);
}
while (1)
{
char num[8];
memset(num, '\0', 8);
fscanf(infile, "%s", num);
if (num[0] == '\0')
break;
ticket *node = (ticket *)malloc(SIZE2);
memset(node, 0, SIZE2);
strcpy(node-ticknum, num);
fscanf(infile, "%s %s %d", node-votername, node-voteraddr, node-sportsman);
tail2-next = node;
tail2 = node;
}
fclose(infile);
}
void clearlist()
{
sportsman *q1, *p1;
q1 = p1 = head1-next;
ticket *q2, *p2;
q2 = p2 = head2-next;
while (p1 != NULL)
{
q1 = q1-next;
free(p1);
p1 = q1;
}
while (p2 != NULL)
{
q2 = q2-next;
free(p2);
p2 = q2;
}
tail1 = head1;
tail2 = head2;
head1-next = NULL;
head2-next = NULL;
}
void cleanup()
{
free(head1);
free(head2);
}
sportsman *sortlist1(sportsman *head)
{
sportsman *cursor, *first, *tail, *prev, *max;
first = NULL;
while (head != NULL)
{
for (cursor = max = head; cursor-next != NULL; cursor = cursor-next)
{
if (cursor-next-tickets max-tickets)
{
prev = cursor;
max = cursor-next;
}
}
if (first == NULL)
first = tail = max;
else
tail = tail-next = max;
if (max == head)
head = head-next;
else if (prev)
prev-next = max-next;
}
if (first != NULL)
{
tail-next = NULL;
tail1 = tail;
}
return first;
}
ticket *sortlist2(ticket *head)
{
ticket *cursor, *first, *tail, *prev, *max;
first = NULL;
while (head != NULL)
{
for (cursor = max = head; cursor-next != NULL; cursor = cursor-next)
{
if (cursor-next-scores max-scores)
{
prev = cursor;
max = cursor-next;
}
}
if (first == NULL)
first = tail = max;
else
tail = tail-next = max;
if (max == head)
head = head-next;
else if (prev)
prev-next = max-next;
}
if (first != NULL)
{
tail-next = NULL;
tail2 = tail;
}
return first;
}
void process()
{
if (head1-next != NULL || head2-next != NULL)
clearlist();
create();
sportsman *p1 = head1-next;
ticket *p2;
int score = 9;
while (p1 != NULL)
{
p2 = head2-next;
while (p2 != NULL)
{
if (p2-sportsman == p1-number)
++p1-tickets;
p2 = p2-next;
}
p1 = p1-next;
}
head1-next = sortlist1(head1-next);
p2 = head2-next;
while (p2 != NULL score 0)
{
p1 = head1-next;
while (p1 != NULL)
{
if (p2-sportsman == p1-number)
{
p2-scores += score;
break;
}
p1 = p1-next;
}
--score;
p2 = p2-next;
}
head2-next = sortlist2(head2-next);
showtop10();
}
void showtop10()
{
printf("最佳前10名运动员名单:\n");
sportsman *p1 = head1-next;
int i = 0;
ticket *p2 = head2-next;
while (p1 != NULL i 10)
{
printf("%d\t%s\t%d\n", i+1, p1-name, p1-tickets);
p1 = p1-next;
++i;
}
printf("\n最佳前10名投票人名单:\n");
i = 0;
while (p2 != NULL i 10)
{
printf("%d\t%s\t%\n", i+1, p2-votername, p2-scores);
p2 = p2-next;
++i;
}
}
void showdetail()
{
printf("详细投票信息:\n");
int pagesize = 10;
ticket *p1 = head2-next;
while (p1 != NULL)
{
printf("选票编号: %s\n", p1-ticknum);
printf("投票人姓名:%s\n", p1-votername);
printf("投票人住址:%s\n", p1-voteraddr);
int sportsman = p1-sportsman;
printf("运动员编号:");
if (sportsman 10)
printf("0%d\n", sportsman);
else
printf("%d\n", sportsman);
if (--pagesize == 0)
{
pagesize = 10;
printf("\n按任意键继续。。。");
getch();
}
p1 = p1-next;
}
}
void main()
{
printf("欢迎使用简单投票系统\n");
char choice;
init();
while (1)
{
printf("功能选择:\n");
printf("a. 统计\tb. 核对选票\tc. 退出\n");
choice = getchar();
while (getchar() != '\n');
switch (choice)
{
case 'a':
process();
break;
case 'b':
showdetail();
break;
case 'c':
cleanup();
exit(0);
}
printf("\n按任意键继续。。。");
getchar();
system("cls");
}
}
c语言 投票程序
给你写一个:
#includestdio.h
#define M 100 //投票人数上限
#define N 3 //候选人数
struct
{
char code; //代号
int score; //得分
} candidates[N], st; //候选人结构体数组
int main()
{
char candidate[M][N]; //投票
int n, c=0; //n: 实际投票人数,c: 有效选票
int i,j,k,t;
char tmp[N+1]; //存放投票的中间变量
int s[N]={5,3,2};
for(i=0; iN; ++i)
{
printf("输入第 %d 个候选人的代号:", i+1);
scanf("%c", candidates[i].code);
fflush(stdin); //清除回车符
candidates[i].score=0;
}
printf("输入投票人数:");
scanf("%d",n);
for(i=0; in; ++i) //输入选票
{
printf("输入第 %d 张选票:", i+1);
scanf("%s",tmp);
candidate[i][0]=tmp[0];
candidate[i][1]=tmp[1];
candidate[i][2]=tmp[2];
if(tmp[0]==tmp[1] || tmp[1]==tmp[2] || tmp[0]==tmp[2])
printf("该票无效:%s\n", tmp);
else
{
//tmp[0]号候选人加5分;
//tmp[1]号候选人加3分;
//tmp[2]号候选人加2分;
c++;
for(j=0; jN; ++j)
for(k=0; kN; ++k)
if(candidates[k].code==tmp[j])
candidates[k].score+=s[j];
}
}
//得分排序
for(i=0; iN-1; ++i)
{
k=i;
for(j=i+1; jN; ++j)
if(candidates[j].scorecandidates[k].score)
k=j;
st=candidates[i];
candidates[i]=candidates[k];
candidates[k]=st;
}
if(candidates[0].score==candidates[1].score)
printf("前两名得分是否相同,都是 %d 分,重新投票。", candidates[0].score);
else
{
printf("候选人\t得分\n");
for(i=0; iN; ++i)
printf("%c\t%d\n", candidates[i].code, candidates[i].score);
printf("祝贺 %c 当选冠军!\n", candidates[0].code);
}
return 0;
}