数据结构 / day01 作业

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

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

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

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

cpp 复制代码
#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);

}
~                                                                                                                                                                                                                      
~                                                                                                                                                                                                                      
~                                                                                                                                                                                                                      
~  

输出结果:

cpp 复制代码
please input no.[1] student's info:
name:a
age:19
gender:f
please input no.[2] student's info:
name:b
age:29
gender:m
please input no.[3] student's info:
name:c
age:27
gender:f
please input no.[4] student's info:
name:d
age:18
gender:m
please input no.[5] student's info:
name:e
age:23
gender:f
before sort:
name	age	gender
a	19	f
b	29	m
c	27	f
d	18	m
e	23	f


after sort:
name	age	gender
d	18	m
a	19	f
e	23	f
c	27	f
b	29	m

2.定义车辆a和车辆b,实现交换

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

cpp 复制代码
#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);


}
~             

输出结果

cpp 复制代码
car a:
brand	price	color
Tesla	20	black

car b:
brand	price	color
byd	30	blue

car a:
brand	price	color
byd	30	blue

car b:
brand	price	color
Tesla	20	black

3.思维导图

相关推荐
apcipot_rain1 小时前
计科八股20260616(1)——堆存中位数、链表判环、黑白测试、敏捷开发与瀑布模型、配置管理、持续集成、池化
数据结构·算法·软件工程
三品吉他手会点灯6 小时前
C语言学习笔记 - 50.流程控制4 - 流程控制为什么非常非常重要
c语言·开发语言·笔记·学习
JAVA面经实录9178 小时前
Java 数据结构与算法 (终极完整学习文档)
java·数据结构·算法
影视飓风TIM10 小时前
数据结构 | 链表超全笔记(单链表+双链表+高频算法题)
数据结构·笔记·链表
十月的皮皮10 小时前
C语言学习笔记20260615-有序升序序列合并
c语言·笔记·学习
暮云星影10 小时前
全志linux开发屏幕适配(一)屏幕参数设置说明
linux·arm开发
牛油果子哥q11 小时前
STL set与map底层精讲,红黑树适配原理、有序去重特性、迭代器遍历、API实战与面试核心考点全解
开发语言·数据结构·c++·面试
swordbob12 小时前
NIO 的 Channel 里有多个 BIO 吗?
linux·网络·nio
Fcy64812 小时前
Linux下 信号的保存与捕捉
linux·中断·信号的捕捉·信号的保存
一切皆是因缘际会12 小时前
LLM轻量化联邦微调机理
数据结构·人工智能·数学建模·ai