(小问题 50分)c++ 计算时间间隔多少秒

Daniel_Hsu_CN 2009-01-08 10:32:46
我是学JAVA和c#的
c++不懂,一个朋友要我帮他写个学生代码
很简单 但是c++的基本函数我都不清楚
我没法写 所以请大家帮忙

题目 :用户输入2001年1月1日-2009年1月1日 任意一天,输入完之后要求计算该天与2001-01-01相隔多少秒?
是在DOS下运行的那种..
所以用记事本写比较好 有源码的请教一下
c++我一窍不通 ,请各位大侠 务必写的详细点
...全文
919 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
liunanzhisheng 2009-01-08
  • 打赏
  • 举报
回复
#incude<iostream.h>
int main()
{
int a[12]={0,31,28,31,30,31,30,31,31,30,31,30};
int year,month,day,leapyear,i,tol,sum=0,m=0,n=0;
cout<<"输入年月日"<<endl;
cin>>year>>month>>day;
if((year%4==0 && year%100!=0) || year%400==0) leapyear==1;//确定是不是闰年
else leapyear==0;
for(i=0;i<month;i++)
{
sum+=a[i]+day;
}
if(leapyear==1)
{
sum=sum+1;
if(month==1 || mpmth==2) sum=sum-1;
}//排除1月2月的干扰
for(i=2001;i<2009;i++)//求年的天数
{ if((i%4==0 && I%100!=0) || i%400==0) m++;
else n++;
}
tol=(m*366+n*365+sum)*24*3600;//求总天数的秒数
cout<<"该天与2001-01-01相隔"<<tol<<"秒"<<endl;
return 0;
}
刚刚读大一,做过一个与这差不多的,应该是这样的把.......
hearoequal 2009-01-08
  • 打赏
  • 举报
回复
就算是学C#
也不能把命令行叫DOS啊
fancy_zhh 2009-01-08
  • 打赏
  • 举报
回复

// Another template function

// convert an string to long
template<>
inline long Convert<long>(const std::string& src)
{
char* endPtr = 0;
std::string temp(src);
TrimLR(temp);
return strtol(temp.c_str(), &endPtr, 10); // return 0 if failed
}
fancy_zhh 2009-01-08
  • 打赏
  • 举报
回复
用户输入2001年1月1日-2009年1月1日 任意一天,输入完之后要求计算该天与2001-01-01相隔多少秒?

//伪代码
int main()
{
tm t_end;
tm t_begin;
// note: 下面的字符串时间可以用scanf输入
// 如: char chrEnd[256], chrBegin[256];
// std::cout << "please input end date: " << std::endl;
// scanf("%s", chrEnd);
// std::cout << "please input begin date: " << std::endl;
// scanf("%s", chrBegin);

if (Convert("2009-01-01 00:00:00", true, t_end)) {
return 1;
}

if (Convert("2001-01-01 00:00:00", true, t_begin)) {
return 1;
}

time_t tt_begin = TmToTime_t(t_begin);
time_t tt_end = TmToTime_t(t_end);

std::cout << "interval: " << tt_end - tt_begin << std::endl;
return 0;
}

//将一个tm格式的时间转换成time_t(time_t也就是秒)
inline time_t TmToTime_t(tm& t)
{
time_t tt = mktime(&t);
if (tt == -1) {
return 0;
}
return tt;
}

//将一个字符串时间转换成tm格式的时间
inline bool Convert(const std::string& src, const bool& bFormated, tm& dtime)
{
if (bFormated) { //src: 2005-12-12 12:12:12
size_t stLength = src.length();
//这里只是进行粗略的数据正确性检查
if ( stLength != 19 || //检查字符的个数
(count(&(src[0]), &(src[stLength]), '-') != 2) || //检查'-'字符的个数
(count(&(src[0]), &(src[stLength]), ':') != 2) || //检查':'字符的个数
(count(&(src[0]), &(src[stLength]), ' ') != 1)) { //检查' '字符的个数
return false;
}

int beginPos = 0;
int endPos = src.find_first_of('-', 0);
//年
dtime.tm_year = Convert<long>(src.substr(beginPos, endPos));
dtime.tm_year -= 1900;
//月
beginPos = endPos;
endPos = src.find_last_of('-', stLength);
dtime.tm_mon = Convert<long>(src.substr(beginPos + 1, endPos));
dtime.tm_mon -= 1;
//日
beginPos = endPos;
endPos = src.find_first_of(' ', beginPos);
dtime.tm_mday = Convert<long>(src.substr(beginPos + 1, endPos));
//时
beginPos = endPos;
endPos = src.find_first_of(':', beginPos);
dtime.tm_hour = Convert<long>(src.substr(beginPos + 1, endPos));
//分
beginPos = endPos;
endPos = src.find_last_of(':', stLength);
dtime.tm_min = Convert<long>(src.substr(beginPos + 1, endPos));
//秒
beginPos = endPos;
dtime.tm_sec = Convert<long>(src.substr(beginPos + 1, stLength + 1));
} else { //src: 20051212121212
//得到字符串中字符的个数
size_t stLength = src.length();
//这里只是进行粗略的数据正确性检查
if (stLength != 14) {
return false;
}
//年
std::string strYear = src.substr(0, 4);
dtime.tm_year = Convert<long>(strYear);
dtime.tm_year -= 1900;
//月
std::string strMonth = src.substr(4, 2);
dtime.tm_mon = Convert<long>(strMonth);
dtime.tm_mon -= 1;
//日
std::string strDate = src.substr(6, 2);
dtime.tm_mday = Convert<long>(strDate);
//时
std::string strHour = src.substr(8, 4);
dtime.tm_hour = Convert<long>(strHour);
//分
std::string strMinute = src.substr(10, 2);
dtime.tm_min = Convert<long>(strMinute);
//秒
std::string strSecond = src.substr(12, 2);
dtime.tm_sec = Convert<long>(strSecond);
}

return true;
}
帅得不敢出门 2009-01-08
  • 打赏
  • 举报
回复
把你写的java或者C#代码贴出来先
Daniel_Hsu_CN 2009-01-08
  • 打赏
  • 举报
回复
哎 是高手太少了 还是愿意帮忙的太少了呢
oyljerry 2009-01-08
  • 打赏
  • 举报
回复
Windows API,改成FILETIME...
直接计算两个之间的差值
Daniel_Hsu_CN 2009-01-08
  • 打赏
  • 举报
回复
没有人知道吗? 高手帮忙呀
ypb362148418 2009-01-08
  • 打赏
  • 举报
回复
找找万年历的写法,差不多了,将天数转为秒就行了
Daniel_Hsu_CN 2009-01-08
  • 打赏
  • 举报
回复
谢谢大家
就呆在云上 2009-01-08
  • 打赏
  • 举报
回复

直接用系统函数就是了萨
c语言里面就有时间转换函数,你设置两个时间点,换成time_t类型就是你要的时间间隔了
xiaoyisnail 2009-01-08
  • 打赏
  • 举报
回复
例子

#include <time.h>
#include <iostream>
using namespace std;

int main()
{
tm pt = {0};
pt.tm_year = 101;//2001年
pt.tm_mon = 0;//1月
pt.tm_mday = 1;//1日

time_t start = mktime(&pt);

cout<<"please enter a day between 2001/1/1 and 2009/1/1, format: 'XXXX XX XX'"<<endl;
int year=0, month=0, day=0;
cin>>year>>month>>day;
pt.tm_year = year-1900;
pt.tm_mon = month-1;
pt.tm_mday = day;

time_t end = mktime(&pt);

cout<<"seconds between 2001/1/1 and "<<pt.tm_year+1900<<"/"<<pt.tm_mon+1<<"/"<<pt.tm_mday<<" is: "<<end-start<<endl;
return 0;
}
waizqfor 2009-01-08
  • 打赏
  • 举报
回复
这没什么困难的啊 贴个类似的 改改

time.h
#ifndef __LX_TIME
#define __LX_TIME
#include <dos.h>
#include <iostream>
using namespace std;
typedef struct time sTime;

class myTime
{
private:
sTime _t1,_t2;
public:
unsigned int _hour,_min,_sec,_hund;
myTime(sTime t);
myTime();
~myTime();
myTime GetTime();
myTime Start();
myTime End();
};

#endif

time.cpp
#include "time.h"

myTime operator -(myTime &T1,myTime &T2)
{
int hr,m,s,hd;
myTime ret;

hr=T1._hour;
hd=T1._hund;
s=T1._sec ;
m=T1._min ;

hd-=T2._hund;
if(hd<0)
{
hd+=100;
s-=1;
}
s-=T2._sec;
if(s<0)
{
s+=60;
m-=1;
}
m-=T2._min;
if(m<0)
{
m+=60;
hr-=1;
}
hr-=T2._hour;

ret._hund=hd;
ret._min=m;
ret._sec=s;
ret._hour=hr;
return ret;
}
myTime::~myTime()
{
// cout<<"Destructor"<<endl;
}
myTime::myTime()
{
// cout<<"Constructor without param"<<endl;
}
myTime::myTime(sTime t)
{
_hour=t.ti_hour;
_min=t.ti_min;
_sec=t.ti_sec;
_hund=t.ti_hund;
// cout<<"Constructor with param"<<endl;
}

myTime myTime::Start()
{
// cout<<"Begin Start"<<endl;
gettime(&_t1);
myTime x(_t1);
// cout<<"Finish Start"<<endl;
return x;
}
myTime myTime::End()
{
// cout<<"Begin End"<<endl;
gettime(&_t2);
myTime tmp1(_t1),tmp2(_t2);
// cout<<"Finish End"<<endl;
return (tmp2-tmp1);
}

myTime myTime::GetTime()
{
sTime t;
gettime(&t);

_hour=t.ti_hour;
_min=t.ti_min;
_sec=t.ti_sec;
_hund=t.ti_hund;

return *this;
}

ostream & operator << (ostream &os ,myTime const T)
{
os<<(int)T._hour<<":"<<(int)T._min<<":"<<(int)T._sec<<"."<<(int)T._hund;
return os;
}

64,682

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧