当前位置:网站首页>函数在结构体中的应用练习
函数在结构体中的应用练习
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;
}
运行结果:
边栏推荐
猜你喜欢
力扣刷题 每日两题(一)
How to disable software from running in the background in Windows 11?How to prevent apps from running in the background in Windows 11
设计思维 | 详看设计工作坊Workshop的11个关键技巧
【深度学习】高效轻量级语义分割综述
An动画基础之元件的图形动画与按钮动画
PyTorch构建分类网络模型(Mnist数据集,全连接神经网络)
leetcode 11. The container that holds the most water
[Deep Learning] Overview of Efficient and Lightweight Semantic Segmentation
An introduction to the skeleton tool
Yahoo!Answers - data set
随机推荐
Basic principle of the bulk of the animation and shape the An animation tip point
Golang GMP principle
Multithreading in Redis 6
GameFi industry down but not out | June Report
Insert or Merge
TensorFlow离线安装包
Win11怎么禁止软件后台运行?Win11系统禁止应用在后台运行的方法
Real number rounding and writing to file (C language file)
How to disable software from running in the background in Windows 11?How to prevent apps from running in the background in Windows 11
冷链行业商业供应链系统:实现全流程数字化协同,激活企业迸发市场活力
An动画基础之元件的图形动画与按钮动画
svn安装包和客户端
d写二进制
An introduction to the skeleton tool
OpenCV 透视变换
Nanoprobes Ni-NTA-Nanogold——用于 His 标签标记和检测
leetcode 11. 盛最多水的容器
Relia Tech活性VEGFR重组蛋白丨小鼠 VEGF120实例展示
PyTorch framework to train linear regression model (CPU and GPU environment)
tinymce 如何实现动态国际化