Перегрузка оператора сложения - C# (199545)

Узнай цену своей работы

Формулировка задачи:

помогите пожалуйста создать перегрузку для + так чтоб секунды увеличивались на 1 класс програм
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace ConsoleApplication13
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. Console.WriteLine("Введите время");
  12. Console.WriteLine("Часы:");
  13. int Hours = Convert.ToInt32(Console.ReadLine());
  14. Console.WriteLine("Минуты:");
  15. int Minutes = Convert.ToInt32(Console.ReadLine());
  16. Console.WriteLine("Секунды:");
  17. int Seconds = Convert.ToInt32(Console.ReadLine());
  18. var tim = new Time(Hours, Minutes, Seconds);
  19.  
  20. Console.ReadKey();
  21. }
  22. }
  23. }
класс тайм
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace ConsoleApplication13
  6. {
  7. class Time
  8. {
  9. public Time(int Hours, int Minutes, int Seconds)
  10. {
  11. if (Hours > 23 || Hours < 0)
  12. {
  13. Console.WriteLine("Неверный формат времени(часов)");
  14. }
  15. if (Minutes > 59 || Minutes < 0)
  16. {
  17. Console.WriteLine("Неверный формат времени(минут)");
  18. }
  19. if (Seconds > 59 || Minutes < 0)
  20. {
  21. Console.WriteLine("Неверный формат времени(секунд)");
  22. }
  23. else
  24. Console.WriteLine("{0}:{1}:{2}", Hours, Minutes, Seconds);
  25. }
  26. public static Time operator +()
  27. {
  28. }
  29. }
  30. }

Решение задачи: «Перегрузка оператора сложения»

textual
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace GROM32RUS
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             MyTime t1 = new MyTime(02, 02, 35);
  13.             Console.WriteLine(t1);
  14.             t1.EnSeconds(40);
  15.             //t1++;
  16.             Console.WriteLine(t1);
  17.             Console.Read();
  18.         }
  19.     }
  20.     class MyTime
  21.     {
  22.         public MyTime(int H, int M, int S)
  23.         {
  24.             Hours = H;
  25.             Minutes = M;
  26.             Seconds = S;
  27.         }
  28.         public int Seconds
  29.         {
  30.             get;
  31.             private set;
  32.         }
  33.         public int Minutes
  34.         {
  35.             get;
  36.             private set;
  37.         }
  38.         public int Hours
  39.         {
  40.             get;
  41.             private set;
  42.         }
  43.         public static MyTime operator++(MyTime obj)
  44.         {
  45.             return new MyTime(obj.Hours, obj.Minutes, obj.Seconds + 1);
  46.         }
  47.         public void EnSeconds(int s)
  48.         {
  49.             Seconds += s;
  50.             if (Seconds >= 60)
  51.             {
  52.                 Seconds = Math.Abs(Seconds-60);
  53.                 Minutes++;
  54.                 if (Minutes >= 60)
  55.                 {
  56.                     Hours++;
  57.                     Minutes = 0;                  
  58.                     Seconds = Math.Abs(Seconds - 60);
  59.  
  60.                     if (Hours >= 24)
  61.                     {
  62.                         Hours = 0;
  63.                         Minutes = 0;
  64.                         Seconds = Math.Abs(Seconds - 60);
  65.                     }
  66.                 }
  67.  
  68.             }
  69.         }
  70.         public override string ToString()
  71.         {
  72.             return string.Format("{0:d2}:{1:d2}:{2:d2}", Hours, Minutes, Seconds);
  73.         }
  74.     }
  75. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

6   голосов , оценка 3.5 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы