c语言 12-4

1、

复制代码
#include <stdio.h>
#include <string.h>

#define NUMBER 5
#define NAME_LEN 64

typedef struct{
    char name[NAME_LEN];
    int height;
    float weight;
    long schols;
} Student;

void swap_str(Student *x, Student *y)
{
    Student tmp = *x;
    *x = *y;
    *y = tmp;
}

void sort_hei(Student a[], int n)
{
    int i, j;
    for(i = 0; i < n - 1; i++)
    {
        for(j = n - 1; j > i; j--)
        {
            if(a[j - 1].height > a[j].height)
            {
                swap_str(&a[j - 1], &a[j]);
            }
        }
    }
}

int main(void)
{
    int i;
    Student str[NUMBER];
    
    for(i = 0; i < NUMBER; i++)
    {
        printf("mem.%d.name = ", i + 1); scanf("%s", str[i].name);
        printf("mem.%d.height = ", i + 1); scanf("%d", &str[i].height);
        printf("mem.%d.weight = ", i + 1); scanf("%f", &str[i].weight);
        printf("mem.%d.schols = ", i + 1); scanf("%ld", &str[i].schols); 
    }
    
    /*Student str[] = {
    {"Sato", 178, 61.2, 80000},
    {"Sanaka", 175, 62.5, 73000},
    {"Takao", 173, 86.2, 0},
    {"Mike", 165, 72, 70000},
    {"Masaki", 179, 77.5, 70000},
    };*/
    
    puts("\n=============================");
    for(i = 0; i < NUMBER; i++)
        printf("%-8s %7d%7.2f%7ld\n", str[i].name, str[i].height, str[i].weight, str[i].schols);
        
    sort_hei(str, NUMBER);
    puts("\n==============================");
    for(i = 0; i < NUMBER; i++)
        printf("%-8s %7d%7.2f%7ld\n",str[i].name, str[i].height, str[i].weight, str[i].schols);
    return 0;
}
复制代码

 

 

2、

复制代码
#include <stdio.h>
#include <string.h>

#define NUMBER 5
#define NAME_LEN 64

typedef struct{
    char name[NAME_LEN];
    int height;
    float weight;
    long schols;
} Student;

void swap_str(Student *x, Student *y)
{
    Student tmp = *x;
    *x = *y;
    *y = tmp;
}

void sort_hei(Student a[], int n)
{
    int choose;
    printf("choose = 0, sort by height\nchoose = 1, sort by name.\nchoose = "); scanf("%d", &choose);
    
    int i, j;
    if(choose == 0)
    {
        for(i = 0; i < n - 1; i++)
        {
            for(j = n - 1; j > i; j--)
            {
                if(a[j - 1].height > a[j].height)
                {
                    swap_str(&a[j - 1], &a[j]);
                }
            }
        }
    }
    if(choose == 1)
    {
        for(i = 0; i < n - 1; i++)
        {
            for(j = n - 1; j > i; j--)
            {
                if(strcmp(a[j - 1].name,a[j].name) > 0)
                {
                    swap_str(&a[j - 1], &a[j]);
                }
            }
        }
    }
}

int main(void)
{
    int i;
    
    Student str[] = {
    {"Sato", 178, 61.2, 80000},
    {"Sanaka", 175, 62.5, 73000},
    {"Takao", 173, 86.2, 0},
    {"Mike", 165, 72, 70000},
    {"Masaki", 179, 77.5, 70000},
    };
    

    for(i = 0; i < NUMBER; i++)
        printf("%-8s %7d%7.2f%7ld\n", str[i].name, str[i].height, str[i].weight, str[i].schols);
    
    puts("\n=============================");
    sort_hei(str, NUMBER);
    puts("\n==============================");
    for(i = 0; i < NUMBER; i++)
        printf("%-8s %7d%7.2f%7ld\n",str[i].name, str[i].height, str[i].weight, str[i].schols);
    return 0;
}
复制代码

 

posted @   小鲨鱼2018  阅读(41)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示