zoukankan      html  css  js  c++  java
  • qsort实现结构体数组排序

    要注意强制转换

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct{
    	int num;
    	char name[20];
    	float chinese;
    	float math;
    	float english;
    }Stu,*pStu;
    #define N 3
    void arrPrint(pStu sArr)
    {
    	int i;
    	for(i=0;i<N;i++)
    	{
    		printf("%d %10s %5.2f %5.2f %5.2f
    ",sArr[i].num,sArr[i].name,sArr[i].chinese,sArr[i].math,sArr[i].english);
    	}
    }
    int compare(const void* pleft,const void* pright)
    {
    	pStu p1=(pStu)pleft;
    	pStu p2=(pStu)pright;
    	if(p1->chinese>p2->chinese)
    	{
    		return 1;
    	}else if(p1->chinese<p2->chinese)
    	{
    		return -1;
    	}else{
    		return 0;
    	}
    }
    int main()
    {
    	Stu sArr[N];
    	int i;
    	for(i=0;i<N;i++)
    	{
    		scanf("%d%s%f%f%f",&sArr[i].num,sArr[i].name,&sArr[i].chinese,&sArr[i].math,&sArr[i].english);
    	}
    	printf("----------------------------
    ");
    	arrPrint(sArr);
    	qsort(sArr,N,sizeof(Stu),compare);
    	printf("----------------------------
    ");
    	arrPrint(sArr);
    	system("pause");
    }
    
  • 相关阅读:
    Flask基础
    Scrapy框架(持久化,去重,深度控制,cookie)
    scrapy框架 简易整理
    BeautifulSoup 模块
    requests模块
    复习第三天
    在Django中使用原生Sql
    ajax跨域简单请求和复杂请求
    复习第二天
    IOS
  • 原文地址:https://www.cnblogs.com/Mered1th/p/10667895.html
Copyright © 2011-2022 走看看