借阅时刻和归还时刻在C语言中的表示技巧
在我们的日常生活中,借阅书籍一个常见的场景,怎样在C语言中表示借阅时刻和归还时刻呢?这篇文章小编将为无论兄弟们详细介绍怎样在C语言中表示借阅时刻和归还时刻。
我们需要定义一个结构体来存储日期和时刻信息,在C语言中,可以使用struct关键字来定义结构体,下面内容一个简单的结构体定义,用于存储日期和时刻信息:
include <stdio.h>struct Date int year; int month; int day;};struct Time int hour; int minute; int second;};struct BorrowReturn struct Date borrowDate; struct Date returnDate; struct Time borrowTime; struct Time returnTime;};
我们需要为结构体中的每个成员编写相应的函数,用于表示借阅时刻和归还时刻,下面内容一个示例:
// 设置借阅日期void setBorrowDate(struct BorrowReturn borrowReturn, int year, int month, int day) borrowReturn->borrowDate.year = year; borrowReturn->borrowDate.month = month; borrowReturn->borrowDate.day = day;}// 设置归还日期void setReturnDate(struct BorrowReturn borrowReturn, int year, int month, int day) borrowReturn->returnDate.year = year; borrowReturn->returnDate.month = month; borrowReturn->returnDate.day = day;}// 设置借阅时刻void setBorrowTime(struct BorrowReturn borrowReturn, int hour, int minute, int second) borrowReturn->borrowTime.hour = hour; borrowReturn->borrowTime.minute = minute; borrowReturn->borrowTime.second = second;}// 设置归还时刻void setReturnTime(struct BorrowReturn borrowReturn, int hour, int minute, int second) borrowReturn->returnTime.hour = hour; borrowReturn->returnTime.minute = minute; borrowReturn->returnTime.second = second;}
我们可以创建一个BorrowReturn结构体实例,并设置借阅时刻和归还时刻:
int main() struct BorrowReturn borrowReturn; // 设置借阅日期和时刻 setBorrowDate(&borrowReturn, 2021, 9, 1); setBorrowTime(&borrowReturn, 10, 30, 0); // 设置归还日期和时刻 setReturnDate(&borrowReturn, 2021, 9, 15); setReturnTime(&borrowReturn, 18, 0, 0); // 打印借阅时刻和归还时刻 printf("借阅时刻:%d年%d月%d日 %d时%d分%d秒\n", borrowReturn.borrowDate.year, borrowReturn.borrowDate.month, borrowReturn.borrowDate.day, borrowReturn.borrowTime.hour, borrowReturn.borrowTime.minute, borrowReturn.borrowTime.second); printf("归还时刻:%d年%d月%d日 %d时%d分%d秒\n", borrowReturn.returnDate.year, borrowReturn.returnDate.month, borrowReturn.returnDate.day, borrowReturn.returnTime.hour, borrowReturn.returnTime.minute, borrowReturn.returnTime.second); return 0;}
怎么样?经过上面的分析示例,我们可以在C语言中表示借阅时刻和归还时刻,希望这篇文章小编将对无论兄弟们有所帮助!?
