c投票系统源码
① 有三个候选人,每个选民只能投票选一人,要求用C语言编写一个投票系统,不用结构体数组编写
#include"stdio.h"
#include"string.h"
struct People
{
char name[20];
int count;
}leader[3]={"zhang",0,"li",0,"sun",0};
void main()
{
char leader_name[20];
int i,j;
for(i=1;i<=10;i++)
{
scanf("%s",leader_name);
for(j=0;j<3;j++)
if(strcmp(leader_name,leader[j].name)==0)
leader[j].count++;
}
printf("the result:\n");
for(i=0;i<3;i++)
printf("%5s:%d\n",leader[i].name,leader[i].count);
}
② 急急急急急急!!!!一个关于用C语言写投票选举系统。
在strcpy(stu[0].name,gets(a1)); 的前面加入
fflush(stdin);,保证可以。
原因就是:scanf("%s",str1); 这条指令输入后留下了个回车符号,所以要清除此处的输入流。
③ C语言投票程序
提示输入,可以直接输入下标,0~2。
这样就不需要判断了。还能避免输入错误。
提示输入的文字建议用循环动态生成。这样方便后期扩展,比如变成5个人。
提示输入的文字大致可以这样写:
把数组大小定义成常量,方便修改,比如#define SIZE 3
printf("请输入对应数字,为下列人物投票:");
for(i=0;i<SIZE;i++)
printf("%d:%s ",i,arr[i].name);
这样后面输入只要输入结构数组下标,就可以直接++,无需判断。
④ 求C语言投票系统源代码代码的注释。
#include<stdio.h>
#include<conio.h>
struct hxr
{
int m;
int point;
} hxr[3]; //选票结构体
void main()
{
int i=0,j=0,k,temp=0;
char x,y,z;
struct hxr hxr[3];
for(i=0;i<3;i++)//初始化选票结构体
{
hxr[i].m=i;
hxr[i].point=0;
}
printf("请输入投票总人数\n");
scanf("%d",&k);
printf("请输入投票\n");
getchar();
for(i=1;i<=k;i++)
{
scanf("%c%c%c",&x,&y,&z); //输入选票
getchar();
if(x!=y&&x!=z&&y!=z)//3个选票不能相同否则为无效选票
{
hxr[0].point+=(x=='a'?5:(y=='a'?3:(z=='a'?2:0))); //统计a的得票数,切输入3个选票从左到右得分分别是5.3.2
hxr[1].point+=(x=='b'?5:(y=='b'?3:(z=='b'?2:0)));
hxr[2].point+=(x=='c'?5:(y=='c'?3:(z=='c'?2:0)));
printf(" point: %d %d %d\n",hxr[0].point,hxr[1].point,hxr[2].point); //输出a,b,c的得分
continue;
}
printf("此票无效\n");
}
for(i=0;i<3;i++)//分别输出a,b,c的得分
{
if(temp<hxr[i].point)
{
temp=hxr[i].point;
j='a'+i;
}
printf("people %c point %d\n",'a'+hxr[i].m,hxr[i].point);
}
if(((hxr[0].point==hxr[1].point)&&hxr[0].point>=hxr[2].point&&hxr[1].point>=hxr[2].point ) ||( (hxr[0].point==hxr[2].point)&&hxr[0].point>=hxr[1].point&&hxr[2].point>=hxr[1].point) ||((hxr[1].point==hxr[2].point)&&hxr[1].point>=hxr[0].point&&hxr[2].point>=hxr[0].point))
printf("重新投票\n");
else
{ printf("祝贺%c当选冠军\n",j); }
getchar();
}
⑤ 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); 加上这句可以显示最后的得分情况
}
⑥ C语言选民投票管理系统代码
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct person
{
char name[10];
int num;
int age;
} leader
void main()
{
void start();
void admin();
void customer();
char a;
scanf("&c",a);
if(a==#) admin();
if(a==*) customer();
}
void start()
{
printf("******************");
printf("*欢迎进入投票系统*");
printf("*********************");
printf("#管理穿口");
printf("*选民进口");
}
void admin()
{
int a ;
void admin1();
printf("**********");
printf("*管理员才可以进入");
printf("*请输入密码:");
printf("password: *");
printf("********");
scanf("&d",a);
clrscr();
for(i=1;i<3;i++)
{if (a==123456)<br> {printf("**********");<br> printf("登陆成功");<br> printf("**********");<br> admin1();<br> clrscr();<br> break;}
else
{printf("***********");<br> printf("*密码错误,请重新输入,您共有三次机会")<br> }
}
void admin1()
{
int b;
printf("**********");
printf("*1.查询候选人信息*");
printf("*2.查询候选人票数*");
printf("*3.录入候选人信息*");
printf("*请输入: *");
printf("*************");
scanf("&d",b);
if (b==1) admin11();
if (b==2) admin12();
if (b==3) admin13();
}
void admin13()
{
int i;
printf("请输入候选人信息:")
for(i=0;i<3;i++)
scanf("% s% d% %d s",&leader[i].name,&leader[i].num,&leader[i].age,leader[i],addr);
save();
}
void save ()
{
file *fp;
int i;
if(fp=fopen("leader_dat","wb"==NULL))
{
printf("cannot open file\n");
return;
}
for(i=0;i<3;i++)
if(fwrite(&leader[i],sizeof(struct leader_type,1,fp)!=1)
printf("file write error\n");
fclose(fp);
}
void admin12()
{
int i;
file*fp;
if((sp=fopen("leader_dat""rb"))=NUll
{
printf("cannot open file\n");
return;
}
for(i=0;i<3;i++)
{
fread (&leader[i],sizeof(struct leader_type),1,fp);
printf("%-10s %4d % 4d %-15s\n",leader[i].name,leader[i].num,leader[i].age,leader[i].addr)
}
fclose(fp);
}
customer()
{
int c;
printf("*************");
printf("*1.查询候选人信息*");
printf("*2.进入投票系统*");
printf("请输入: ");
scanf("%d,&c");
if(c==1) admin12();
if(c==2) vote();
}
void vote()
{
int i,j;
admin12();
for(i=1;i<=10;i++)
{
scanf("%s",leader_name);
if(strcmp(leader_name,leader[j],name==0)) leader[j].count++;
}
}
⑦ 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会很容出问题。
⑧ 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再告诉我,我再改,写了一个多小时,累啊。