當前位置:首頁 » 編程語言 » c語言投票編輯
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言投票編輯

發布時間: 2022-04-21 00:40:21

A. c語言編寫的投票程序

我這個比二樓的要簡單一些.你在定義中加入整形flag=0;
以下是在for語句中的修改:
for(j=0;j<2;j++)
{
scanf("%d",&k);
if(k==flag) //判斷是否和先前輸入一樣
{
printf("errer!\nselect again please:");
j=j-1;
}
else
{
flag=k;
switch(k)
{
case 6001:
count1++;
break;
case 6002:
count2++;
break;
case 6003:
count3++;
break;
case 6004:
count4++;
break;
case 6005:
count5++;
break;
case -1:
exit();
default:
break;
}
if(j==1)//加入的兩行,flag置0
flag=0;
}
}

B. c語言編寫一個選票統計程序4個候選人分別用ABCD表示,現有20人進行投票,每人只能選ABCD4個

摘要 main()

C. C語言編寫一個模擬投票系統

#define PRT(j,k) printf("j的票數為%d",k); 這里你如果想要J顯示你想要的內容的話直接 把j放在雙引號的外面就可以了, #define PRT(j,k) printf(j「的票數為%d",k); 但是這里注意的是你傳入的j的值就一定要是字元串形式的,就比如 調用PRT(」jack「,10) 那麼顯示出來就會是 jack的票數為10 調試了一下發現要把hxr定義為hxr[2]才行 想了下也就是hxr要把\0也接收到才行 首先你定義hxr為char型,一個char型,你需要使用 %c 去接受,改成scanf("%c",&hxr); 還有 scanf以後注意謹慎使用,用多了以後你會發現 scanf會很容出問題。

D. 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(i>0)

{

index=MVR-i;

if(vtrs[index].vote==-1)

waiver++;

i--;

}

printf(" 共%d人參與投票,%d人棄權 ",MVR-waiver,waiver);

i=MCDE;

while(i>0)

{

index=MCDE-i;

printf("候選人%s得票%d張 ",cdes[index].name,cdes[index].vote);

if(max<cdes[index].vote)

max=cdes[index].vote,inxSave=index;

i--;

}

printf("得票最多的候選人為:%s,%d票 ",cdes[inxSave].name,cdes[inxSave].vote);

}

void tp(CDE *cdes,VTR *vtrs)

{

int i=MVR,index,n,n2;

srand(time(NULL));

while(i>0)

{

index=MVR-i;

if(vtrs[index].vote>0)

{

n=rand()%MCDE;//隨機0~MCDE-1,給對應候選人投票

n2=rand()%2;//隨機0~1,1投票,0棄權

if(n2)

{

printf("%s給候選人:%s投了%d票 ",vtrs[index].name,cdes[n].name,vtrs[index].vote);

cdes[n].vote+=vtrs[index].vote;

vtrs[index].vote=0;

}

else

{

printf("%s選擇棄權 ",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(i>0)

{

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;

}

E. C語言投票程序

提示輸入,可以直接輸入下標,0~2。
這樣就不需要判斷了。還能避免輸入錯誤。
提示輸入的文字建議用循環動態生成。這樣方便後期擴展,比如變成5個人。
提示輸入的文字大致可以這樣寫:
把數組大小定義成常量,方便修改,比如#define SIZE 3
printf("請輸入對應數字,為下列人物投票:");
for(i=0;i<SIZE;i++)
printf("%d:%s ",i,arr[i].name);
這樣後面輸入只要輸入結構數組下標,就可以直接++,無需判斷。

F. c語言的投票程序如何設計

給你寫一個:
#include<stdio.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; i<N; ++i)
{
printf("輸入第 %d 個候選人的代號:", i+1);
scanf("%c", &candidates[i].code);
fflush(stdin); //清除回車符
candidates[i].score=0;
}
printf("輸入投票人數:");
scanf("%d",&n);
for(i=0; i<n; ++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; j<N; ++j)
for(k=0; k<N; ++k)
if(candidates[k].code==tmp[j])
candidates[k].score+=s[j];
}
}
//得分排序
for(i=0; i<N-1; ++i)
{
k=i;
for(j=i+1; j<N; ++j)
if(candidates[j].score>candidates[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; i<N; ++i)
printf("%c\t%d\n", candidates[i].code, candidates[i].score);
printf("祝賀 %c 當選冠軍!\n", candidates[0].code);
}
return 0;
}

G. C語言投票程序編寫

樓上不是用C語言寫的,而是C++
而且樓上的結果是不正確的,比如說輸入四個abc,一個cba,應該是a位冠軍,但是樓上的顯示是c為冠軍
下面是我的程序,經過初步的測試是符合要求的

#include"stdio.h"
int main()
{
char a[3];
int n,i,j,na=0,nb=0,nc=0;
printf("請輸入投票的人數\n");
scanf("%d",&n);
getchar(); //getchar是為了讀取輸入的回車,保證後面讀取的正確
for(i=1;i<=n;i++)
{
printf("請第%d位投票:\n",i);
gets(a); //輸入時中間不要加空格
if(a[0]==a[1]||a[1]==a[2]||a[0]==a[2])
{
printf("此票無效\n");
}
else
{
if(a[0]=='a')na+=5;
if(a[0]=='b')nb+=5;
if(a[0]=='c')nc+=5;
if(a[1]=='a')na+=3;
if(a[1]=='b')nb+=3;
if(a[1]=='c')nc+=3;
if(a[2]=='a')na+=2;
if(a[2]=='b')nb+=2;
if(a[2]=='c')nc+=2;
}
}
if(na>nb&&na>nc)
printf("恭喜a當選冠軍!\n");
else if(nb>na&&nb>nc)
printf("恭喜b當選冠軍!\n");
else if(nc>na&&nc>nb)
printf("恭喜c當選冠軍!\n");
else
printf("重新投票\n");
//printf("得分a:%d b:%d c:%d\n",na,nb,nc); 加上這句可以顯示最後的得分情況
}

H. C語言編寫投票及排序系統(框架)

#include <stdio.h>
#include <stdlib.h>

typedef struct node
{ // 候選人結構
char name[8];
int num;
int score;
int tax;
}Node;

void shellSort( Node **, int );

int main(void)
{
int n = 0;
Node * pArray[9]={};//指針數組,長度9
int count = 0;
//int status = 1;
int vote = -1;

printf("Input the number of the candidates(1-9):\n");
scanf("%d", &n);
while(getchar()!='\n')
{
;
}

while (n>9 || n<1)
{
if (n>9)
{
printf("No, there cannot be so many candidates. Retry.\n");
}
else
{
printf("No candidates? It cannot be! Retry!\n");
}
scanf("%d", &n);
while(getchar()!='\n')
{
;
}
}

for (count=0; count<n; count++)
{
pArray[count] = (Node *)malloc(sizeof(Node));
pArray[count]->num = count+1;
pArray[count]->tax = 0;
pArray[count]->score = 0;
printf("Input No.%d candidate's name:\n", count+1);
gets(pArray[count]->name);
}

while (vote)
{
printf("Now, let us vote:\n*************\n");
for (count=0; count<n; count++)
{
printf("%d. %s\n", count+1, pArray[count]->name);
}
printf("0.quit\n*************\n");
scanf("%d", &vote);
while(getchar()!='\n')
{
;
}

while (vote<0 || vote>n)
{
printf("No joke, thank you. Revote.\n");
scanf("%d", &vote);
while(getchar()!='\n')
{
;
}

}
if (vote>0&&vote<=n)
{
pArray[vote-1]->score++;
}
}

printf("Finish voting. Let's find the winner......\n\n");

shellSort( pArray, n );

for (count=0; count<n; count++)
{
pArray[count]->tax=count+1;
printf("%d. %s %d votes.\n", count+1, pArray[count]->name, pArray[count]->score);
}

for(count=0; count<n; count++)
{
free(pArray[count]);
pArray[count] = NULL;
}

return 0;
}

void shellSort( Node *p[], int len )
{
const int Length = len;
int i = 0;
int j = 0;
int gap = 0;
Node *temp = NULL;

gap = Length/2;

while (gap>0)
{
for (i=gap; i<Length; i++)
{
j = i - gap;
temp = *(p+i);

while ( (j>=0) && (p[j]->score < temp->score) )
{
*(p+j+gap) = *(p+j);
j = j - gap;
}

*(p+j+gap) = temp;
}
gap /= 2;
}
}

運行與輸入方式:

1.程序提示,輸入候選人人數, 輸入數字(1-9),大於9或小於1或者輸入不合法字元會提示錯誤。

2.程序提示依次輸入候選人名字,不能超過7個字元(少了點,你題目給的,數組拉長點會更安全些)

3.按照程序提示的數字開始投票,或者退出。

4.投票過程結束後,程序調用shellSort(希爾排序)函數對所有參選人按照得票數目進行降序排序,並將排序結果輸出。

目前沒有發現什麼嚴重bug,你先用著,如果出現bug再告訴我,我再改,寫了一個多小時,累啊。