广州网站(建设信科网络)可视化编辑建站平台
广州网站(建设信科网络),可视化编辑建站平台,平面设计初中毕业能学吗,为公司建立网站1.定义结构体数组存储5个学生的信息#xff1a;姓名#xff0c;年龄#xff0c;性别
定义函数实现输入#xff0c;要求形参使用结构体指针接收
函数实现5个学生年龄排序(注意对年龄排序时#xff0c;交换的是所有信息)
定义函数实现输出#xff0c;要求形参使用结构体…1.定义结构体数组存储5个学生的信息姓名年龄性别
定义函数实现输入要求形参使用结构体指针接收
函数实现5个学生年龄排序(注意对年龄排序时交换的是所有信息)
定义函数实现输出要求形参使用结构体指针接收
#include string.h
#include stdlib.h
#include stdio.hstruct 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 n5;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 i0; in ;i){printf(please input no.[%d] students info:\n, i1);printf(name:);scanf(%s, (pi)-name);printf(age:);scanf(%d, (pi)-age);printf(gender:);scanf( %c, (pi)-gender);}
}
void sort_by_age(int n, struct student *p )
{int descend0;for(int i0; in-1; i){int curi;for(int ji1; jn; j){if(descend1 (pcur)-age (pj)-age){curj;}else if(descend0 (pcur)-age (pj)-age){curj;}}if(cur!i){struct student t;strcpy(t.name, (pcur)-name);t.age (pcur)-age;t.gender (pcur)-gender;strcpy((pcur)-name, (pi)-name);(pcur)-age (pi)-age;(pcur)-gender (pi)-gender;strcpy((pi)-name, t.name);(pi)-age t.age;(pi)-gender t.gender;}}}
void ouput(int n, struct student *p)
{printf(name\tage\tgender\n);for(int i0; in; i){printf(%s\t%d\t%c\n, (pi)-name, (pi)-age, (pi)-gender);} putchar(10);}
~
~
~
~
输出结果:
please input no.[1] students info:
name:a
age:19
gender:f
please input no.[2] students info:
name:b
age:29
gender:m
please input no.[3] students info:
name:c
age:27
gender:f
please input no.[4] students info:
name:d
age:18
gender:m
please input no.[5] students 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 fafter sort:
name age gender
d 18 m
a 19 f
e 23 f
c 27 f
b 29 m2.定义车辆a和车辆b实现交换
车的信息品牌单价颜色
#include string.h
#include stdlib.h
#include stdio.hstruct 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);//exchangestruct 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);}
~
输出结果
car a:
brand price color
Tesla 20 blackcar b:
brand price color
byd 30 bluecar a:
brand price color
byd 30 bluecar b:
brand price color
Tesla 20 black3.思维导图
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/89716.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!