• 按学生成绩排序


    已知学生结构体如下:
    struct student 
    {
     int num;
     char name[8];
     char sex;
     int age;
     float grade;
    };
    要求按照学生成绩进行排序,输出排序后的结果。

    #include<stdio.h>
    #include
    <conio.h>
    #include
    <string.h>
    #define N 10
    extern unsigned _floatconvert;    /*防止floating point formats not linked 错误发生*/
    #pragma extref _floatconvert

    typedef 
    struct student    
    {
        
    int num;
        
    char name[8];
        
    char sex;
        
    int age;
        
    float grade;
    }STU;

    STU stu[N]
    ={{101,"Zhang",'M',19,95.6},
                {
    102,"Wang" ,'F',18,92.4},
                {
    103,"Zhao" ,'M',19,85.7},
                {
    104,"Li"   ,'M',20,96.3},
                {
    105,"Gao"  ,'M',19,96.4},
                {
    106,"Lin"  ,'M',18,91.5},
                {
    107,"Ma"   ,'F',17,98.7},
                {
    108,"Zhen" ,'M',21,90.1},
                {
    109,"Xu"   ,'M',19,89.8},
                {
    110,"Mao"  ,'F',18,94.9}};

    void print(STU *p[])
    {
        
    int i;
        printf(
    "num\tname\tsex\tage\tgrade\n");
        
    for(i=0;i<N;i++)
        {
            printf(
    "%d\t%s\t%c\t%d\t%5.1f\n",p[i]->num,p[i]->name,p[i]->sex,p[i]->age,p[i]->grade);
        }
    }

    void gradebub(STU *p[])
    {
        STU 
    *temp;
        
    int i,j,flag;
        
    for(i=0;i<N-1;i++)
        {
            flag
    =0;
            
    for(j=0;j<N-i-1;j++)
                
    if(p[j]->grade>p[j+1]->grade)
                {
                    temp
    =p[j];p[j]=p[j+1];p[j+1]=temp;
                    flag
    =1;
                }
            
    if(flag==0)    break;
        }
    }

    void main()
    {
        
    int i;
        STU 
    *p[N];
        
    for(i=0;i<N;i++)
            p[i]
    =&stu[i];
        print(p);
        printf(
    "\n");
        gradebub(p);
        print(p);
        getch();
    }
  • 相关阅读:
    写在vue总结之前(一)
    前端应该掌握的web基础和网络知识
    sass之为什么要使用预处理器
    ThinkPHP简单的验证码实现
    ajax接收php返回得到一堆html代码
    Bootstrap 4,“未捕获错误:Bootstrap工具提示需要Tether(http://github.hubspot.com/tether/)”
    百度AI开放平台- API实战调用
    最短路径算法—Dijkstra(迪杰斯特拉)算法分析与实现(C/C++)
    C#避免踩坑之如何添加paint事件
    php插入mysql中文数据出现乱码
  • 原文地址:https://www.cnblogs.com/qixin622/p/734429.html
Copyright © 2020-2023  润新知