当前位置:网站首页>结构体函数练习
结构体函数练习
2022-08-04 04:11:00 【BSP初级小学僧】
1、编写一个程序,完成根据学员姓名查询成绩的功能(要求查询通过函数实现),
定义一个学员结构体,包含姓名,成绩,定义一个结构体数组,保存所有学员的
信息(假定有3个学生),首先录入学员信息,然后调用查询函数获得要查询学员成绩
/*
3.编写一个程序,完成根据学员姓名查询成绩的功能(要求查询通过函数实现),
定义一个学员结构体,包含姓名,成绩,定义一个结构体数组,保存所有学员的
信息(假定有3个学生),首先录入学员信息,然后调用查询函数获得要查询学员成绩
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct
{
char name[10];
float score;
}STU;
void struct_input(STU arr[],int n)
{
for(int i=0;i<n;i++)
{
printf("请输入第%d位学员的信息:\n",i+1);
scanf("%s",&arr[i].name);
scanf("%f",&arr[i].score);
}
}
void struct_seek(STU arr[],int n)
{
char name_seek[10];
printf("请输入要查询的学员姓名:\n");
scanf("%s",&name_seek);
for(int i=0;i<n;i++)
{
int ret=-1;
ret=strcmp(name_seek,arr[i].name);
if(ret==0)
{
printf("log:%d\n",i);
printf("您要查询的%s的成绩为%.1f",arr[i].name,arr[i].score);
}
}
}
void struct_print(STU arr[],int n)
{
printf("姓名\t分数\n");
for(int i=0;i<n;i++)
printf("%s\t%.1f\n",arr[i].name,arr[i].score);
}
int main()
{
STU arr[3];
struct_input(arr,3);
struct_print(arr,3);
struct_seek(arr,3);
return 0;
}
运行结果:

2、对一个结构体数组实现增删改查操作
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct
{
int num;
char name[20];
char sex;
float score;
}boy;
boy boy_test[6]={
{101,"li ping",'m',45},
{102,"zhang ping",'m',62.5},
{103,"he feng",'m',92.5},
{104,"cheng li",'f',87},
{106,"wang ming",'m',58},};
void struct_print(boy *p01,int n)
{
printf("\n");
printf("学号\t姓名\t 性别\t分数\n");
for(int i=0;i<n;i++)
printf("%d\t%s\t%c\t%.1f\t\n",(*(p01+i)).num,(*(p01+i)).name,(*(p01+i)).sex,(*(p01+i)).score);
}
void struct_count(boy boy_test01[],int n)
{
int count=0;
for(int i=0;i<n;i++)
{
if ((*(boy_test01+i)).score<60 && (*(boy_test01+i)).num != 0)
count++;
}
printf("不及格人数为:%d\n",count);
}
void struct_insert(boy boy_test01[],int n)
{
boy p =
{
.num=105,
.name="ma li",
.sex='f' ,
.score=20
};
memcpy( (void *)(boy_test01+5), (void *)(boy_test01+4), sizeof(boy_test01[4]) );
memcpy( (void *)(boy_test01+4), (void *)(&p), sizeof(boy_test01[4]));
}
void struct_del(boy boy_test01[],int n)
{
boy p =
{
.num=0,
.name="NULL",
.sex='0' ,
.score=0
};
memcpy( (void *)(boy_test01+3), (void *)(&p), sizeof(boy_test01[3]));
}
int main()
{
boy *p=boy_test;
struct_count(p,6);
struct_print(p,6);
struct_insert(p,6);
struct_print(p,6);
struct_del(p,6);
struct_print(p,6);
return 0;
}
运行结果:

边栏推荐
- 7-1 LVS+NAT load balancing cluster, NAT mode deployment
- Basic form validation process
- sql语句查询String类型字段小于10的怎么查
- Mobile payment online and offline payment scenarios
- 企业直播风起:目睹聚焦产品,微赞拥抱生态
- 元宇宙“吹鼓手”Unity:疯狂扩局,悬念犹存
- 转:管理是对可能性的热爱,管理者要有闯进未知的勇气
- How class only static allocation and dynamic allocation
- Learn iframes and use them to solve cross-domain problems
- SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropri
猜你喜欢
随机推荐
SQL interview Questions
7-3 LVS+Keepalived Cluster Description and Deployment
系统设计.如何设计一个秒杀系统(完整版 转)
系统设计.秒杀系统
PHP高级开发案例(1):使用MYSQL语句跨表查询无法导出全部记录的解决方案
【技巧】借助Sentinel实现请求的优先处理
Deep learning -- CNN clothing image classification, for example, discussed how to evaluate neural network model
7-1 LVS+NAT load balancing cluster, NAT mode deployment
Gigabit 2 X light 8 electricity management industrial Ethernet switches WEB management - a key Ring Ring net switch
移动支付线上线下支付场景
中信证券网上开户怎么开的?安全吗?
嵌入式数据库开发编程MySQL(全)
new Date converts strings into date formats Compatible with IE, how ie8 converts strings into date formats through new Date, how to replace strings in js, and explain the replace() method in detail
A Preliminary Study of RSS Subscription to WeChat Official Account-feed43
如何简化现代电子采购的自动化?
机器学习之视频学习【更新】
打造一份优雅的简历
2022杭电多校联赛第五场 题解
7-2 LVS+DR Overview and Deployment
8.Haproxy 搭建Web集群








