本文目錄一覽:
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;
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/200828.html