当前位置:网站首页>结构体函数练习
结构体函数练习
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;
}
运行结果:

边栏推荐
猜你喜欢
![The video of machine learning to learn [update]](/img/e7/c9a17b4816ce8d4b0787c451520ac3.png)
The video of machine learning to learn [update]

目标检测-中篇

This Thursday evening at 19:00, the fourth live broadcast of knowledge empowerment丨The realization of equipment control of OpenHarmony smart home project
2022年软件测试——精选金融银行面试真题
SQL injection in #, - +, - % 20, % 23 is what mean?

Learn iframes and use them to solve cross-domain problems

drools从下载到postman请求成功

mq应用场景介绍

10 Convolutional Neural Networks for Deep Learning 3

十一种概率分布
随机推荐
XSS相关知识点
y86.第四章 Prometheus大厂监控体系及实战 -- prometheus存储(十七)
企业直播风起:目睹聚焦产品,微赞拥抱生态
FFmpeg —— 录制麦克风声音(附源码)
4-way two-way HDMI integrated business high-definition video optical transceiver 8-way HDMI high-definition video optical transceiver
FPGA parsing B code----serial 3
什么是数字孪生智慧城市应用场景
张量篇-应用案例
7. The principle description of LVS load balancing cluster
【 observe 】 super fusion: the first mention of "calculate net nine order" evaluation model, build open prosperity of power network
拿捏JVM性能优化(自己笔记版本)
2022 Hangzhou Electric Power Multi-School League Game 5 Solution
Senior PHP development case (1) : use MYSQL statement across the table query cannot export all records of the solution
软件测试如何系统规划学习呢?
缓存穿透、缓存击穿、缓存雪崩以及解决方案
7-1 LVS+NAT load balancing cluster, NAT mode deployment
RSS订阅微信公众号初探-feed43
用户与用户互发红包/支付宝C2C/B2C现金红包php源码示例/H5方式/兼容苹果/安卓
机器学习模型的“可解释性”
初识Numpy