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

边栏推荐
猜你喜欢

Based on the statistical QDirStat Qt directory

4-way two-way HDMI integrated business high-definition video optical transceiver 8-way HDMI high-definition video optical transceiver

manipulation of file contents

转:管理是对可能性的热爱,管理者要有闯进未知的勇气

外卖店优先级

MySQL查询优化与调优

【Ryerson情感说话/歌唱视听数据集(RAVDESS) 】

仿牛客论坛项目梳理

2 Gigabit Optical + 6 Gigabit Electric Rail Type Managed Industrial Ethernet Switch Supports X-Ring Redundant Ring One-key Ring Switch

7-3 LVS+Keepalived Cluster Description and Deployment
随机推荐
【21天学习挑战赛】直接插入排序
typescript type 和 interface 的区别
2 Gigabit Optical + 6 Gigabit Electric Rail Type Managed Industrial Ethernet Switch Supports X-Ring Redundant Ring One-key Ring Switch
深度学习——以CNN服装图像分类为例,探讨怎样评价神经网络模型
什么是数字孪生智慧城市应用场景
杭电多校-Slipper-(树图转化+虚点建图)
PHP高级开发案例(1):使用MYSQL语句跨表查询无法导出全部记录的解决方案
《nlp入门+实战:第八章:使用Pytorch实现手写数字识别》
Senior PHP development case (1) : use MYSQL statement across the table query cannot export all records of the solution
数据集类型转换—TFRecords文件
MySQL Query Exercise (1)
7-3 LVS+Keepalived Cluster Description and Deployment
Power button (LeetCode) 215. The first K largest elements in the array (2022.08.03)
"Introduction to nlp + actual combat: Chapter 8: Using Pytorch to realize handwritten digit recognition"
函数,递归以及dom简单操作
Hey, I had another fight with HR in the small group!
2.15 keil使用电脑端时间日期
2022杭电多校联赛第五场 题解
Basic characteristics of TL431 and oscillator circuit
7-1 LVS+NAT load balancing cluster, NAT mode deployment