免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: 技术小菜鸟
打印 上一主题 下一主题

[C] 可以在main函数内完成排序,但是当我想把排序单独用一个函数sort处理时,程序老是出 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2013-07-31 17:25 |只看该作者
回复 10# 技术小菜鸟

报什么错误?贴出来看看


   

论坛徽章:
0
12 [报告]
发表于 2013-07-31 17:56 |只看该作者
  1. #include<stdlib.h>
  2. #include<stdio.h>
  3. #include<unistd.h>

  4. struct student {
  5. int num;
  6. char name[10];
  7. int score;
  8. };

  9. void sort(struct student *p,int len);

  10. int main()
  11. {
  12. int i;
  13. int len;
  14. struct student *pstu;

  15. printf("please input the student number: ");
  16. scanf("%d",&len);
  17. pstu = (struct student*)malloc(len*sizeof(struct student));

  18. for(i=0;i<len;++i) {
  19.   printf("please input the %dth student'infomation\n",i);
  20.   scanf("%d %s %d",&(pstu[i].num),pstu[i].name,&(pstu[i].score));
  21.   printf("\n");
  22. }

  23. sort(pstu,len);

  24. for(i=0;i<len;i++) {
  25.   printf("%d %s %d\n",pstu[i].num,pstu[i].name,pstu[i].score);
  26. }

  27. }

  28. void sort(struct student *p,int len)
  29. {
  30. struct student t;
  31. int i,j;

  32. for(i=0;i<len-1;i++)
  33.   for(j=i+1;j<len;j++) {
  34.     if(p[i].score < p[j].score)
  35.     {
  36.       t = p[i];
  37.       p[i]=p[j];
  38.       p[j]=t;
  39.     }
  40.   }
  41. }
复制代码

论坛徽章:
0
13 [报告]
发表于 2013-07-31 17:59 |只看该作者
补充一下,我的代码是在linux系统上写的,可能头文件要换一下就行

论坛徽章:
0
14 [报告]
发表于 2013-07-31 21:21 |只看该作者
回复 13# seabiscuitxf
看了你的代码,我瞬间就明白了,我把sort函数写在了struct student 数据类型前面了,导致程序不知道什么是struct student,谢谢大神赐教!

   

论坛徽章:
2
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11
15 [报告]
发表于 2013-07-31 22:07 |只看该作者
  1. #include <stdio.h>
  2. #include <malloc.h>

  3. struct student
  4. {
  5.         int num;
  6.         char name[100];
  7.         char sex;
  8.         float score;
  9. };

  10. void sort(struct student *p, int l)
  11. {
  12.         struct student t;
  13.         int i, j;

  14.     for(i = 0; i <= l - 1; ++i) {
  15.                 for (j = 0; j <= l - 1 - i; ++j) {
  16.                         if (p[j].score < p[j + 1].score) {
  17.                                 t = p[j];
  18.                                 p[j] = p[j+1];
  19.                                 p[j+1] = t;
  20.                         }
  21.                 }
  22.         }
  23. }

  24. int main (void)
  25. {
  26.         int i;
  27.         int len;
  28.         struct student * pArr;

  29.         printf("请输入学生个数:");
  30.         scanf("%d", &len);
  31.         if (len > 0) {
  32.                 pArr = (struct student *)malloc(len * sizeof(struct student));
  33.                 if (pArr != NULL) {
  34.                         for (i = 0; i < len; ++i) {
  35.                                 printf("请输入第%d个学生信息:\n", i);
  36.                                 scanf("%d %s %c %f", &pArr[i].num, pArr[i].name, &pArr[i].sex, &pArr[i].score);
  37.                                 printf("\n");
  38.                         }
  39.                         sort(pArr, len);
  40.                         for (i = 0; i < len; ++i) {
  41.                                 printf("输出的是成绩第%d名学生信息:\n", i);
  42.                                 printf("%d %s %c %f", pArr[i].num, pArr[i].name, pArr[i].sex, pArr[i].score);
  43.                                 printf("\n");
  44.                         }
  45.                         free(pArr);
  46.                         return 0;
  47.                 }
  48.         }
  49.         return -1;
  50. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP