c語言編寫選票,用c語言寫一個投票程序

本文目錄一覽:

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-hk/n/200828.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-06 11:28
下一篇 2024-12-06 11:28

相關推薦

  • Python程序需要編譯才能執行

    Python 被廣泛應用於數據分析、人工智能、科學計算等領域,它的靈活性和簡單易學的性質使得越來越多的人喜歡使用 Python 進行編程。然而,在 Python 中程序執行的方式不…

    編程 2025-04-29
  • python強行終止程序快捷鍵

    本文將從多個方面對python強行終止程序快捷鍵進行詳細闡述,並提供相應代碼示例。 一、Ctrl+C快捷鍵 Ctrl+C快捷鍵是在終端中經常用來強行終止運行的程序。當你在終端中運行…

    編程 2025-04-29
  • Python程序文件的拓展

    Python是一門功能豐富、易於學習、可讀性高的編程語言。Python程序文件通常以.py為文件拓展名,被廣泛應用於各種領域,包括Web開發、機器學習、科學計算等。為了更好地發揮P…

    編程 2025-04-29
  • AES加密解密算法的C語言實現

    AES(Advanced Encryption Standard)是一種對稱加密算法,可用於對數據進行加密和解密。在本篇文章中,我們將介紹C語言中如何實現AES算法,並對實現過程進…

    編程 2025-04-29
  • 學習Python對學習C語言有幫助嗎?

    Python和C語言是兩種非常受歡迎的編程語言,在程序開發中都扮演着非常重要的角色。那麼,學習Python對學習C語言有幫助嗎?答案是肯定的。在本文中,我們將從多個角度探討Pyth…

    編程 2025-04-29
  • Python購物車程序

    Python購物車程序是一款基於Python編程語言開發的程序,可以實現購物車的相關功能,包括商品的添加、購買、刪除、統計等。 一、添加商品 添加商品是購物車程序的基礎功能之一,用…

    編程 2025-04-29
  • 爬蟲是一種程序

    爬蟲是一種程序,用於自動獲取互聯網上的信息。本文將從如下多個方面對爬蟲的意義、運行方式、應用場景和技術要點等進行詳細的闡述。 一、爬蟲的意義 1、獲取信息:爬蟲可以自動獲取互聯網上…

    編程 2025-04-29
  • Python被稱為膠水語言

    Python作為一種跨平台的解釋性高級語言,最大的特點是被稱為”膠水語言”。 一、簡單易學 Python的語法簡單易學,更加人性化,這使得它成為了初學者的入…

    編程 2025-04-29
  • Vb運行程序的三種方法

    VB是一種非常實用的編程工具,它可以被用於開發各種不同的應用程序,從簡單的計算器到更複雜的商業軟件。在VB中,有許多不同的方法可以運行程序,包括編譯器、發佈程序以及命令行。在本文中…

    編程 2025-04-29
  • Python一元二次方程求解程序

    本文將詳細闡述Python一元二次方程求解程序的相關知識,為讀者提供全面的程序設計思路和操作方法。 一、方程求解 首先,我們需要了解一元二次方程的求解方法。一元二次方程可以寫作: …

    編程 2025-04-29

發表回復

登錄後才能評論