Chinaunix

标题: C排序问题,郁闷中,求解 [打印本页]

作者: 寻找高手    时间: 2008-12-08 17:58
标题: C排序问题,郁闷中,求解
#include  "stdio.h"
#include  "conio.h"
int i,j;
struct student
{
int noname[6];
char name[8];
int score[3];
float ave;
}stu[2],change;

void two()
{
int sum=0;
for(j=0;j<3;j++)
    {
     //sum=0;
     printf("score %d:",j+1);
     scanf("%d",j);
     sum=sum+j;
    }
stu.ave=sum/3.0;
}

void one()
{
int sum;
for(i=0;i<2;i++)
  {
//  two();
   printf("\nInput score of student %d:\n",i+1);
   printf("NO.    :");
   scanf("%s",stu);
   printf("name   :");
   scanf("%s",stu);
   two();
  }
}

void therr()
{
int n;
n=i;
for(i=0;i<n;j++)
   for(j=i+1;j<n;j++)
     if(stu.ave<stu[j].ave)
       {
        change=stu;
        stu=stu[j];
        stu[j]=change;
       }
}

main()
{
// void one();
clrscr();
//int i;
FILE *fp;
one();
therr();
fp=fopen("d:\\mcutest1\\tc\\stud","w");
for(i=0;i<2;i++)
   if(fwrite(&stu,sizeof(struct student),1,fp)!=1)
     printf("File write error\n");
for(i=0;i<2;i++)
    {
     fread(&stu,sizeof(struct student),1,fp);
     printf("%s,%s,%d,%d,%d,%6.2f\n",stu.noname,stu.name,stu.score[0],stu.score[1],stu.score[2],stu.ave);
    }
fclose(fp);
return 0;
}
作者: MMMIX    时间: 2008-12-08 18:05
说过多少次了,贴代码用 [code][/code] 括起来,咋就是不听呢?
作者: 寻找高手    时间: 2008-12-09 10:36

  1. #include  "stdio.h"
  2. #include  "conio.h"
  3. int i,j;
  4. struct student
  5. {
  6. int noname[6];
  7. char name[8];
  8. int score[3];
  9. float ave;
  10. }stu[2],change;

  11. void two()
  12. {
  13. int sum=0;
  14. for(j=0;j<3;j++)
  15.     {
  16.      //sum=0;
  17.      printf("score %d:",j+1);
  18.      scanf("%d",j);
  19.      sum=sum+j;
  20.     }
  21. stu.ave=sum/3.0;
  22. }

  23. void one()
  24. {
  25. int sum;
  26. for(i=0;i<2;i++)
  27.   {
  28. //  two();
  29.    printf("\nInput score of student %d:\n",i+1);
  30.    printf("NO.    :");
  31.    scanf("%s",stu);
  32.    printf("name   :");
  33.    scanf("%s",stu);
  34.    two();
  35.   }
  36. }

  37. void therr()
  38. {
  39. int n;
  40. n=i;
  41. for(i=0;i<n;j++)
  42.    for(j=i+1;j<n;j++)
  43.      if(stu.ave<stu[j].ave)
  44.        {
  45.         change=stu;
  46.         stu=stu[j];
  47.         stu[j]=change;
  48.        }
  49. }

  50. main()
  51. {
  52. // void one();
  53. clrscr();
  54. //int i;
  55. FILE *fp;
  56. one();
  57. therr();
  58. fp=fopen("d:\\mcutest1\\tc\\stud","w");
  59. for(i=0;i<2;i++)
  60.    if(fwrite(&stu,sizeof(struct student),1,fp)!=1)
  61.      printf("File write error\n");
  62. for(i=0;i<2;i++)
  63.     {
  64.      fread(&stu,sizeof(struct student),1,fp);
  65.      printf("%s,%s,%d,%d,%d,%6.2f\n",stu.noname,stu.name,stu.score[0],stu.score[1],stu.score[2],stu.ave);
  66.     }
  67. fclose(fp);
  68. return 0;
  69. }
复制代码
  这样吗?下次记着了
作者: MMMIX    时间: 2008-12-09 12:02
标题: 回复 #3 寻找高手 的帖子
直接编辑你顶楼的帖子吧
作者: 寻找高手    时间: 2008-12-09 14:01
标题: 我整的,但平均数AVE不正确

  1. #include  "stdio.h"
  2. #include  "conio.h"
  3. int i,j;
  4. struct student
  5. {
  6. int noname[6];
  7. char name[8];
  8. int score[3];
  9. float ave;

  10. }stu[2],change;

  11. void one()
  12. {
  13. int sum;
  14. for(i=0;i<2;i++)
  15.   {
  16.    printf("\nInput score of student %d:\n",i+1);
  17.    printf("NO.    :");
  18.    scanf("%s",&stu[i].noname);
  19.    printf("name   :");
  20.    scanf("%s",&stu[i].name);
  21.    for(j=0;j<3;j++)
  22.      {
  23.       printf("score %d:",j+1);
  24.       scanf("%d",&stu[i].score[j]);
  25.       sum=sum+stu[i].score[j];
  26.      }
  27.     stu[i].ave=sum/3.0;
  28.   }
  29. }

  30. void therr()
  31. {
  32. int n;
  33. n=i;
  34. for(i=0;i<n;i++)
  35.    for(j=i+1;j<n;j++)
  36.      if(stu[i].ave<stu[j].ave)
  37.        {
  38.         change=stu[i];
  39.         stu[i]=stu[j];
  40.         stu[j]=change;
  41.        }
  42. }

  43. main()
  44. {int n;
  45. clrscr();
  46. FILE *fp;
  47. one();
  48. therr();
  49. fp=fopen("d:\\mcutest1\\tc\\stud","w");
  50. for(i=0;i<2;i++)
  51.    if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1)
  52.      printf("File write error\n");
  53. for(i=0;i<2;i++)
  54.     {
  55.      fread(&stu[i],sizeof(struct student),1,fp);
  56.      printf("%s,%s,%d,%d,%d,%6.2f\n",stu[i].noname,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].ave);
  57.     }
  58. fclose(fp);
  59. return 0;
  60. }
复制代码

作者: duanjigang    时间: 2008-12-09 20:15
找本数据结构的书或者算法基础的书籍看看吧。。。不要一上来就编码
理解算法在解决问题过程中比编码重要
作者: 寻找高手    时间: 2008-12-10 12:04
标题: 回复 #6 duanjigang 的帖子
谢谢
作者: ujiangzq    时间: 2008-12-10 18:01
你的代码里问题很多,找本c语言基础书籍看看。
作者: exir    时间: 2008-12-10 22:38
请问版主有的代码还带语法高亮彩色显示使用什么tag括起来的啊?
作者: MMMIX    时间: 2008-12-11 09:33
原帖由 exir 于 2008-12-10 22:38 发表
请问版主有的代码还带语法高亮彩色显示使用什么tag括起来的啊?

那个不是一个 tag 可以做到的。在编辑框的上方选择 "C++" 那个按钮,然后选择语言类型并输入代码即可。

[ 本帖最后由 MMMIX 于 2008-12-11 09:36 编辑 ]
作者: xiangyu1986    时间: 2008-12-12 10:01
标题: 回复 #5 寻找高手 的帖子
这个程序开始冒看还真不晓得有这么多问题
类型没有匹配,读文件时指针也没有回到头
真不知道自己是不是也写成这个样
作者: liu2g    时间: 2008-12-13 22:37

  1. #include  "stdio.h"
  2. #include  "conio.h"
复制代码

作者: 寻找高手    时间: 2009-01-31 03:42





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2