吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1799|回复: 6
收起左侧

[求助] 如何用C语言的结构体数组中的某一值排序?

[复制链接]
绅士夭儿 发表于 2019-12-22 15:04
#include <stdio.h>
#include <string.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) ;
}

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

我心她有丶 发表于 2019-12-22 16:08
[C] 纯文本查看 复制代码
#include <stdio.h>
#include <string.h>
#define   N   16
typedef  struct
{
    char  num[10];
    int   s;
} STREC;
void  fun(STREC  a[])
{
    int i, 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;
            }
        }
    }
}
int 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;
    printf("The data before sorted :\n");
    for (i = 0; i < N; i++)
    {
        if ((i) % 4 == 0)printf("\n");
        printf("%s  %4d  ", s[i].num, s[i].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[i].num, s[i].s);
    }
    return 0;
}

你应该说的是这样吧,我用的选择排序按照分数的升序排序了
QQ图片20191222160702.png

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
绅士夭儿 + 1 + 1 谢谢@Thanks!

查看全部评分

深夜雪 发表于 2019-12-22 16:37
冒泡排序即可
[C] 纯文本查看 复制代码
void  fun(STREC  a[])
{
	for (int i = 0; i < N - 1; i++)
	{
		for (int j = 0; j < N - 1 - i; j++)
		{
			if (a[j].s > a[j + 1].s)
			{
				STREC temp = { 0 };
				temp = a[j + 1];
				a[j + 1] = a[j];
				a[j] = temp;
			}
		}
	}
}
ethanchengyu 发表于 2019-12-22 17:29
Xer0 发表于 2019-12-23 08:45
只是普通的结构体数组 不是链表,就跟普通数组一样冒泡法排序就行了
注意一下数据拷贝问题就好
MaKa_Maka 发表于 2019-12-26 22:35
我心她有丶 发表于 2019-12-22 16:08
[mw_shl_code=c,true]#include
#include
#define   N   16

大哥帮我做一道题

如下



小明要制作班里的通讯录,要求参照手机通讯录。按A~Z排序(10分)
#include "stdio.h"
#include "string.h"
struct student{
        char name[20];
        char tel[12];
};
void sort(student ss[]);
void outPut(student ss[]);
main()
{

    student        contacts[10]={{"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");
}
我心她有丶 发表于 2019-12-27 12:11
MaKa_Maka 发表于 2019-12-26 22:35
大哥帮我做一道题

如下

[C] 纯文本查看 复制代码
#include "stdio.h"
#include "string.h"
struct student {
    char name[20];
    char tel[12];
};
void sort(student ss[]);
void outPut(student ss[]);
int main()
{
    student contacts[10] = { {"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[i].name[0] >= ss[j].name[0]) {
                temp = ss[i];
                ss[i] = ss[j];
                ss[j] = 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[i].name,ss[i].tel);
    printf("\n");
}
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-26 22:51

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表