减去两个时间间隔 [英] Subtracting two time interval

查看:25
本文介绍了减去两个时间间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想减去两个时间间隔.这里一个时间间隔是 5 小时 30 分钟,另一个是当前时间.代码编写如下.

I wanted to subtract two time interval. here one time interval is 5hour 30 minute and other is current time.the code is written as follow.

main()
{
int Time1;
int Time2;
int hour=10;
int minute=5;
int second=13;
int h; int m;
int  Ntime;
Time1=(60*5)+(30);
Time2=60*hour+minute;
Ntime=Time2-Time1;
 m=(Ntime%60);
  Ntime=Ntime/60;
  h=(int)(Ntime);
printf("hour after subtraction is : %d hour %d min",h,m) 

}

推荐答案

我没有查看您程序中的任何逻辑错误,但您发布的错误是由于 mod 运算符 ie % 期望操作数为整数.所以如果你以这种方式修改你的代码,它应该会消除错误.

I have not looked at any logical errors in your program but the error you post is due to the fact that the mod operator i.e. % expects the operand to be integer. So if you modify your code in this way, it should remove the error.

main()
{
int Time1;
int Time2;
int hour=10;
int minute=5;
int second=13;
int h; int m;
int  Ntime; //double has been changed to int
double Ntime2;
Time1=(3600*5)+(60*30);
Time2=(3600*hour)+(60*minute)+second;
Ntime=Time2-Time1;
Ntime2=((double)((Ntime%60)/100) + (double)(Ntime/60));

h=(int)(Ntime2);
m=((Ntime2 - (double)h)*100);
printf("hour after subtraction is : %d hour %d min",h,m) 
}

您的代码中涉及太多类型转换,您应该寻找一种更简单的方法来执行此操作.查看 time.h 头文件,您可能会发现一些有用的东西.

There is too much type casting involved in your code, you should look for a simpler way to do this. Look into the time.h header file, you may find something useful to work with.

这篇关于减去两个时间间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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