当前位置: 代码迷 >> C语言 >> 关于结构体排序问题
  详细解决方案

关于结构体排序问题

热度:291   发布时间:2005-09-15 20:46:00.0
关于结构体排序问题

补填fun函数使结构体中的数按从大到小排列?哪位大虾帮我啊 #include <stdio.h> #define N 16 typedef struct { char num[10]; int s; } STREC; void fun(STREC a[]) {

} main () { STREC s[N]= {{"GA005",85}, {"GA003",76}, {"GA002",69}, {"GA004",85}, {"GA001",91}, {"GA007",72}, {"GA008",64}, {"GA006", 87}, {"GA015",85}, {"GA013",91}, {"GA012",64}, {"GA014",91}, {"GA011",66}, {"GA017",64}, {"GA018",64}, {"GA016",72}}; int i; FILE *out; fun ( s ); printf ("The data after sorted :\n"); for (i=0; i<N; i++) { if ( (i)%4==0 ) printf ("\n"); printf ("%s %4d ", s[i].num, s[i].s); } printf ("\n"); out=fopen ("out.dat", "w"); for (i=0; i<N; i++) { if ( (i)%4==0 && i ) fprintf (out, "\n" ); fprintf (out, "%4d", s[i].s); } fprintf ( out, "\n" ); fclose (out ); getch(); }

http://bbs.bc-cn.net/bbs/dispbbs.asp?boardID=5&ID=28357&page=1 B . 标题不合要求。起一个耸人听闻的、不能反应贴子内容的标题。

[此贴子已经被knocker于2005-9-24 11:22:20编辑过]

搜索更多相关的解决方案: 结构体  

----------------解决方案--------------------------------------------------------

我已帮你完成了,看看是你需要的吗? #include <stdio.h> #define N 16 typedef struct { char num[10]; int s; } STREC; void fun(STREC a[]) { int i,t,j; for(j=0;j<N;j++) for(i=0;i<N;i++) if(a[i].s>a[i+1].s) {t=a[i].s; a[i].s=a[i+1].s; a[i+1].s=t;}

} main () { STREC s[N]= {{"GA005",85}, {"GA003",76}, {"GA002",69}, {"GA004",85}, {"GA001",91}, {"GA007",72}, {"GA008",64}, {"GA006", 87}, {"GA015",85}, {"GA013",91}, {"GA012",64}, {"GA014",91}, {"GA011",66}, {"GA017",64}, {"GA018",64}, {"GA016",72}}; int i; FILE *out; fun ( s ); printf ("The data after sorted :\n"); for (i=0; i<N; i++) { if ( (i)%4==0 ) printf ("\n"); printf ("%s %4d ", s[i].num, s[i].s); } printf ("\n"); out=fopen ("out.dat", "w"); for (i=0; i<N; i++) { if ( (i)%4==0 && i ) fprintf (out, "\n" ); fprintf (out, "%4d", s[i].s); } fprintf ( out, "\n" ); fclose (out ); /*getch();*/ }


----------------解决方案--------------------------------------------------------
这是什么啊

----------------解决方案--------------------------------------------------------

----------------解决方案--------------------------------------------------------

#include <stdio.h> #define N 16 typedef struct { char num[10]; int s; } STREC; void fun(STREC a[]) { int i,t,j; STREC temp; for(i=0;i<N-1;i++) for(j=i+1;j<N;j++) if(a[i].s<a[j].s) {temp=a[i]; a[i]=a[j]; a[j]=temp;}

} main () { STREC s[N]= {{"GA005",85}, {"GA003",76}, {"GA002",69}, {"GA004",85}, {"GA001",91}, {"GA007",72}, {"GA008",64}, {"GA006", 87}, {"GA015",85}, {"GA013",91}, {"GA012",64}, {"GA014",91}, {"GA011",66}, {"GA017",64}, {"GA018",64}, {"GA016",72}}; int i; FILE *out; fun ( s ); printf ("The data after sorted :\n"); for (i=0; i<N; i++) { if ( (i)%4==0 ) printf ("\n"); printf ("%s %4d ", s[i].num, s[i].s); } printf ("\n"); out=fopen ("out.dat", "w"); for (i=0; i<N; i++) { if ( (i)%4==0 && i ) fprintf (out, "\n" ); fprintf (out, "%4d", s[i].s); } fprintf ( out, "\n" ); fclose (out ); }


----------------解决方案--------------------------------------------------------
for (i=0; i<N; i++)
{
if ( (i)%4==0 && i )
fprintf (out, "\n" );
fprintf (out, "%4d", s[i].s);
}
fprintf ( out, "\n" );
fclose (out );
getch();
有什么用处啊,不写一样可以排出顺序啊
----------------解决方案--------------------------------------------------------
  相关解决方案