当前位置:网站首页>函数在结构体中的应用练习
函数在结构体中的应用练习
2022-08-03 13:10:00 【BSP初级小学僧】
1.编写一个函数,计算该日在本年中是第几天,注意闰年问题。
要求(定义一个结构体变量(包含年、月、日)。
/*
1.编写一个函数,计算该日在本年中是第几天,注意闰年问题。
要求(定义一个结构体变量(包含年、月、日)。
*/
#include <stdio.h>
struct date_time//声明结构体的全局变量
{
int year;//定义相关类型
int month;
int day;
};
int main()
{
struct date_time time_test;
printf("请输入要判断的年月日:\n");
scanf("%d %d %d",&time_test.year,&time_test.month,&time_test.day);
struct_time(time_test);
return 0;
}
void struct_time(struct date_time time_test)
{
int i;
int ping[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int run[12]={31,29,31,30,31,30,31,31,30,31,30,31};
if(time_test.year%4 == 0&&time_test.year%100 != 0||time_test.year%400 == 0)
{
printf("log:此年为闰年(注意2月份有29天)\n");
int count=0;
for(i=0;i<=time_test.month-2;i++)
count += run[i];
count += time_test.day;
printf("该日是%d的第%d天",time_test.year,count);
}
else
{
printf("log:此年为平年(注意2月份有28天)\n");
int count=0;
for(i=0;i<=time_test.month-2;i++)
count += ping[i];
count += time_test.day;
printf("该日是%d的第%d天",time_test.year,count);
}
}
运行结果:
2.有10个学生,每个学生的数据包括学号、 姓名、3门课程的成绩,从键盘输人10个学
生数据,要求输出3门课程总平均成绩,以及最高分的学生的数据(包括学号、姓名、3
门课程成绩、平均分数)。
/*
2.有10个学生,每个学生的数据包括学号、 姓名、3门课程的成绩,从键盘输人10个学
生数据,要求输出3门课程总平均成绩,以及最高分的学生的数据(包括学号、姓名、3
门课程成绩、平均分数)。
*/
#include <stdio.h>
#include <string.h>
struct kemu
{
int yuwen;
int math;
int english;
};
struct stu_informa//声明结构体的全局变量
{
int id;//定义相关类型
char name[50];
struct kemu sore;//结构体的嵌套
};
void struct_input(struct stu_informa stu[],int n)//录入学生信息
{
int i;
for(i=0;i<n;i++)
{
printf("请输入第%d个学生信息(依次包含id、name、sore.math、sore.english、sore.yuwen):\n",i);
scanf("%d",&stu[i].id);
getchar();
gets(stu[i].name);
scanf("%d",&stu[i].sore.math);
getchar();
scanf("%d",&stu[i].sore.english);
getchar();
scanf("%d",&stu[i].sore.yuwen);
getchar();
}
printf("\n");
}
void struct_print(struct stu_informa stu[],int n)
{
int i,j;
for(i=0;i<n;i++)
{
printf("\n");
printf("stu[%d].id=%d\n",i,stu[i].id);
printf("stu[%d].name=%s\n",i,stu[i].name);
printf("stu[%d].sore=%d\n",i,stu[i].sore.english);
printf("stu[%d].sore=%d\n",i,stu[i].sore.math);
printf("stu[%d].sore=%d\n",i,stu[i].sore.yuwen);
}
}
void struct_count(struct stu_informa stu[],int n)
{
int i=0,sum[n],avg[n],max=0;
for(i=0;i<n;i++)
{
sum[i] += (stu[i].sore.english+stu[i].sore.math+stu[i].sore.yuwen);
printf("第%d位同学的总成绩为:%d \n",i,sum[i]);
avg[i] = (sum[i])/n;
printf("第%d位同学的平均成绩为:%d \n",i,sum[i]);
}
for(i=0;i<n;i++)
{
if(max<sum[i])
{
max=sum[i];
printf("最高成绩为:%d\n",sum[i]);
printf("该学生编号为:%d\n",i);
printf("该学生信息如下:%d\n",i);
printf("id=%d\n",i,stu[i].id);
printf("name=%s\n",i,stu[i].name);
printf("sore.english=%d\n",i,stu[i].sore.english);
printf("sore.math=%d\n",i,stu[i].sore.math);
printf("sore.yuwen=%d\n",i,stu[i].sore.yuwen);
printf("平均分为:%d\n",i,avg[i]);
}
}
}
void test(void)
{
struct stu_informa stu[2];
struct_input(stu,2);
struct_print(stu,2);
struct_count(stu,2);
}
int main()
{
test();
return 0;
}
3.编程,输入2个时刻,定义一个时间的结构体(包括时,分,秒),计算2个
时刻之间的时间差
/*
3.编程,输入2个时刻,定义一个时间的结构体(包括时,分,秒),计算2个
时刻之间的时间差
*/
#include <stdio.h>
#include <string.h>
#include <math.h>
struct time
{
int hour;
int minute;
int s;
};
void struct_input(struct time time[],int n)
{
int i=0,sum[n];
for(i=0;i<n;i++)
{
printf("请输入第%d个时间戳,包含(时、分、秒)以空格分隔,回车结束:\n",i+1);
scanf("%d %d %d",&time[i].hour,&time[i].minute,&time[i].s);
sum[i]=(time[i].hour*3600)+(time[i].minute*60)+time[i].s;
printf("您输入的第%d个时间共计%ds(秒)\n",i+1,sum[i]);
}
int ret=0;
ret=sum[0]-sum[1];
printf("log:%d\n",ret);
printf("两个时间戳的时间差为:%ds(秒)",abs(ret));
}
int test(void)
{
struct time time[2];
struct_input(time,2);
}
int main()
{
test();
return 0;
}
运行结果:
边栏推荐
- 厨卫电器行业数字化集采管理系统:优化产业供应结构,实现采购业务流程集中管控
- 升级农企业务运营建设,智慧供应链管理平台打造“共赢生态链”
- ECCV 2022|通往数据高效的Transformer目标检测器
- In order to counteract the drop in sales and explore the low-end market, Weilai's new brand products are priced as low as 100,000?
- 第07章 InnoDB数据存储结构【2.索引及调优篇】【MySQL高级】
- OpenCV 透视变换
- Hanyuan Hi-Tech G8032 standard ERPS ring network switch Gigabit 4 optical 10 electrical industrial Ethernet switch ring network + WEB management + SNMP VLAN planning
- PyTorch framework to train linear regression model (CPU and GPU environment)
- An动画优化之传统引导层动画
- IDO代币预售dapp开发及NFT模式
猜你喜欢
随机推荐
Graphic animation and button animation of an animation basic component
PyTorch builds a classification network model (Mnist dataset, fully connected neural network)
苹果终于认清现实,销量成为优先考虑,iPhone14将不涨价
The maximum number of sliding window
细胞图像数据的主动学习
An animation basic element movie clip effect
有哪些好用的IT资产管理平台?
国产替代风潮下,电子元器件B2B商城系统如何助力企业突围市场竞争
How to disable software from running in the background in Windows 11?How to prevent apps from running in the background in Windows 11
可视化图表设计Cookbook
In order to counteract the drop in sales and explore the low-end market, Weilai's new brand products are priced as low as 100,000?
Golang GMP principle
升级农企业务运营建设,智慧供应链管理平台打造“共赢生态链”
[Microservice] Multi-level cache
工具模板 | 用APOEM方法消除对用户行为的偏见
Golang Mutex
IDO代币预售dapp开发及NFT模式
【OpenCV】 书本视图矫正 + 广告屏幕切换 透视变换图像处理
An introduction to the camera
Postman插件下载