|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 LukaDoncicY77 于 2021-12-15 18:42 编辑
#include <stdio.h>
#include <string.h>
struct men
{
int id;
char name[20];
char tel[12];
int mpoints;
};
int main()
{
int i,j;
struct men t;
struct men mem[5]={
{1,"Lihong","123",789},
{4,"ZhaoHan","123",789},
{2,"QianQi","123",789},
{5,"ZhouHe","123",789},
{3,"SunHai","123",789}
};
for(i=0;i<4;i++)
{
for(j=0;j<4-i;j++)
if(strcmp(mem[i].name,mem[j+1].name)>0)
{t=mem[i];mem[i]=mem[j+1];mem[j+1]=t;}
}
printf("\n");
for(i=0;i<5;i++)
printf("%6d%20s%15s%6d\n",mem[i].id,mem[i].name,mem[i].tel,mem[i].mpoints);
return 0;
}
我的排序法 应该如何修改?
for(i=0;i<4;i++)
{
for(j=i+1;j<5;j++)
if(strcmp(mem[i].name,mem[j].name)>0)
{t=mem[i];mem[i]=mem[j];mem[j]=t;}
}
printf("\n");
已解决。
大家能否帮忙看一下 下面这个 选择排序法 我不太理解
for(i=1;i<5;i++)
{
int k=5-i
for(j=0;j<5-i;j++)
if(strcmp(mem[j].name,mem[k].name)>0)
k=j;
if(k!=5-i)
{
t=mem[l];mem[k]=mem[5-i];mem[5-i]=t;
}
}
#include <stdio.h>
#include <string.h>
struct men
{
int id;
char name[20];
char tel[12];
int mpoints;
};
int main(void)
{
int i,j;
struct men t;
struct men mem[5]={
{1,"Lihong","123",789},
{4,"ZhaoHan","123",789},
{2,"QianQi","123",789},
{5,"ZhouHe","123",789},
{3,"SunHai","123",789}
};
for(i = 0 ; i < 4 ; i ++)
{
for(j = i + 1 ; j && strcmp(mem[j] . name , mem[j - 1] . name) < 0 ; j --) {
t = mem[j] ;
mem[j] = mem[j - 1] ;
mem[j - 1] = t ;
}
}
printf("\n");
for(i=0;i<5;i++)
printf("%6d%20s%15s%6d\n",mem[i].id,mem[i].name,mem[i].tel,mem[i].mpoints);
}
|
|