投票选班长

这是我的第一个博客,就用一个期末项目来开启我的博客之旅吧!

这个项目的主要功能是输入票数,通过比较票数来选出班长。

代码如下:

#include<stdio.h>
#include<stdlib.h>
#include <malloc.h>
#include <algorithm>
#define MAX 50
using namespace std;


//定义结构体,用来保存班长的序号和票数
struct node
{
    int num;
    int count;
}boy[100],t;


bool cmp(node a,node b)
{
    return a.count>b.count;
}


/*
**功能:主界面菜单显示
**入口参数:无
**出口参数:(n)
*/
int showMain()
{
    system("color A");
    printf("\t\t************************************\n");
    printf(" \t\t*\t 1、输入选票\t\t   *\n");
    printf(" \t\t*\t 2、输出票数\t\t   *\n");
    printf(" \t\t*\t 3、输出结果\t\t   *\n");
    printf(" \t\t*\t 0、退出系统\t\t   *\n");
    printf("\t\t************************************\n");
    printf("请输入您的选择:\n");
    int choose;
    scanf("%d",&choose);
    return (choose);
}


/*
**功能:选票输入
**入口参数:无
**出口参数:(struct node boy[0])
*/
struct node input()
{
    printf("开始选举。请注意:有效候选人代号为1,2,3,4。\n");
    boy[1].count=boy[2].count=boy[3].count=boy[4].count=0;
    for(int i=1; ;i++)
    {
        printf("请输入班长候选人代号(数字0结束):");
        scanf("%d",&boy[i].num);
        if(boy[i].num==1)
        {
            boy[1].count++;
        }
        else if(boy[i].num==2)
        {
            boy[2].count++;
        }
        else if(boy[i].num==3)
        {
            boy[3].count++;
        }
        else if(boy[i].num==4)
        {
            boy[4].count++;
        }
        else if(boy[i].num==0)
        {
            printf("选举结束。\n");
             break;
        }
        else
        {
            printf("此选票无效\n");
        }
    }
    return (boy[1]);
}


/*
**功能:选票输出
**入口参数:(struct node boy[])
**出口参数:(struct node boy[1])
*/
struct node output(struct node boy[])
{
    for(int i=1;i<=4;i++)
    {
        printf("第%d位候选人的票数是:%d\n",i,boy[i].count);
    }
    return (boy[1]);
};


/*
**功能:选票比较
**入口参数:(struct node boy[])
**出口参数:(int winner)
*/
int process(struct node boy[])
{
    sort(boy+1,boy+5,cmp);
    /*for(int i=1;i<=4;i++)
    {
        printf("%d ",boy[i].count);
    }*/
    printf("选举最终结果是:候选人代号是-->%d<--的同学当选班长\n",boy[1].num);
    return 0;
}






/*
**功能:主函数
**入口参数:(无)
**出口参数:(无)
*/
int main()
{
    system("color C");
    system("pause");
    printf("\t\t\t欢迎使用班长选举系统\t\n\n");
    char *filePath = (char *)malloc(sizeof(char)*MAX);


    while(1)
    {
        int n;
        n=showMain();//菜单显示
        switch(n)
       {
           case 1:
            {
                input();//输入票数函数
                break;
            }
            case 2:
            {
               output(boy);//输出票数函数
                break;
            }
            case 3:
            {
                process(boy);//比较函数
                break;
            }
            case 0://退出系统
            {
                printf("谢谢您的使用!\n");
                return 0;
            }
       }
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值