#include<stdio.h> void month_day(int year,int days,int *month,int *day){ int d[2][13] = { {0,31,28,31,30,31,30,31,31,30,31,30,31}, {0,31,29,31,30,31,30,31,31,30,31,30,31} }; int sum = 0,isRun; if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){ isRun = 1; } else{ isRun = 0; } for(int i=0;i<13;i++){ sum+=d[isRun][i]; if(sum>days){ *month = i-1; *day = days-sum+d[isRun][i]; printf("%d,%d",*month,*day); break; } } } int main(){ int *month,*day; month = day = NULL; int year,days; printf("请输入年份:\n"); scanf("%d",&year); printf("请输入天数:\n"); scanf("%d",&days); month_day(year,days,month,day); printf("今天日期是:%d:%d:%d",year,*month,*day); return 0; }
时间: 2023-06-27 22:00:45 浏览: 80
这段代码的功能是根据输入的年份和天数计算出对应的月份和日期,并打印出来。
其中,month_day函数接收四个参数:年份year,天数days,以及两个指向整型变量的指针month和day。在函数中,首先定义了一个二维数组d,其中d[0]表示平年每个月的天数,d[1]表示闰年每个月的天数。然后,根据输入的年份判断是否为闰年,并根据闰年或平年的情况遍历数组d,计算出对应的月份和日期,并将其通过指针参数返回。
在main函数中,首先定义了两个指向整型变量的指针month和day,并将它们初始化为NULL。然后,从标准输入中读取年份和天数,并调用month_day函数计算出对应的月份和日期。最后,打印出结果。
需要注意的是,由于指针month和day在调用函数时还未被赋值,因此需要在函数内部进行赋值操作,否则会导致程序出错。此外,输出结果中的月份应该加1,因为数组d中的月份是从1开始计数的,而指针month返回的是从0开始计数的月份。
相关问题
输入年和天数,输出对应的月和日。例如:输入2000和61,输出3和1。 函数接口定义: void month_day(int,int,int*,int*); 裁判测试程序样例: #include <stdio.h> #include <stdlib.h> #include <time.h> void month_day(int,int,int*,int*); int main( ) { int d,m,y,yd; while(scanf("%d%d",&y,&yd)!=EOF){ month_day(y,yd,&m,&d); printf("%d-%d\n",m,d); } } /* 你的代码将被嵌在这里 */
实现代码如下:
```c
#include <stdio.h>
void month_day(int year, int day, int *month, int *date) {
int leap = (year%4 == 0 && year%100 != 0) || year%400 == 0; // 判断是否为闰年
int days_in_month[13] = {0,31,28+leap,31,30,31,30,31,31,30,31,30,31}; // 存储每个月的天数
int i;
for (i = 1; day > days_in_month[i]; i++) { // 计算月份和日期
day -= days_in_month[i];
}
*month = i;
*date = day;
}
int main() {
int year, day;
int month, date;
while(scanf("%d%d",&year,&day) != EOF){
month_day(year, day, &month, &date);
printf("%d-%d\n", month, date);
}
return 0;
}
```
程序的核心在 `month_day` 函数中,通过判断是否为闰年,以及存储每个月的天数,计算出对应的月份和日期。在 `main` 函数中,循环读取输入的年和天数,并调用 `month_day` 函数,输出对应的月份和日期。
用c语言编程自定义1个函数day_of_year(year, month, day),计算并返回年year、月month和日day对应的是该年的第几天。 函数接口定义: int day_of_year(year, month, day); year, month, day分别为输入的年,月,日 裁判测试程序样例: #include<stdio.h> int day_of_year(int year, int month, int day); int main(void) { int year,month,day; scanf("%d%d%d",&year,&month,&day); printf("%d",day_of_year(year,month,day)); return 0; } /* 请在这里填写答案 */ 输入样例: 2000 3 1 输出样例: 61
以下是实现函数day_of_year的代码:
```
#include <stdio.h>
int day_of_year(int year, int month, int day) {
int days_in_month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int i, days = 0;
// 判断是否为闰年
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
days_in_month[1] = 29;
}
// 计算天数
for (i = 0; i < month - 1; i++) {
days += days_in_month[i];
}
days += day;
return days;
}
int main(void) {
int year, month, day;
scanf("%d%d%d", &year, &month, &day);
printf("%d", day_of_year(year, month, day));
return 0;
}
```
该函数首先判断给定的年份是否为闰年,根据闰年的情况修改二月的天数。然后根据输入的月份和天数计算出该日期是该年的第几天,最后返回这个天数。在主函数中,读入年、月、日,调用day_of_year函数计算该日期是该年的第几天,并输出结果。
相关推荐
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)