当前位置: 首页 > news >正文

C语言 写一个函数days,实现某日在本年中是第几天计算。

写一个函数days,

【定义一个结构体变量(包括年、月、日)。计算该日在本年中是第几天,注意闰年问题(即将闰年情况包含在内)】

由主函数将年、月、日传递给days函数,计算后将日子数传回主函数输出。

​#include <stdio.h>typedef struct {int year;int month;int day;
} Date;int isLeapYear(int year) {if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)return 1;return 0;
}int daysOfMonth(int month, int year) {int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};if (month == 2 && isLeapYear(year))return 29;return days[month - 1];
}int dayOfYear(Date date) {int days = 0;for (int i = 1; i < date.month; i++) {days += daysOfMonth(i, date.year);}days += date.day;return days;
}int main() {Date date;printf("Enter year, month, day: ");scanf("%d %d %d", &date.year, &date.month, &date.day);int day = dayOfYear(date);printf("The day is the %dth day of the year.\n", day);return 0;
}[点击并拖拽以移动]
​

程序实现

#include <stdio.h>typedef struct {int year;int month;int day;
} Date;int isLeapYear(int year) {if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)return 1;return 0;
}int daysOfMonth(int month, int year) {int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};if (month == 2 && isLeapYear(year))return 29;return days[month - 1];
}int days(Date date) {int days = 0;for (int i = 1; i < date.month; i++) {days += daysOfMonth(i, date.year);}days += date.day;return days;
}int main() {Date date;printf("Enter year, month, day: ");scanf("%d %d %d", &date.year, &date.month, &date.day);int day = days(date);printf("The day is the %dth day of the year.\n", day);return 0;
}

代码解释:

1. 定义 `days` 函数:编写 `days` 函数,实现第1题的计算逻辑。该函数接受年、月、日作为参数,并返回该日期在一年中的第几天。

2. 主函数调用:在主函数中,读取用户输入的年、月、日,并调用 `days` 函数计算天数,最后将结果输出。

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • c++中grpc简单使用---函数介绍及其代码演示
  • 如何处理selenium Webdriver中的文本框?
  • Linux环境docker部署Firefox结合内网穿透远程使用浏览器测试
  • SpringBoot 日志
  • C:图案打印
  • C++——QT:保姆级教程,从下载到安装到用QT写出第一个程序
  • Android串口开发及读取完整数据的解决方法
  • Vite项目中根据不同打包命令配置不同的后端接口地址,proxy解决跨域
  • Linux中的文件操作
  • 学习java的设计模式
  • XPathParser类
  • 【React】详解如何获取 DOM 元素
  • 【图解网络】学习记录
  • 每日OJ_牛客CM26 二进制插入
  • 【模型】LightGBM
  • CSS魔法堂:Absolute Positioning就这个样
  • ERLANG 网工修炼笔记 ---- UDP
  • JAVA_NIO系列——Channel和Buffer详解
  • java8 Stream Pipelines 浅析
  • JavaScript HTML DOM
  • JavaScript创建对象的四种方式
  • Lsb图片隐写
  • MySQL Access denied for user 'root'@'localhost' 解决方法
  • MySQL QA
  • session共享问题解决方案
  • Vue2.0 实现互斥
  • vue2.0一起在懵逼的海洋里越陷越深(四)
  • zookeeper系列(七)实战分布式命名服务
  • 阿里云爬虫风险管理产品商业化,为云端流量保驾护航
  • 爱情 北京女病人
  • 分布式熔断降级平台aegis
  • 力扣(LeetCode)357
  • 前端每日实战:70# 视频演示如何用纯 CSS 创作一只徘徊的果冻怪兽
  • 探索 JS 中的模块化
  • 小程序、APP Store 需要的 SSL 证书是个什么东西?
  • 【云吞铺子】性能抖动剖析(二)
  • !! 2.对十份论文和报告中的关于OpenCV和Android NDK开发的总结
  • $jQuery 重写Alert样式方法
  • $refs 、$nextTic、动态组件、name的使用
  • (Matlab)遗传算法优化的BP神经网络实现回归预测
  • (分布式缓存)Redis持久化
  • (分布式缓存)Redis分片集群
  • (十八)三元表达式和列表解析
  • (算法)N皇后问题
  • (一)Java算法:二分查找
  • (原创) cocos2dx使用Curl连接网络(客户端)
  • (终章)[图像识别]13.OpenCV案例 自定义训练集分类器物体检测
  • (转) Android中ViewStub组件使用
  • .NET Micro Framework初体验(二)
  • .net 获取某一天 在当月是 第几周 函数
  • .NET运行机制
  • //usr/lib/libgdal.so.20:对‘sqlite3_column_table_name’未定义的引用
  • [ vulhub漏洞复现篇 ] Celery <4.0 Redis未授权访问+Pickle反序列化利用
  • [<死锁专题>]
  • [BFS广搜]迷阵