当前位置:网站首页>函数在结构体中的应用练习
函数在结构体中的应用练习
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;
}
运行结果:
边栏推荐
猜你喜欢
IronOS, an open source system for portable soldering irons, supports a variety of portable DC, QC, PD powered soldering irons, and supports all standard functions of smart soldering irons
An animation optimization of shape tween and optimization of traditional tweening
VLAN 实验
第07章 InnoDB数据存储结构【2.索引及调优篇】【MySQL高级】
An动画基础之按钮动画与基础代码相结合
Yahoo!Answers - data set
类和对象(中上)
PyTorch构建分类网络模型(Mnist数据集,全连接神经网络)
An animation optimization of traditional guide layer animation
设计思维 | 详看设计工作坊Workshop的11个关键技巧
随机推荐
优思学院|2022年获美质协ASQ和ILSSI奖项的《精益六西格玛的十条戒律》
Sogou news - dataset
Win11怎么禁止软件后台运行?Win11系统禁止应用在后台运行的方法
4年工作经验,多线程间的5种通信方式都说不出来,你信吗?
漫画:怎么证明sleep不释放锁,而wait释放锁?
超大规模的产业实用语义分割数据集PSSL与预训练模型开源啦!
An animation basic element movie clip effect
leetcode 448. Find All Numbers Disappeared in an Array 找到所有数组中消失的数字(简单)
Jmeter use
BOM系列之sessionStorage
Golang GMP principle
Golang sync.WaitGroup
Heaps
【二叉树】统计最高分的节点数目
不卷不pua,早9晚6,这个招聘深得我心
Nanoprobes金脂质偶联物的相关应用
【深度学习】高效轻量级语义分割综述
An动画优化之遮罩层动画
冷链行业商业供应链系统:实现全流程数字化协同,激活企业迸发市场活力
ECCV 2022|通往数据高效的Transformer目标检测器