C#:使用递减模数学时钟 [英] C#: decrementing a clock using modulus math

查看:139
本文介绍了C#:使用递减模数学时钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图用手模仿一个24小时制的翻转(含数学与使用的时间跨度类)。递增的部分很容易弄清楚如何翻身从23:00至0:00从,但得到它走另外一条路是谈到了真正令人困惑。这是我到目前为止有:

 静态无效IncrementMinute(INT分钟,INT增量)
{
INT newMin =分钟+增量,
hourIncrement = newMin / 60;

//递增或递减小时
如果((双)newMin%,60℃的放大器;及(双)newMin%60> -1)
hourIncrement = -1;

Console.WriteLine(一小时增量{0},hourIncrement);
}

这是IM的发现是退步的时候,如果的模数问题号之间,它将不能正确递减。例如:它是12:00和你减去61分钟,我们知道时间是十点59的小时应该回退1小时,从12:00持续到晚上11:59,然后再回到从11:00去至10:59。不幸的是即时的方式计算它:newMin%60在这种情况下,只抓住第一小时回滚,但由于第二回滚技术上-1.0166作为剩余部分,并且由于模仅返回一个整数,其四舍五入。我敢肯定,我在想念着一些基本的数学在这里,但可能有人帮助我



编辑:?我这个长期和短期写了许多的方法。有些人比其他人更接近,但我知道这是简单的比它似乎。我知道这一次似乎有点跆拳道是他做的,但你应该能够看到基本上是什么我试着去做。递增的时钟,并具有从23:59至展期0:00很容易。退步已被证明是不那么容易了。



确定,这里的incrementMinute的翻转。简单。但尽量往回走。不工作。

 静态无效IncrementMinute(INT分钟,INT增量)

{
INT newMin =分钟+增量,
hourIncrement = newMin / 60;

敏= newMin%60;

Console.WriteLine(,分,hourIncrement新分钟内{0}和钟头{1}增加);
}


解决方案

我会去的东西简单一点

 公共类时钟
{
公共const int的HourPerDay = 24;
公共const int的MinutesPerHour = 60;
公共const int的MinutesPerDay = MinutesPerHour * HourPerDay;

私人诠释totalMinutes;

公众诠释分钟
{
{返回this.totalMinutes%MinutesPerHour; }
}

公众诠释小时
{
{返回this.totalMinutes / MinutesPerHour; }
}

公共无效AddMinutes(INT分钟)
{
this.totalMinutes + =分钟;
this.totalMinutes%= MinutesPerDay;
如果(this.totalMinutes℃下)
this.totalMinutes + = MinutesPerDay;
}

公共无效AddHours(INT小时)
{
this.AddMinutes(小时* MinutesPerHour);
}

公共重写字符串的ToString()
{
返回的String.Format({} 0:00:{} 1:00,this.Hour ,this.Minute);
}
}



使用范例:

 新的时钟()AddMinutes(-1)。 // 23:59 
新时钟()AddMinutes(-61); // 22:59
新时钟()AddMinutes(-1441); // 23:59
新时钟()AddMinutes(1); // 0点01
新时钟()AddMinutes(61); // 01:01
新时钟()AddMinutes(1441); // 00:01


Trying to emulate the rollover of a 24 hour clock by hand (with math vs. using the timespan classes). The incrementing part was easy to figure out how to roll over from 23:00 to 0:00 and from, but getting it to go the other way is turning out to be really confusing. Here's what I have so far:

static void IncrementMinute(int min, int incr)
{
    int newMin = min + incr,
                hourIncrement = newMin / 60;

    //increment or decrement the hour
    if((double)newMin % 60 < 0 && (double)newMin % 60 > -1)
        hourIncrement = -1;

    Console.WriteLine("Hour increment is {0}: ", hourIncrement);
}

The problem that im finding is when going backwards, if the the modulus of is between numbers, it will not decrement correctly. Example: it is 12:00 and you subtract 61 minutes, we know the time would be 10:59 as the hour should roll back 1 hour for going from 12:00 to 11:59, then back again for going from 11:00 to 10:59. Unfortunately the way im calculating it: newMin % 60 in this case, only grabs the first hour rollback, but since the second rollback is technically -1.0166 as a remainder, and since mod only returns a whole number, its rounding off. Im sure im missing some basic math here, but could someone help me out?

EDIT: I've written this a number of ways long and short. Some are closer than others, but I know this is simpler than it seems. I know this one seems kinda "wtf was he doing", but you should be able to see basically what Im trying to do. Incrementing a clock and having it rollover from 23:59 to 0:00 is easy. Going backwards has proven to be not so easy.

OK, here's the incrementMinute with the rollover. Simple. But try to go backwards. Doesn't work.

static void IncrementMinute(int min, int incr)

        {
            int newMin = min + incr,
                hourIncrement = newMin / 60;

            min = newMin % 60;

            Console.WriteLine("The new minute is {0} and the hour has incremented by {1}", min, hourIncrement);
        }

解决方案

I'd go for something a bit simpler

public class Clock
{
    public const int HourPerDay = 24;
    public const int MinutesPerHour = 60;
    public const int MinutesPerDay = MinutesPerHour * HourPerDay;

    private int totalMinutes;

    public int Minute
    {
        get { return this.totalMinutes % MinutesPerHour; }
    }

    public int Hour
    {
        get { return this.totalMinutes / MinutesPerHour; }
    }

    public void AddMinutes(int minutes)
    {
        this.totalMinutes += minutes;
        this.totalMinutes %= MinutesPerDay;
        if (this.totalMinutes < 0)
            this.totalMinutes += MinutesPerDay;
    }

    public void AddHours(int hours)
    {
        this.AddMinutes(hours * MinutesPerHour);
    }

    public override string ToString()
    {
        return string.Format("{0:00}:{1:00}", this.Hour, this.Minute);
    }
}

Sample usage :

new Clock().AddMinutes(-1);    // 23:59
new Clock().AddMinutes(-61);   // 22:59
new Clock().AddMinutes(-1441); // 23:59
new Clock().AddMinutes(1);     // 00:01
new Clock().AddMinutes(61);    // 01:01
new Clock().AddMinutes(1441);  // 00:01

这篇关于C#:使用递减模数学时钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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