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

边栏推荐
- leetcode16 Sum of the closest three numbers (sort + double pointer)
- 安全狗《云原生安全威胁分析报告》首次提出双检测模型
- [Practical skills] APP video tutorial for updating APP in CANFD, I2C, SPI and serial port mode of single-chip bootloader (2022-08-01)
- 苹果终于认清现实,销量成为优先考虑,iPhone14将不涨价
- 可视化图表设计Cookbook
- The components of the basis of An animation movie clip animation between traditional filling
- An animation basic element movie clip effect
- d作者:d的新特性
- 第07章 InnoDB数据存储结构【2.索引及调优篇】【MySQL高级】
- Nanoprobes金脂质偶联物的相关应用
猜你喜欢

TensorFlow离线安装包

An工具介绍之钢笔工具、铅笔工具与画笔工具

An introduction to 3D tools
![[Microservice] Multi-level cache](/img/58/72e01c789a862c058cba58b9113272.png)
[Microservice] Multi-level cache

Graphic animation and button animation of an animation basic component

An动画基础之散件动画原理与形状提示点

An animation based button animation combined with basic code

漫画:怎么证明sleep不释放锁,而wait释放锁?

华云数据张华林:投身数字蓝海 绘就云上强国

【R】用grafify搞定统计绘图、方差分析、干预比较等!
随机推荐
Tinymce plugins [Tinymce扩展插件集合]
HCIP第十五天笔记(企业网的三层架构、VLAN以及VLAN 的配置)
365天挑战LeetCode1000题——Day 048 有序队列 脑筋急转弯
leetcode/字符串中的所有变位词(s1字符串的某个排列是s2的子串)的左索引
Jmeter使用
An工具介绍之3D工具
【蓝桥杯选拔赛真题48】Scratch跳舞机游戏 少儿编程scratch蓝桥杯选拔赛真题讲解
豪威集团发布新款5000万像素图像传感器OV50E
Nanoprobes FluoroNanogold 偶联物的特色和应用
Golang sync.WaitGroup
OpenCV perspective transform
来广州找工作有一个多月了,今天终于有着落了,工资7000
An动画基础之元件的影片剪辑动画与传统补间
Nanoprobes金脂质偶联物的相关应用
D the author: d new features
leetcode 11. 盛最多水的容器
An工具介绍之骨骼工具
Golang arrays and slices
An animation optimization of traditional guide layer animation
有哪些好用的IT资产管理平台?