当前位置:网站首页>Small application of C language using structure to simulate election
Small application of C language using structure to simulate election
2022-07-26 17:04:00 【Xiaohao programming】
C Small application of simulated election of language structure
- Now there are three candidates , Five voters
- In the end, the higher one wins
Used in small applications C The structure in the language is the main writing body
Code up
#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];
// Initialize voter information
len = sizeof(xm)/sizeof(xm[0]);
for(i = 0; i<len; i++){
xm[i].tickets = 0;
printf(" Please enter the first %d The name of a voter :\n",i+1);
scanf("%s",xm[i].name);
}
// Ticket singing
for(i=0;i<5;i++){
mark = 0;
printf(" Please enter who you vote for :\n");
memset(tmpName,'\0',sizeof(tmpName)); // Empty it every time
scanf("%s",tmpName);// Enter the name of the selected voter , Like opening a ballot , See the same name
for(j = 0;j<len; j++){
// Get the name , The number of votes of the corresponding candidate plus 1
if(strcmp(tmpName, xm[j].name) == 0){
xm[j].tickets++;
mark = 1;
}
}
if(mark == 0){
printf(" There is no such candidate , give up \n");
feiPiao++;
}
}
// Publish the results
for(i=0;i<len;i++){
printf(" name :%s, Number of votes :%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))// There is a case that the situation of the same number of voters is announced separately
{
printf("%s and %s With %d Votes were elected at the same time !! The invalid ticket is %d\n",max.name,max1.name,max1.tickets,feiPiao);
}
else{
printf("%s With %d Elected by ballot !! The invalid ticket is %d\n",max.name,max.tickets,feiPiao);
}
return 0;
}
give the result as follows :
The highest number of votes 
The same number of votes 
It confirms the fact that the higher vote wins
There's another way to write it
#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(" Please enter how many people are running :\n");
scanf("%d",pn);
p = (struct XuanMin*)malloc(*pn*sizeof(struct XuanMin));
}
for(i=0;i<*pn;i++){
p->tickets = 0;
printf(" Please enter the first %d The name of a voter :\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(" name :%s, Number of votes :%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(" Please enter who you vote for :\n");
memset(tmpName,'\0',sizeof(tmpName)); // Empty it every time
scanf("%s",tmpName);// Enter the name of the selected voter , Like opening a ballot , See the same name
p = pbak;
for(j = 0;j<len; j++){
// Get the name , The number of votes of the corresponding candidate plus 1
if(strcmp(tmpName, p->name) == 0){
p->tickets++;
mark = 1;
}
p++;
}
if(mark == 0){
printf(" There is no such candidate , give up \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(" The number of invalid tickets is :%d\n",feip);
printXms(xm,total);
final = getMax(xm,total);
printf("%s With %d Elected by ballot !! The invalid ticket is %d\n",max->name,final->tickets,feip);
return 0;
}
More advanced writing
#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(" Please enter how many people are running :\n");
scanf("%d",pt);
printf(" altogether %d Individual candidacy \n",*pt);
*pxm = (struct XuanMin*)malloc(*pt * sizeof(struct XuanMin));
}
for(i=0;i<*pt;i++){
(*pxm)->tickets == 0;
printf(" Please enter the first %d The name of a voter :\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(" name :%s, Number of votes :%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(" Please enter who you vote for :\n");
memset(tmpName,'\0',sizeof(tmpName)); // Empty it every time
scanf("%s",tmpName);// Enter the name of the selected voter , Like opening a ballot , See the same name
p = pbak;
for(j = 0;j<len; j++){
// Get the name , The number of votes of the corresponding candidate plus 1
if(strcmp(tmpName, p->name) == 0){
p->tickets++;
mark = 1;
}
p++;
}
if(mark == 0){
printf(" There is no such candidate , give up \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(" The number of invalid tickets is :%d\n",feip);
printXms(xm,total);
final = getMax(xm,total);
printf("%s With %d Elected by ballot !! The invalid ticket is %d\n",final->name,final->tickets,feip);
return 0;
}
边栏推荐
- Anaconda download and Spyder error reporting solution
- Final consistency distributed transaction TCC
- Video media video
- Tensorflow Lite source code analysis
- 【开发教程7】疯壳·开源蓝牙心率防水运动手环-电容触摸
- After Oracle creates a table partition, the partition is not given during the query, but the value specified for the partition field will be automatically queried according to the partition?
- [untitled]
- 【无标题】
- Definition and relationship of derivative, differential, partial derivative, total derivative, directional derivative and gradient
- Is it safe for Guosen Securities to open an account? How can I find the account manager
猜你喜欢
![[basic course of flight control development 1] crazy shell · open source formation UAV GPIO (LED flight information light and signal light control)](/img/48/6dcaf4c9695d90e62036396cd81366.png)
[basic course of flight control development 1] crazy shell · open source formation UAV GPIO (LED flight information light and signal light control)

37.【重载运算符的类别】

PXE efficient batch network installation

2022-2023 topic recommendation of information management graduation project
![[development tutorial 7] crazy shell · open source Bluetooth heart rate waterproof sports Bracelet - capacitive touch](/img/b8/cf563fa54f8a8a2e051bbf585a0a68.png)
[development tutorial 7] crazy shell · open source Bluetooth heart rate waterproof sports Bracelet - capacitive touch

Alibaba Cloud Toolkit —— 项目一键部署工具

Idea Alibaba cloud multi module deployment

OA项目之我的会议(会议排座&送审)
![[fluent -- advanced] packaging](/img/aa/bd6ecad52cbe4a34db75f067aa4dfe.png)
[fluent -- advanced] packaging

Marxan model, reserve optimization and protection vacancy selection technology, application in invest ecosystem
随机推荐
Digital intelligence transformation, management first | jnpf strives to build a "full life cycle management" platform
Idea Alibaba cloud multi module deployment
【开发教程9】疯壳·ARM功能手机-I2C教程
Detailed explanation of tcpdump command
Tcpdump命令详解
My SQL is OK. Why is it still so slow? MySQL locking rules
PXE高效批量网络装机
MySQL lock mechanism (example)
Difference between C event and delegation
My meeting of OA project (meeting seating & submission for approval)
movable-view 组件(可上下左右拖动 )
C#转整型的三种方式的区别以及效率对比
【飞控开发基础教程3】疯壳·开源编队无人机-串口(基础收发)
It turns out that cappuccino information security association does this. Let's have a look.
The difference and efficiency comparison of three methods of C # conversion integer
How to implement Devops with automation tools | including low code and Devops application practice
Understanding JS foundation and browser engine
快速学会配置yum的本地源和网络源,并学会yum的使用
带你一分钟了解对称加密和非对称加密
[daily3] vgg16 learning