如何用C语言的结构体数组中的某一值排序?
#include <stdio.h>#include <string.h>
#define N 16
typedefstruct
{charnum;
int s;
} STREC;
voidfun( STRECa[] )
{
}
main()
{STRECs={{"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}};
inti;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.num,s.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.s);
}
fprintf(out,"\n");
fclose(out) ;
} #include <stdio.h>
#include <string.h>
#define N 16
typedefstruct
{
charnum;
int s;
} STREC;
voidfun(STRECa[])
{
int i, j;
STREC temp;
for ( i = 0; i < N - 1; ++i)
{
for ( j = i + 1 ; j < N; ++j)
{
if (a.s >= a.s) {
temp = a;
a = a;
a = temp;
}
}
}
}
int main()
{
STRECs = { {"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} };
inti; FILE* out;
printf("The data before sorted :\n");
for (i = 0; i < N; i++)
{
if ((i) % 4 == 0)printf("\n");
printf("%s%4d", s.num, s.s);
}
puts("");
fun(s);
printf("The data after sorted :\n");
for (i = 0; i < N; i++)
{
if ((i) % 4 == 0)printf("\n");
printf("%s%4d", s.num, s.s);
}
return 0;
}
你应该说的是这样吧,我用的选择排序按照分数的升序排序了 冒泡排序即可
voidfun(STRECa[])
{
for (int i = 0; i < N - 1; i++)
{
for (int j = 0; j < N - 1 - i; j++)
{
if (a.s > a.s)
{
STREC temp = { 0 };
temp = a;
a = a;
a = temp;
}
}
}
} 大神的代码来一波 只是普通的结构体数组 不是链表,就跟普通数组一样冒泡法排序就行了
注意一下数据拷贝问题就好 我心她有丶 发表于 2019-12-22 16:08
#include
#include
#define N 16
大哥帮我做一道题
如下
小明要制作班里的通讯录,要求参照手机通讯录。按A~Z排序(10分)
#include "stdio.h"
#include "string.h"
struct student{
char name;
char tel;
};
void sort(student ss[]);
void outPut(student ss[]);
main()
{
student contacts={{"LiLei","13678941562"},
{"WangHao","15046523182"},
{"WangDi","13598741863"},
{"OuyangDan","13875469871"},
{"HanLi","13647152364"},
{"Zhengchao","15098563214"},
{"Futong","13213254698"},
{"FangYang","15978632132"},
{"BaiHao","15432145698"},
{"Cenyu","15896566532"}};
sort(contacts);
outPut(contacts);
}
void sort(struct student ss[])
{
}
void outPut(struct student ss[])
{
int i;
printf("------通讯录-------\n");
printf("姓名\t\t电话\t\n");
//补充代码
printf("\n");
} MaKa_Maka 发表于 2019-12-26 22:35
大哥帮我做一道题
如下
#include "stdio.h"
#include "string.h"
struct student {
char name;
char tel;
};
void sort(student ss[]);
void outPut(student ss[]);
int main()
{
student contacts = { {"LiLei","13678941562"},
{"WangHao","15046523182"},
{"WangDi","13598741863"},
{"OuyangDan","13875469871"},
{"HanLi","13647152364"},
{"Zhengchao","15098563214"},
{"Futong","13213254698"},
{"FangYang","15978632132"},
{"BaiHao","15432145698"},
{"Cenyu","15896566532"} };
sort(contacts);
outPut(contacts);
return 0;
}
void sort(struct student ss[])
{
int i,j;
student temp;
for (i = 0; i < 10 - 1; ++i)
for (j = i + 1; j < 10; ++j)
{
if (ss.name >= ss.name) {
temp = ss;
ss = ss;
ss = temp;
}
}
}
void outPut(struct student ss[])
{
int i;
printf("------通讯录-------\n");
printf("姓名\t\t电话\t\n");
for (i = 0; i < 10; i++)
printf("%s\t\t%s\t\n",ss.name,ss.tel);
printf("\n");
}
页:
[1]