C语言程序设计——结构体 给定如下定义:struct date_rec { int day ; int month ; int year ; } ; struct date_rec current_

给定如下定义:
struct date_rec
  {
    int day ;
    int month ;
    int year ;
  } ;
  struct date_rec current_date ;
写一个程序包含如下的函数,完成:
(a) 输入current_date的值:
void input_date(struct date_rec *current_date)
(b) 将current_date增加1天:
void increment_date(struct date_rec *current_date)
(c) 显示current_date的值:
void output_date(struct date_rec *current_date)
考虑每个月的实际天数,同时也考虑闰年的情况。

**输入格式要求:"%d%d%d" 提示信息:"请输入当前日期(年 月 日):"
**输出格式要求:"当前日期:%d年%d月%d日!" (加1天后的新日期)

代码如下:

#include <stdio.h>

struct date_rec
{     		  
    int day;
    int month;
    int year;
}     		  ;

struct date_rec current_date;

int days_of_month[][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 is_leap(int year)
{     		  
    return ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)));
}     		  

void input_date(struct date_rec *current_date)
{     		  
    printf("请输入当前日期(年 月 日):");
    scanf("%d%d%d", &current_date->year, &current_date->month, &current_date->day);
}     		  

void increment_date(struct date_rec *current_date)
{     		  
    current_date->day++;
    if (current_date->day > days_of_month[is_leap(current_date->year)][current_date->month])
    {     		  
        current_date->day = current_date->day - days_of_month[is_leap(current_date->year)][current_date->month]
                            ;
        current_date->month++;
        if (current_date->month > 12)
        {     		  
            current_date->year++;
            current_date->month = current_date->month - 12;
        }
    }
}     		  

void output_date(struct date_rec *current_date)
{     		  
    printf("当前日期:%d年%d月%d日!", current_date->year,
           current_date->month, current_date->day);
}     		  

int main()
{     		  
    input_date(&current_date);
    increment_date(&current_date);
    output_date(&current_date);

    return 0;
}     		  

好的,这是一个关于时间结构体的问题,让我来为您提供C语言的实现方案。 以下是实现程序: ```c #include <stdio.h> struct time_rec { int hours; int mins; int secs; }; void input_time(struct time_rec *current_time); void increment_time(struct time_rec *current_time); void output_time(struct time_rec *current_time); int main() { struct time_rec current_time; input_time(&current_time); increment_time(&current_time); output_time(&current_time); return 0; } void input_time(struct time_rec *current_time) { printf("请输入当前时间(时 分 秒):"); scanf("%d%d%d", &current_time->hours, &current_time->mins, &current_time->secs); } void increment_time(struct time_rec *current_time) { current_time->secs++; if (current_time->secs >= 60) { current_time->secs = 0; current_time->mins++; } if (current_time->mins >= 60) { current_time->mins = 0; current_time->hours++; } if (current_time->hours >= 24) { current_time->hours = 0; } } void output_time(struct time_rec *current_time) { printf("当前时间:%d时%d分%d秒!", current_time->hours, current_time->mins, current_time->secs); } ``` 程序中的 `input_time()` 函数使用指针作为参数,通过输入的方式给 `current_time` 结构体变量赋值;`increment_time()` 函数用于将时间加上1秒,注意如果秒钟超过60,需要对分和时进行进位;`output_time()` 函数用于输出最终的时间。 输入格式要求为 "%d%d%d",即三个整数分别表示当前时间的小时、分钟和秒钟。输出格式要求为 "当前时间:%d时%d分%d秒!",即输出当前时间的小时、分钟和秒钟。 希望能够解答您的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杪商柒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值