C 语言 求指定一起加一天后的日期

这段C语言代码实现了一个功能,即输入一个日期后,计算并输出该日期加一天的结果。程序首先定义了闰年月份天数的数组,接着读取用户输入的日期,通过函数判断是否为合法日期,并计算下一天的日期。如果日期是2月29日且非闰年,会返回错误信息。最后,根据计算结果输出新的日期。
摘要由CSDN通过智能技术生成

//---------------------------------------------------------------------------

#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused
int LeapYear_Month_SumDay[12]={31,29,31,30,31,30,31,31,30,31,30,31};//闰年数组,保存各月天数。

int main(int argc, char* argv[])
{
    int Year=0,Month=0,Day=0,NextDay=0;
    printf("pleace enter right Date(example:2005 6 27):/n");
    scanf("%d%d%d",&Year,&Month,&Day);
    NextDay=ReturnNextDay(Year,Month,Day);

    switch (NextDay)
    {
        case 0: printf("not a current date/n");
        break;
        case 1:
        if (Month==12)
        {
            Year++;
            Month=1;
        }
        else
        {
            Month++;
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 下面是一个简单的 C 语言程序,用于计算给定年份的第 n 天所对应的日期。程序假定每个月有 30 天。你可以根据需要进行修改以适应实际情况。 ```c #include <stdio.h> int main() { int year, day, month, days_in_month, day_in_year; printf("Enter year: "); scanf("%d", &year); printf("Enter day of the year: "); scanf("%d", &day); month = 1; days_in_month = 30; day_in_year = 0; while (day > days_in_month && month <= 12) { day -= days_in_month; month++; days_in_month = 30; } day_in_year += day; printf("Date: %d-%d-%d\n", year, month, day_in_year); return 0; } ``` 在这个程序中,我们首先输入年份和第 n 天。然后,我们初始化月份为 1,每个月的天数为 30,一年中的天数为 0。 接下来,我们使用一个循环来递增月份和减少天数,直到我们找到了正确的月份。我们假定每个月都有 30 天,但在实际情况下,每个月的天数是有所不同的。因此,你需要根据实际情况进行修改。 当我们找到了正确的月份时,我们计算出在该月中的天数(即 day_in_year),然后输出日期。 ### 回答2: 要使用C语言编写一个程序来找到指定年份的第n天对应的日期。我们可以使用C语言中的结构体来表示日期。首先,我们需要定义一个包含年、月、日的结构体,如下所示: ``` struct Date { int year; int month; int day; }; ``` 接下来,我们可以编写一个函数来计算指定年份的第n天对应的日期。函数的输入参数包括年份和天数,返回值为一个日期结构体。 ```c #include <stdio.h> #include <stdbool.h> bool isLeapYear(int year) { if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { return true; } else { return false; } } struct Date getNthDate(int year, int n) { struct Date date; int daysOfMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (isLeapYear(year)) { daysOfMonth[1] = 29; } date.year = year; date.month = 1; date.day = 0; while (n > 0) { int remainingDays = daysOfMonth[date.month-1] - date.day; if (n > remainingDays) { n -= remainingDays; date.month++; date.day = 0; } else { date.day += n; break; } } return date; } int main() { int year, n; printf("请输入年份和天数:"); scanf("%d %d", &year, &n); struct Date result = getNthDate(year, n); printf("第%d天对应的日期为:%d年%d月%d日", n, result.year, result.month, result.day); return 0; } ``` 以上就是使用C语言编写一个指定年份的第n天日期的程序。程序首先判断给定的年份是否为闰年,然后根据不同月份的天数计算并更新日期。最后,程序输出计算得到的日期。 ### 回答3: 要编写一个C语言程序来指定年份的第n天的日期,首先需要了解一些基本的知识。 1. 闰年判断: - 如果年份能被4整除,但不能被100整除,则为闰年。 - 如果年份能被400整除,则也是闰年。 2. 月份天数: - 1、3、5、7、8、10和12月有31天。 - 4、6、9和11月有30天。 - 2月平年有28天,闰年有29天。 3. 算法思路: - 首先,判断给定的年份是否为闰年。如果是,则将2月的天数设为29,否则为28。 - 然后,根据给定的天数n,逐个减去每个月的天数,直到n不够减时,即找到了对应的月份。 - 最后,根据剩下的天数n以及当前的月份,日期。 下面是一个简单的C语言代码示例: ```c #include <stdio.h> int isLeapYear(int year) { if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { return 1; } else { return 0; } } void findDate(int year, int n) { // 判断是否为闰年 int isLeap = isLeapYear(year); int daysInMonth[13] = {0, 31, 28 + isLeap, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // 逐个减去月份的天数 int month = 1; while (n > daysInMonth[month]) { n -= daysInMonth[month]; month++; } // 输出日期 printf("%d年第%d天是%d月%d日\n", year, n, month, n); } int main() { int year, n; printf("请输入年份和天数:"); scanf("%d %d", &year, &n); findDate(year, n); return 0; } ``` 以上是一个简单的C语言代码示例,用于指定年份的第n天的日期。用户需要输入年份和天数,程序将计算出对应的日期并输出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值