您的位置:首页 > 其它

投票选班长

2016-06-30 21:46 190 查看
这是我的第一个博客,就用一个期末项目来开启我的博客之旅吧!

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

代码如下:

#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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: