手机
当前位置:查字典教程网 >编程开发 >C语言 >C++自定义函数判断某年某月某日是这一年中第几天
C++自定义函数判断某年某月某日是这一年中第几天
摘要:本文实例讲述了C++自定义函数判断某年某月某日是这一年中第几天的方法。分享给大家供大家参考,具体如下:/**作者:刘同宾*完成日期:2012...

本文实例讲述了C++自定义函数判断某年某月某日是这一年中第几天的方法。分享给大家供大家参考,具体如下:

/* * 作 者: 刘同宾 * 完成日期:2012 年 11 月 30 日 * 版 本 号:v1.0 * * 输入描述: * 问题描述:编写函数判断某年某月某日这一年中是第几天,在主函数中调用该函数。 * 程序输出: * 问题分析:略 * 算法设计:略 */ #include<iostream> using namespace std; int main() { void f(int year,int month,int day); int year,month,day; cout<<"请输入年月日:"<<endl; f(year,month,day); cout<<endl; return 0; } //判断输入的年月日是这一年中的第几天! void f(int year,int month,int day) { int f1(int year,int month,int day); int leap(int year); int t=0,s=0,i; int a[12]={31,28,31,30,31,30,31,31,30,31,30,31}; //定义数组 各月的天数 int b[12]={31,29,31,30,31,30,31,31,30,31,30,31}; while(1) { cin>>year>>month>>day; if(f1(year,month,day)) { if(leap(year)) { for(i=0;i<=month-2;i++) { t=t+b[i]; } s=t+day; } else { for(i=0;i<=month-2;i++) { t=t+a[i]; } s=t+day; } cout<<"这是这一年的第"<<s<<"天!"<<endl; break; } else cout<<"输入错误,请重新输入:"<<endl; } } //判断是否为闰年! int leap(int year) { if((year%4==0&&year%400==0)||(year&4==0&&year%100!=0)) { return true; } else return false; } //判断输入是否合法! int f1(int year,int month,int day) { int days(int year,int month,int day); if(year>0) { if(month>=1&&month<=12) { if(days(year,month,day)) { return true; } else return false; } else return false; } else return false; } //判断输入的天 是否合法! int days(int year,int month,int day) { int leap(int year); if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) { if(day>0&&day<=31) { return true; } else return false; } else if(month==2||month==4||month==6||month==9||month==11) { if(day>0&&day<=30) { return true; } else return false; } else { if(leap(year)) { if(day>0&&day<=29) { return true; } else return false; } else { if(day>0&&day<=28) { return true; } else return false; } } }

运行效果如下图所示:

C++自定义函数判断某年某月某日是这一年中第几天1

PS:这里再为大家推荐一款C语言在线格式化工具,相信在以后的开发中可以用得上:

C语言风格/HTML/CSS/json代码格式化美化工具:

http://tools.jb51.net/code/ccode_html_css_json

希望本文所述对大家C++程序设计有所帮助。

【C++自定义函数判断某年某月某日是这一年中第几天】相关文章:

C++虚析构函数的使用分析

C语言小程序 如何判断三角型类型

C语言字符串操作总结大全(超详细)

数据结构课程设计-用栈实现表达式求值的方法详解

C++中简单读写文本文件的实现方法

c++ 判断奇数偶数实例介绍

c++通过引用实现三个数字求最大值

最长公共子字符串的使用分析

从汇编看c++的默认析构函数的使用详解

如何判断一个数是否为2的幂次方?若是,并判断出来是多少次方?

精品推荐
分类导航