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

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

问题描述

我想减去两个时间间隔。这里一个时间间隔是5小时30分钟等是当前time.the code被写入如下。

 的main()
{
INT时间1;
INT时间2;
INT小时= 10;
INT分钟= 5;
诠释第二= 13;
INT H; INT米;
INT n时间;
时间1 =(60 * 5)+(30);
时间2 = 60 *小时+分钟;
n时间=时间2,时间1;
 M =(n时间60%);
  n时间= n时间/ 60;
  H =(INT)(n时间);
的printf(减法后的小时为:%d个小时%d个分,H,M)}


解决方案

我没有看过你的程序中任何逻辑错误,但您发布的错误是由于该Mod运算符,即%预期操作数是整数的事实。所以,如果您修改code以这种方式,应该删除错误。

 的main()
{
INT时间1;
INT时间2;
INT小时= 10;
INT分钟= 5;
诠释第二= 13;
INT H; INT米;
INT n时间; //双已被更改为int
双Ntime2;
时间1 =(3600 * 5)+(60 * 30);
时间2 =(3600 *小时)+(60 *分钟)+第二;
n时间=时间2,时间1;
Ntime2 =((双)((n时间60%)/ 100)+(双)(n时间/ 60));H =(INT)(Ntime2);
M =((Ntime2 - (双)H)* 100);
的printf(减法后的小时为:%d个小时%d个分,H,M)
}

有是涉及您的code太多类型转换,你应该寻找一个更简单的方式来做到这一点。看看到time.h中的头文件,你可能会发现有用的东西的工作。

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) 

}

解决方案

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) 
}

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天全站免登陆