马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
各位大神好,今天刚刚完成了小甲鱼的投票系统作业,试运行了一下,程序可以执行。但我想请教各位大神帮我看看代码有什么值得改进或者优化的地方。
我还有两个问题:1. 如果有任意两者的得票数一样,如何表示并列第一或者并列第二? 2. 怎么给得票数排序? 谢谢大家
代码如下
#include <stdio.h>
#include <string.h>
#define NUM 10
struct votes
{
char *name;
int suffrage;
};
void main()
{
struct votes poll[3]={{"kobe",0},{"james",0},{"wade",0}};
int i, most;
char player[10];
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[0].suffrage++;
}
else if(strcmp(player,"james")==0)
{
poll[1].suffrage++;
}
else if(strcmp(player,"wade")==0)
{
poll[2].suffrage++;
}
}
printf("the voting result:\n\n");
printf("kobe has %d suffrages\n", poll[0].suffrage);
printf("james has %d suffrages\n", poll[1].suffrage);
printf("wade has %d suffrages\n", poll[2].suffrage);
printf("\n");
most=cmp(poll[0].suffrage, poll[1].suffrage, poll[2].suffrage);
if(poll[0].suffrage==most)
{
printf("the winner is kobe!\n");
}
else if(poll[1].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;
}
排序的话,声明一个临时结构体变量,然后用冒泡排序比较票数进行结构体交换
|