数据结构 21

1、思维导图

2、定义结构体数组存储5个学生的信息:姓名,年龄,性别

定义函数实现输入,要求形参使用结构体指针接收

函数实现5个学生年龄排序(注意对年龄排序时,交换的是所有信息)

定义函数实现输出,要求形参使用结构体指针接收

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
 
struct student
{
    char name[20];
    int age;
    char gender; //f,m
 
};
 
void input(int n, struct student *p);
void sort_by_age(int n, struct student *p );
void ouput(int n, struct student *p);
 
int main(int argc, const char *argv[])
{
    int n=5;
 
    struct student arr_stu[n];
    struct student *p = arr_stu;
 
    input(n, arr_stu);
    printf("before sort:\n");
    ouput(n, arr_stu);
    putchar(10);
    sort_by_age(n, arr_stu);
    printf("after sort:\n");
    ouput(n, arr_stu);
 
    
    return 0;
}
 
 
void input(int n, struct student *p){
    for(int i=0; i<n ;i++)
    {
        printf("please input no.[%d] student's info:\n", i+1);
 
        printf("name:");
        scanf("%s", (p+i)->name);
 
        printf("age:");
        scanf("%d", &(p+i)->age);
 
        printf("gender:");
        scanf(" %c", &(p+i)->gender);
    }
}
void sort_by_age(int n, struct student *p )
{
    int descend=0;
    for(int i=0; i<n-1; i++)
    {
        int cur=i;
 
        for(int j=i+1; j<n; j++)
        {
            if(descend==1 && (p+cur)->age < (p+j)->age)
            {
                cur=j;
            }
            else if(descend==0 && (p+cur)->age > (p+j)->age)
            {
                cur=j;
            }
    
        }
        if(cur!=i)
        {
        
        struct student t;
        strcpy(t.name, (p+cur)->name);
        t.age = (p+cur)->age;
        t.gender = (p+cur)->gender;
 
 
        strcpy((p+cur)->name, (p+i)->name);
        (p+cur)->age = (p+i)->age;
        (p+cur)->gender = (p+i)->gender;
 
        strcpy((p+i)->name, t.name);
        (p+i)->age = t.age;
        (p+i)->gender = t.gender;
        
        }
    
    }
 
 
}
void ouput(int n, struct student *p)
{
    printf("name\tage\tgender\n");
    for(int i=0; i<n; i++)
    {
        printf("%s\t%d\t%c\n", (p+i)->name, (p+i)->age, (p+i)->gender);
    }                                                                                                                                                                                                                  
    putchar(10);
 
}
                                                                                                                                                                                                                                                                                                                                                                               

3、定义车辆a和车辆b,实现交换

车的信息:品牌,单价,颜色

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
 
struct car
{
    char brand[20];
    int price;
    char color[10];
};
 
void output(struct car ca);
 
int main(int argc, const char *argv[])
{
 
    struct car a;
    struct car b;
 
    strcpy(a.brand, "Tesla");
    a.price = 20;
    strcpy(a.color, "black");
    printf("car a:\n");
    output(a);
 
    strcpy(b.brand, "byd");
    b.price =30;
    strcpy(b.color, "blue");
    printf("car b:\n");
    output(b);
 
    //exchange
    
    struct car t;
    strcpy(t.brand, a.brand);
    strcpy(a.brand,  b.brand);
    strcpy(b.brand, t.brand);
 
    t.price = a.price;
    a.price = b.price;
    b.price = t.price;
 
    strcpy(t.color, a.color);                                                                                                                    
    strcpy(a.color, b.color);
    strcpy(b.color, t.color);
 
    printf("car a:\n");
    output(a);
 
    printf("car b:\n");
    output(b);
    
    return 0;
}
 
void output(struct car ca)
{
    printf("brand\tprice\tcolor\n");
    printf("%s\t%d\t%s\n\n", ca.brand, ca.price, ca.color);
 
 
}
~             

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值