本文主要是介绍C语言使用结构体模拟选举的小应用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
C语言结构体的模拟选举小应用
- 现在有三个被选举人,五个投票人
- 最后以票高者获胜
小应用中用到了C语言中的结构体作为主要编写主体
上代码
#include <stdio.h>
#include <string.h>struct XuanMin
{char name[32];int tickets;
};int main()
{struct XuanMin xm[3];struct XuanMin max;struct XuanMin max1;int feiPiao = 0;int i;int len;int j;int mark = 0;int total = 5;char tmpName[32];//初始化选民信息len = sizeof(xm)/sizeof(xm[0]);for(i = 0; i<len; i++){xm[i].tickets = 0;printf("请输入第%d个选民的名字:\n",i+1);scanf("%s",xm[i].name);}//唱票环节for(i=0;i<5;i++){mark = 0;printf("请输入你投给谁:\n");memset(tmpName,'\0',sizeof(tmpName)); //每次清空一下scanf("%s",tmpName);//输入选中的选民名字,像拆开一个选票,看到名字一样for(j = 0;j<len; j++){ //拿到名字,对应候选人票数加1if(strcmp(tmpName, xm[j].name) == 0){xm[j].tickets++;mark = 1;}}if(mark == 0){printf("没有此候选人,放弃\n");feiPiao++;}}//公布结果for(i=0;i<len;i++){printf("名字:%s,票数:%d\n",xm[i].name,xm[i].tickets);}max = xm[0];max1 = xm[0];for(i=1;i<len;i++){if(max.tickets < xm[i].tickets){max = xm[i];}if(max.tickets == xm[i].tickets){max1 = xm[i];}}if((strcmp(max.name,max1.name)!=0)&&(max.tickets == max1.tickets))//有种情况就是相同票数的被选举人的情况分开公布{printf("%s和%s以%d票同时当选!!废票是%d\n",max.name,max1.name,max1.tickets,feiPiao);}else{printf("%s以%d票当选!!废票是%d\n",max.name,max.tickets,feiPiao);}return 0;
}
结果如下:
最高票数的情况
相同票数的情况
证实了票高者获胜的事实
还有一种写法
#include <stdio.h>
#include <string.h>
#include <stdlib.h>struct XuanMin
{char name[32];int tickets;
};struct XuanMin* initXms(struct XuanMin *p,int *pn)
{int i;if(p == NULL){printf("请输入有几个人参选:\n");scanf("%d",pn);p = (struct XuanMin*)malloc(*pn*sizeof(struct XuanMin));}for(i=0;i<*pn;i++){p->tickets = 0;printf("请输入第%d个选民的名字:\n",i+1);scanf("%s",p->name);p++;}return p-*pn;
}void printXms(struct XuanMin *p,int len)
{int i;for(i=0;i<len;i++){printf("名字:%s,票数:%d\n",p->name,p->tickets);p++;}
}int doVot(struct XuanMin *p, int len)
{int i;int j;int feiPiao = 0;int mark;char tmpName[32];struct XuanMin *pbak = p;for(i=0;i<5;i++){mark = 0;printf("请输入你投给谁:\n");memset(tmpName,'\0',sizeof(tmpName)); //每次清空一下scanf("%s",tmpName);//输入选中的选民名字,像拆开一个选票,看到名字一样p = pbak;for(j = 0;j<len; j++){ //拿到名字,对应候选人票数加1if(strcmp(tmpName, p->name) == 0){p->tickets++;mark = 1; }p++;}if(mark == 0){printf("没有此候选人,放弃\n");feiPiao++;}}return feiPiao;
}struct XuanMin* getMax(struct XuanMin *p, int len)
{struct XuanMin* max;int i;max = p;for(i=0;i<len;i++){if(max->tickets < p->tickets){max = p;}p++;}return max;
}int main()
{struct XuanMin *xm = NULL;struct XuanMin *final;int total = 0;xm = initXms(xm,&total);printXms(xm,total);int feip = doVot(xm,total);printf("废票数是:%d\n",feip);printXms(xm,total);final = getMax(xm,total);printf("%s以%d票当选!!废票是%d\n",max->name,final->tickets,feip);return 0;
}
更高深的写法
#include <stdio.h>
#include <string.h>
#include <stdlib.h>struct XuanMin
{char name[32];int tickets;
};void initXms(struct XuanMin **pxm,int *pt)
{int i;//*pt = 0;//*pxm = NULL;if(*pxm == NULL){printf("请输入有几个人参选:\n");scanf("%d",pt);printf("一共%d个人参选\n",*pt);*pxm = (struct XuanMin*)malloc(*pt * sizeof(struct XuanMin));}for(i=0;i<*pt;i++){(*pxm)->tickets == 0;printf("请输入第%d个选民的名字:\n",i+1);scanf("%s",(*pxm)->name);(*pxm)++;}*pxm = *pxm - *pt;
}void printXms(struct XuanMin *p,int len)
{int i;for(i=0;i<len;i++){printf("名字:%s,票数:%d\n",p->name,p->tickets);p++;}
}int doVot(struct XuanMin *p, int len)
{int i;int j;int feiPiao = 0;int mark;char tmpName[32];struct XuanMin *pbak = p;for(i=0;i<5;i++){mark = 0;printf("请输入你投给谁:\n");memset(tmpName,'\0',sizeof(tmpName)); //每次清空一下scanf("%s",tmpName);//输入选中的选民名字,像拆开一个选票,看到名字一样p = pbak;for(j = 0;j<len; j++){ //拿到名字,对应候选人票数加1if(strcmp(tmpName, p->name) == 0){p->tickets++;mark = 1; }p++;}if(mark == 0){printf("没有此候选人,放弃\n");feiPiao++;}}return feiPiao;
}struct XuanMin* getMax(struct XuanMin *p, int len)
{struct XuanMin* max;int i;max = p;for(i=0;i<len;i++){if(max->tickets < p->tickets){max = p;}p++;}return max;
}int main()
{struct XuanMin *xm = NULL;struct XuanMin *final;int total = 0;initXms(&xm,&total);printXms(xm,total);int feip = doVot(xm,total);printf("废票数是:%d\n",feip);printXms(xm,total);final = getMax(xm,total);printf("%s以%d票当选!!废票是%d\n",final->name,final->tickets,feip);return 0;
}
这篇关于C语言使用结构体模拟选举的小应用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!