计算时差.... [英] Calculate Time Difference....

查看:107
本文介绍了计算时差....的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个程序来查找开始时间和结束时间之间的时差。如何计算时差,因为我没有得到正确的区别?



********************************



  #include   <   iostream.h  >  
# include < conio.h >
class time
{
int 小时;
int 分钟;
int 秒;
public
time()
{
hour = 0 < /跨度>;
分钟= 0 ;
秒= 0 ;
}
void get_time()
{
cout<< *********** START TIME / END TIME ***********<< ; ENDL;
cout<< 输入小时:;
cin>>小时;
cout<< 输入分钟:;
cin>>分钟;
cout<< 输入第二个:;
cin>>秒;
cout<< endl;
}
朋友 void 运算符 - (时间,时间);
};
void operator - (时间t,时间s)
{
int a,b,c;
a = t.hour-s.hour;
b = t.minute-s.minute;
c = t.second-s.second;
cout<< 开始时间和结束时间之间的差异=<< ;<< - << b<< - << c<< endl;
}
int main()
{
clrscr();
time st,et;
st.get_time();
et.get_time();
st-et;
getch();
return 0 ;
}

解决方案

假设您可以从小时,分钟中减去小时数,不要试图计算时差从几分钟开始。



想一想:45分钟前你怎么锻炼?从当前的分钟数减去45,并在必要时回收teh小时。



最简单的方法是将两个时间转换为几秒钟:

 totalSeconds =((小时*  60  +分钟)*  60 )+秒



然后从另一个中减去一个,并将结果转换回分钟,然后将分数和模数转换为60小时。


I Have created a program to find time difference between start time and end time.How to calculate the time difference as I am not getting the right difference?

********************************

#include<iostream.h>
#include<conio.h>
class time
{
   int hour;
   int minute;
   int second;
   public:
      time()
      {
         hour=0;
         minute=0;
         second=0;
      }
      void get_time()
      {
         cout<<"*********** START TIME/END TIME ***********"<<endl;
         cout<<"Enter Hour:";
         cin>>hour;
         cout<<"Enter Minute:";
         cin>>minute;
         cout<<"Enter Second:";
         cin>>second;
         cout<<endl;
      }
      friend void operator-(time,time);
};
void operator-(time t,time s)
{
   int a,b,c;
   a=t.hour-s.hour;
   b=t.minute-s.minute;
   c=t.second-s.second;
   cout<<"Difference Between Start Time & End Time="<<a<<"-"<<b<<"-"<<c<<endl;
}
int main()
{
   clrscr();
   time st,et;
   st.get_time();
   et.get_time();
   st-et;
   getch();
   return 0;
}

解决方案

Don't try to work out a time difference by assuming that you can subtract hours from hours, minutes from minutes and so on.

Think about it: how do you work out 45 minutes ago? By subtracting 45 from the current minutes and winding teh hour back if necessary.

The easest way to do this is to convert both times into a number of seconds:

totalSeconds = ((hours * 60 + minutes) * 60) + seconds


Then subtract one from the other, and convert the result back to minutes, and then hours by divide and modulus by 60.


这篇关于计算时差....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆