【C++】 求两string日期间隔天数
2016-07-12 19:40
330 查看
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
const int MOUTHDAYS[12]{31,28,31,30,31,30,31,31,30,31,30,31};
//返回某年某月天数
int inline MouthDays(int mouth,int year)
{
if(year%4 == 0)
if(mouth == 2)
return 29;
return MOUTHDAYS[mouth-1];
}
//将string转为3个int值
void inline Transformation(string str,int &year,int &mouth,int &day)
{
int flag1 = str.find("/");
year = atoi(str.substr(0,flag1).c_str());
str = str.substr(flag1+1);
int flag2 = str.find("/");
mouth = atoi(str.substr(0,flag2).c_str());
str = str.substr(flag2+1);
day = atoi(str.c_str());
}
int DataMinus(string str1,string str2)
{
int year1,year2;
int mouth1,mouth2;
int day1,day2;
Transformation(str1,year1,mouth1,day1);
Transformation(str2,year2,mouth2,day2);
int num = 0;//总天数统计
//统一日期
num += MouthDays(mouth1,year1)-day1;
mouth1++;
num += day2;
//统一月份
for(int i = mouth1;i<=12;i++)
num += MouthDays(i,year1);
year1++;
for(int i = mouth2-1;i>=1;i--)
num += MouthDays(i,year2);
//按年份计算天数
for(int i = year1;i<year2;i++)
{
if(i%4 == 0)
num += 366;
else
num += 365;
}
return num;
}
int main()
{
string str1("1996/8/24");
string str2("2016/4/12");
cout << DataMinus(str1,str2) << endl;
return 0;
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android之获取手机上的图片和视频缩略图thumbnails
- android string.xml文件中的整型和string型代替
- Android java 与 javascript互访(相互调用)的方法例子
- 关于指针的一些事情
- android上改变listView的选中颜色
- String.intern
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Prototype源码浅析 String部分(二)
- Ruby中的String对象学习笔记
- Redis02 使用Redis数据库(String类型)全面解析
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- PostgreSQL ERROR: invalid escape string 解决办法
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题