如何重载运算符“-”处理时间的

发布于 2024-10-24 08:39:38 字数 4204 浏览 0 评论 0原文

尝试找出负运算符时,大脑会痉挛。

资料来源:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassTime
{


    class Program
    {

        class Time
        {
            public int h;
            private int m;
            private int s;

            private int getH()
            {
                return h;
            }

            private int getM()
            {
                return m;
            }

            private int getS()
            {
                return s;
            }

            public void getTime()
            {
                Console.WriteLine("{0} : {1} : {2}", h, m, s);
            }

            //set hours
            private void setH()
            {
                h = 0;
            }

            private void setH(int hours)
            {
                h = hours;
            }

            private void setH(Time obj)
            {
                h = obj.h;
            }

            //set minutes
            private void setM()
            {
                m = 0;
            }

            private void setM(int minutes)
            {
                while (minutes > 59)
                {
                    h++;
                    minutes -= 60;
                }
                m = minutes;
            }

            private void setM(Time obj)
            {
                m = obj.m;
            }

            //set seconds
            private void setS()
            {
                s = 0;
            }

            private void setS(int seconds)
            {
                while (seconds > 59)
                {
                    m++;
                    seconds -= 60;
                }
                s = seconds;
            }

            private void setS(Time obj)
            {
                s = obj.s;
            }

            //set Time
            private void setTime()
            {
                setH();
                setM();
                setS();
            }

            private void setTime(int hours, int minutes, int seconds)
            {
                setH(hours);
                setM(minutes);
                setS(seconds);
            }

            private void setTime(Time obj)
            {
                setH(obj);
                setM(obj);
                setS(obj);
            }

            //constructors
            public Time()
            {
                setTime();
            }

            public Time(int hours, int minutes, int seconds)
            {
                setTime(hours, minutes, seconds);
            }

            public Time(Time obj)
            {
                setTime(obj);
            }

            public static Time operator +(Time obj1, Time obj2)
            {
                Time temp = new Time();

                temp.h = obj1.h + obj2.h;

                temp.m = obj1.m + obj2.m;
                while (temp.m > 59)
                {
                    ++temp.h;
                    temp.m -= 60;
                }

                temp.s = obj1.s + obj2.s;
                while (temp.s > 59)
                {
                    ++temp.m;
                    temp.s -= 60;
                }
                return temp;               
            }

            public static Time operator -(Time obj1, Time obj2)
            {
                Time temp = new Time();

                temp.s = obj1.s - obj2.s;
                while (temp.s > 59)
                {
                    --temp.m;
                    temp.s -= 60;
                }

                temp.m = obj1.m - obj2.m;
                while (temp.m > 59)
                {
                    --temp.h;
                    temp.m -= 60;
                }

                temp.h = obj1.h - obj2.h;

                return temp;
            }
        }

        static void Main(string[] args)
        { 
            Time a = new Time(3, 180, 70);
            Time b = new Time(5, 30, 170);

            a.getTime();
            b.getTime();

            Time c = new Time();
            c = a + b;
            c.getTime();
        }
    }
}

我应该坐哪个座位?

Get brain cramps tryig to figure it out for the negative operator.

Source:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassTime
{


    class Program
    {

        class Time
        {
            public int h;
            private int m;
            private int s;

            private int getH()
            {
                return h;
            }

            private int getM()
            {
                return m;
            }

            private int getS()
            {
                return s;
            }

            public void getTime()
            {
                Console.WriteLine("{0} : {1} : {2}", h, m, s);
            }

            //set hours
            private void setH()
            {
                h = 0;
            }

            private void setH(int hours)
            {
                h = hours;
            }

            private void setH(Time obj)
            {
                h = obj.h;
            }

            //set minutes
            private void setM()
            {
                m = 0;
            }

            private void setM(int minutes)
            {
                while (minutes > 59)
                {
                    h++;
                    minutes -= 60;
                }
                m = minutes;
            }

            private void setM(Time obj)
            {
                m = obj.m;
            }

            //set seconds
            private void setS()
            {
                s = 0;
            }

            private void setS(int seconds)
            {
                while (seconds > 59)
                {
                    m++;
                    seconds -= 60;
                }
                s = seconds;
            }

            private void setS(Time obj)
            {
                s = obj.s;
            }

            //set Time
            private void setTime()
            {
                setH();
                setM();
                setS();
            }

            private void setTime(int hours, int minutes, int seconds)
            {
                setH(hours);
                setM(minutes);
                setS(seconds);
            }

            private void setTime(Time obj)
            {
                setH(obj);
                setM(obj);
                setS(obj);
            }

            //constructors
            public Time()
            {
                setTime();
            }

            public Time(int hours, int minutes, int seconds)
            {
                setTime(hours, minutes, seconds);
            }

            public Time(Time obj)
            {
                setTime(obj);
            }

            public static Time operator +(Time obj1, Time obj2)
            {
                Time temp = new Time();

                temp.h = obj1.h + obj2.h;

                temp.m = obj1.m + obj2.m;
                while (temp.m > 59)
                {
                    ++temp.h;
                    temp.m -= 60;
                }

                temp.s = obj1.s + obj2.s;
                while (temp.s > 59)
                {
                    ++temp.m;
                    temp.s -= 60;
                }
                return temp;               
            }

            public static Time operator -(Time obj1, Time obj2)
            {
                Time temp = new Time();

                temp.s = obj1.s - obj2.s;
                while (temp.s > 59)
                {
                    --temp.m;
                    temp.s -= 60;
                }

                temp.m = obj1.m - obj2.m;
                while (temp.m > 59)
                {
                    --temp.h;
                    temp.m -= 60;
                }

                temp.h = obj1.h - obj2.h;

                return temp;
            }
        }

        static void Main(string[] args)
        { 
            Time a = new Time(3, 180, 70);
            Time b = new Time(5, 30, 170);

            a.getTime();
            b.getTime();

            Time c = new Time();
            c = a + b;
            c.getTime();
        }
    }
}

which seat should i take?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烦人精 2024-10-31 08:39:38

我根本不会这样做 - 内置 DateTime< /code>TimeSpan< /a> 类已经支持这些运算符以及更多:

DateTime startTime;
DateTime endTime;
TimeSpan diff = endTime - startTime;

TimeSpan t1 = DateTime.Now.AddHours(-1).TimeOfDay;
TimeSpan t2 = DateTime.Now.TimeOfDay;
TimeSpan delta = t2 - t1;

I wouldn't do this at all - the built in DateTime and TimeSpan classes already support these operators and more:

DateTime startTime;
DateTime endTime;
TimeSpan diff = endTime - startTime;

TimeSpan t1 = DateTime.Now.AddHours(-1).TimeOfDay;
TimeSpan t2 = DateTime.Now.TimeOfDay;
TimeSpan delta = t2 - t1;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文