当前位置:网站首页>结构体函数练习
结构体函数练习
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-2 LVS+DR概述与部署

函数,递归以及dom简单操作

drools从下载到postman请求成功

【观察】超聚变:首提“算网九阶”评估模型,共建开放繁荣的算力网络

本周四晚19:00知识赋能第4期直播丨OpenHarmony智能家居项目之设备控制实现

8. Haproxy builds a web cluster

Gigabit 2 X light 8 electricity management industrial Ethernet switches WEB management - a key Ring Ring net switch

基于 SSE 实现服务端消息主动推送解决方案

Eight guiding principles to help businesses achieve digital transformation success

SQL query String field less than 10 how to check
随机推荐
小程序 + 电商,玩转新零售
【MD5】采用MD5+盐的加密方式完成注册用户和登录账号
七夕节,我用代码制作了表白信封
4路双向HDMI综合业务高清视频光端机8路HDMI高清视频光端机
用户与用户互发红包/支付宝C2C/B2C现金红包php源码示例/H5方式/兼容苹果/安卓
Embedded database development programming MySQL (full)
使用serve搭建本地服务器
企业直播风起:目睹聚焦产品,微赞拥抱生态
Implementing a server-side message active push solution based on SSE
系统设计.秒杀系统
pnpm 是凭什么对 npm 和 yarn 降维打击的
技术解析|如何将 Pulsar 数据快速且无缝接入 Apache Doris
RSS订阅微信公众号初探-feed43
Introduction to mq application scenarios
if,case,for,while
拿捏JVM性能优化(自己笔记版本)
How to systematically plan and learn software testing?
基本表单验证流程
Oracle与Postgresql在PLSQL内事务回滚的重大差异
7.LVS负载均衡群集之原理叙述