|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<string.h>
struct candicate
{
char name[20];
int count;
};
void function(struct candicate c[],int n);
int main()
{
struct candicate c[] = {{"chen",0},{"he",0},{"wang",0}};
int i,j,n;
char name[20];
printf("参与投票的人数:");
scanf("%d",&n);
for(i = 0;i < n;i++)
{
printf("第%d人投的是:",i+1);
scanf("%s",name);
for(j = 0;j < 3;j++)
{
if(strcmp(c[j].name,name) == 0)
{
c[j].count++;
}
}
}
for(i = 0;i < 3;i++)
{
function(c[i],n);
printf("第%d位候选人\"%s\"的投票数为%d\n",i+1,c[i].name,c[i].count);
}
return 0;
}
void function(struct candicate c[],int n)
{
int a,b;
struct candicate temp;
for(a = 0;a < n - 1;a++)
{
for(b = 0;b < n - 1 - a;b++)
{
if((c[b].count < c[b + 1].count)|| (c[b].count = c[b + 1].count && strcmp(c[b].name,c[b + 1].name) > 0))
{
temp = c[b];
c[b] = c[b + 1];
c[b + 1] = temp;
}
}
}
}
有大佬帮我看看这段代码哪里错了吗?
#include<stdio.h>
#include<string.h>
struct candicate
{
char name[20];
int count;
};
void function(struct candicate c[], int n);
int main()
{
struct candicate c[] = { {"chen",0},{"he",0},{"wang",0} };
int i, j, n;
char name[20];
printf("参与投票的人数:");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("第%d人投的是:", i + 1);
scanf("%s", name);
for (j = 0; j < 3; j++)
{
if (strcmp(c[j].name, name) == 0)
{
c[j].count++;
}
}
}
for (i = 0; i < 3; i++)
{
function(&c[i], n); // 加上了 &
printf("第%d位候选人"%s"的投票数为%d\n", i + 1, c[i].name, c[i].count);
}
return 0;
}
void function(struct candicate c[], int n)
{
int a, b;
struct candicate temp;
for (a = 0; a < n - 1; a++)
{
for (b = 0; b < n - 1 - a; b++)
{
if ((c[b].count < c[b + 1].count) || (c[b].count = c[b + 1].count && strcmp(c[b].name, c[b + 1].name) > 0))
{
temp = c[b];
c[b] = c[b + 1];
c[b + 1] = temp;
}
}
}
}
|
|