鱼C论坛

 找回密码
 立即注册
查看: 913|回复: 2

[已解决]小白求助

[复制链接]
发表于 2020-12-5 19:28:29 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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;
            }
        }
    }
}
有大佬帮我看看这段代码哪里错了吗?
最佳答案
2020-12-6 21:01:46

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

  3. struct  candicate
  4. {
  5.     char name[20];
  6.     int count;
  7. };

  8. void function(struct candicate c[], int n);

  9. int main()
  10. {
  11.     struct  candicate  c[] = { {"chen",0},{"he",0},{"wang",0} };
  12.     int i, j, n;
  13.     char name[20];

  14.     printf("参与投票的人数:");
  15.     scanf("%d", &n);

  16.     for (i = 0; i < n; i++)
  17.     {
  18.         printf("第%d人投的是:", i + 1);
  19.         scanf("%s", name);
  20.         for (j = 0; j < 3; j++)
  21.         {
  22.             if (strcmp(c[j].name, name) == 0)
  23.             {
  24.                 c[j].count++;
  25.             }
  26.         }
  27.     }

  28.     for (i = 0; i < 3; i++)
  29.     {
  30.         function(&c[i], n);                                               // 加上了 &
  31.         printf("第%d位候选人"%s"的投票数为%d\n", i + 1, c[i].name, c[i].count);
  32.     }
  33.     return 0;
  34. }

  35. void function(struct candicate c[], int n)
  36. {
  37.     int a, b;
  38.     struct candicate temp;
  39.     for (a = 0; a < n - 1; a++)
  40.     {
  41.         for (b = 0; b < n - 1 - a; b++)
  42.         {
  43.             if ((c[b].count < c[b + 1].count) || (c[b].count = c[b + 1].count && strcmp(c[b].name, c[b + 1].name) > 0))
  44.             {
  45.                 temp = c[b];
  46.                 c[b] = c[b + 1];
  47.                 c[b + 1] = temp;
  48.             }
  49.         }
  50.     }
  51. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-12-5 22:15:50 | 显示全部楼层
第 37 行错误,function(c[], i);  // 出现不存在从 "candicate" 到 "candicate *" 的适当转换函数
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-6 21:01:46 | 显示全部楼层    本楼为最佳答案   

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

  3. struct  candicate
  4. {
  5.     char name[20];
  6.     int count;
  7. };

  8. void function(struct candicate c[], int n);

  9. int main()
  10. {
  11.     struct  candicate  c[] = { {"chen",0},{"he",0},{"wang",0} };
  12.     int i, j, n;
  13.     char name[20];

  14.     printf("参与投票的人数:");
  15.     scanf("%d", &n);

  16.     for (i = 0; i < n; i++)
  17.     {
  18.         printf("第%d人投的是:", i + 1);
  19.         scanf("%s", name);
  20.         for (j = 0; j < 3; j++)
  21.         {
  22.             if (strcmp(c[j].name, name) == 0)
  23.             {
  24.                 c[j].count++;
  25.             }
  26.         }
  27.     }

  28.     for (i = 0; i < 3; i++)
  29.     {
  30.         function(&c[i], n);                                               // 加上了 &
  31.         printf("第%d位候选人"%s"的投票数为%d\n", i + 1, c[i].name, c[i].count);
  32.     }
  33.     return 0;
  34. }

  35. void function(struct candicate c[], int n)
  36. {
  37.     int a, b;
  38.     struct candicate temp;
  39.     for (a = 0; a < n - 1; a++)
  40.     {
  41.         for (b = 0; b < n - 1 - a; b++)
  42.         {
  43.             if ((c[b].count < c[b + 1].count) || (c[b].count = c[b + 1].count && strcmp(c[b].name, c[b + 1].name) > 0))
  44.             {
  45.                 temp = c[b];
  46.                 c[b] = c[b + 1];
  47.                 c[b + 1] = temp;
  48.             }
  49.         }
  50.     }
  51. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-8 21:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表