icecocotea 发表于 2020-6-29 17:24:53

关于小甲鱼老师的通票系统作业

各位大神好,今天刚刚完成了小甲鱼的投票系统作业,试运行了一下,程序可以执行。但我想请教各位大神帮我看看代码有什么值得改进或者优化的地方。
我还有两个问题:1. 如果有任意两者的得票数一样,如何表示并列第一或者并列第二? 2. 怎么给得票数排序? 谢谢大家

代码如下

#include <stdio.h>
#include <string.h>

#define NUM 10

struct votes
{
        char *name;
        int suffrage;
};

void main()
{

        struct votes poll={{"kobe",0},{"james",0},{"wade",0}};
        int i, most;
        char player;
        int cmp(int a, int b, int c);
       
        printf("welcome to the voting system!\nplease vote for 3 candidates: kobe, james and wade\n");
        for(i=0;i<NUM;i++)
        {
                printf("vote %d:", i+1);
                gets(player);
               
                if(strcmp(player,"kobe")==0)
                {
                        poll.suffrage++;
                }
                else if(strcmp(player,"james")==0)
                {
                        poll.suffrage++;
                }
                else if(strcmp(player,"wade")==0)
                {
                        poll.suffrage++;
                }
        }
       
        printf("the voting result:\n\n");
        printf("kobe has %d suffrages\n", poll.suffrage);
        printf("james has %d suffrages\n", poll.suffrage);
        printf("wade has %d suffrages\n", poll.suffrage);
        printf("\n");

        most=cmp(poll.suffrage, poll.suffrage, poll.suffrage);

        if(poll.suffrage==most)
        {
                printf("the winner is kobe!\n");
        }
        else if(poll.suffrage==most)
        {
                printf("the winner is james!\n");
        }
        else
        {
                printf("the winner is wade!\n");
        }
       
}

int cmp(int a, int b, int c)
{
        int w;
        w=a>((b>c?b:c))?a:((b>c)?b:c);
       
        return w;
}

chxchxkkk 发表于 2020-6-30 11:13:58

排序的话,声明一个临时结构体变量,然后用冒泡排序比较票数进行结构体交换
页: [1]
查看完整版本: 关于小甲鱼老师的通票系统作业