简体   繁体   English

动态C,如何将2个“小时”添加为4位整数

[英]Dynamic C, How to add 2 “hours” as 4 digit Integer

I need to know how can I add 2 "hours" as integer? 我需要知道如何将2个“小时”加为整数? It is 24-h format 24小时制

int add2Hours(int _time1,int _time2)
{ 
} 

sample: 13:45 is: (hhmm) 1345 样本:13:45是:(hhmm)1345

1345 + 30 returns 1415 1345 + 30返回1415

Your time is in hhmm format, separate the hh and mm part. 您的时间为hhmm格式,将hhmm部分分开。 Then add the parts separately. 然后分别添加零件。

int add2hours(int _time1, int _time2)
{
    int hh1, hh2, mm1, mm2;
    int rHH,rMM, res;
    hh1 = _time1/100;
    hh2 = _time2/100;
    mm1 = _time1 % 100;
    mm2 = _time2 % 100;
    rMM = mm1 + mm2;
    rHH = rMM/60;
    rMM = rMM % 60;
    rHH = rHH + hh1 + hh2;
    res = rHH*100 + rMM;
    return res;
}

NOTE: This will not handle any time greater than 24hrs. 注意: 这将不会处理超过24小时的任何时间。 eg if the inputs are 2345 and 30, the output will be 2415 instead of 15(0015). 例如,如果输入是2345和30,则输出将是2415,而不是15(0015)。 You have to handle it if you need. 如果需要,您必须处理它。

If the function for adding time is declared as follows, 如果添加时间的函数声明如下,

int add2Hours(int _time1,int _time2);

and the syntax of passing time is as follows, 传递时间的语法如下:

hhmm (For example 2230)

Then you can add the times as follow, 然后,您可以添加以下时间,

temp1= _time1;
temp2= _time2;

m1 = temp1 % 100;
m2 = temp2 % 100;

h1 = temp1 / 100;
h2 = temp2 / 100;

m3 = m1 + m2;

m3 > 59 ? m3=m3%60, h3=1 : h3=0;

h3 = h1 + h2 + h3;

h3 > 23 ? h3=h3%24, d=1 : d=0; /* If more than 23 hours */

printf("\nThe time is %d-%d-%d",d,h3,m3);   

First convert the time to a common domain(sec/millisec...). 首先将时间转换为通用域(秒/毫秒...)。 Then add and make the result to the required format. 然后添加并将结果制成所需的格式。

  • Add the hours together. 将小时数加在一起。 13 + 0. 13 + 0。
  • Get the minutes of each time by subtracting the time with the time/100*100. 通过用时间/ 100 * 100减去时间来获得每次的分钟数。 Note that integer arithmetic in C truncates. 请注意,C中的整数算术会截断。 Example: 例:

m = time - (time/100*100) m =时间-(时间/ 100 * 100)

m = 1345 - (1345/100*100) m = 1345-(1345/100 * 100)

m = 1345 - (13*100) m = 1345-(13 * 100)

m = 1345 - 1300 m = 1345-1300

m = 45 m = 45

  • Add the minutes together. 将分钟数加在一起。 45 + 30 = 75. 45 + 30 = 75。
  • Add "the minute sum/60" hours to the result. 将“分钟总数/ 60”小时添加到结果中。 Ie 75/60 = 1. 即75/60 = 1。
  • Then add "the minute sum modulo 60" minutes to the same result. 然后将“分钟总和取模60”分钟添加到相同的结果。 Ie 75%60 = 15. 即75%60 = 15。

How about this? 这个怎么样?

int add2Hours(int _time1,int _time2)
{
    int minutes = (_time1%100 + _time2%100)
    int time = (((minutes/60)+(_time1/100 + _time2/100))%24)*100 + minutes%60;
    return time;
}

Thanks guys... here is my function! 谢谢大家...这是我的职责!

int Add2Times (int _time1, int _time2)
{
    int hour = _time1 / 100 + _time2 / 100;
    int min = _time1 % 100 + _time2 % 100;

    hour += min / 60;
    min = min % 60;


    hour = hour % 24;

    return hour * 100 + min;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM