初识结构体
2015-06-30 16:13
281 查看
结构体的声明与使用小练习,输入今天的日期,输出明天的日期。
#include <stdio.h> #include <stdbool.h> #include <stdlib.h> struct date { int month; int day; int year; }; int numberOfDays(struct date d);//本月最大天数 bool isLeap(struct date d);//判断是否为闰年 int main(int argc, const char * argv[]) { // insert code here... struct date today; struct date tomorrow; printf("Please enter today's date (mm dd yyyy): ");//收录今日日期 scanf("%i %i %i",&today.month,&today.day,&today.year); if (today.day>numberOfDays(today)||today.month>12) {//判断输入时期是否合法 printf("Overflow!!!"); return 0; } if (today.day!=numberOfDays(today)) {//计算明天日期 tomorrow.day=today.day+1; tomorrow.month=today.month; tomorrow.year=today.year; } else if(today.month==12) { tomorrow.day=1; tomorrow.month=1; tomorrow.year=today.year+1; }else{ tomorrow.day=1; tomorrow.month=today.month+1; tomorrow.year=today.year; } printf("tomorrow's date is %i-%i-%i",tomorrow.month,tomorrow.day,tomorrow.year); return 0; } int numberOfDays(struct date d){ const int daysPerMonth[12]={31,28,31,30,31,30,31,31,30,31,30,31}; int days=daysPerMonth[d.month-1]; if (d.month==2) { if (isLeap(d)) { days=29; } } return days; } bool isLeap(struct date d){ bool leap=false; if ((d.year%4==0&&d.year%100!=0)||d.year%400==0) { leap=true; } return leap; }
相关文章推荐
- Linux C函数参考手册(PDF版)
- 注册表的组织结构
- Lua教程(十七):C API简介
- SQLSERVER的非聚集索引结构深度理解
- 调整SQLServer2000运行中数据库结构
- C#基础语法:结构和类区别详解
- 深入c# 类和结构的区别总结详解
- c#结构和类的相关介绍
- C#中结构(struct)的部分初始化和完全初始化实例分析
- C#中类与结构的区别实例分析
- C#枚举类型与结构类型实例解析
- C#实现打造气泡屏幕保护效果
- javascript实现表现、结构、行为分离的选项卡效果!
- C/C++数据对齐详细解析
- C 语言基础教程(我的C之旅开始了)[三]
- C++中的extern “C”用法详解
- C 语言基础教程(我的C之旅开始了)[七]
- C字符串操作函数实现方法小结
- C/C++ 宏详细解析