当前位置:网站首页>结构体函数练习
结构体函数练习
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;
}
运行结果:
边栏推荐
猜你喜欢
Basic characteristics of TL431 and oscillator circuit
JVM笔记
drools从下载到postman请求成功
复现20字符短域名绕过
10 Convolutional Neural Networks for Deep Learning 3
y86.第四章 Prometheus大厂监控体系及实战 -- prometheus存储(十七)
SQL query String field less than 10 how to check
基本表单验证流程
基于 SSE 实现服务端消息主动推送解决方案
Explain detailed explanation and practice
随机推荐
千兆2光8电管理型工业以太网交换机WEB管理X-Ring一键环网交换机
马尔可夫链
XSS相关知识点
4路双向HDMI综合业务高清视频光端机8路HDMI高清视频光端机
文件内容的操作
打造一份优雅的简历
2022支付宝C2C现金红包PHP源码DEMO/兼容苹果/安卓浏览器和扫码形式
Jenkins 导出、导入 Job Pipeline
基地址:环境变量
元宇宙“吹鼓手”Unity:疯狂扩局,悬念犹存
八年软件测试工程师带你了解-测试岗进阶之路
如何简化现代电子采购的自动化?
7-1 LVS+NAT load balancing cluster, NAT mode deployment
Use serve to build a local server
mysql index notes
解决问题遇到的问题
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
【源码】使用深度学习训练一个游戏
base address: environment variable
This Thursday evening at 19:00, the fourth live broadcast of knowledge empowerment丨The realization of equipment control of OpenHarmony smart home project