Перегрузка оператора сложения - C# (199545)
Формулировка задачи:
помогите пожалуйста создать перегрузку для + так чтоб секунды увеличивались на 1
класс програм
класс тайм
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication13
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Введите время");
- Console.WriteLine("Часы:");
- int Hours = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("Минуты:");
- int Minutes = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("Секунды:");
- int Seconds = Convert.ToInt32(Console.ReadLine());
- var tim = new Time(Hours, Minutes, Seconds);
- Console.ReadKey();
- }
- }
- }
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication13
- {
- class Time
- {
- public Time(int Hours, int Minutes, int Seconds)
- {
- if (Hours > 23 || Hours < 0)
- {
- Console.WriteLine("Неверный формат времени(часов)");
- }
- if (Minutes > 59 || Minutes < 0)
- {
- Console.WriteLine("Неверный формат времени(минут)");
- }
- if (Seconds > 59 || Minutes < 0)
- {
- Console.WriteLine("Неверный формат времени(секунд)");
- }
- else
- Console.WriteLine("{0}:{1}:{2}", Hours, Minutes, Seconds);
- }
- public static Time operator +()
- {
- }
- }
- }
Решение задачи: «Перегрузка оператора сложения»
textual
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace GROM32RUS
- {
- class Program
- {
- static void Main(string[] args)
- {
- MyTime t1 = new MyTime(02, 02, 35);
- Console.WriteLine(t1);
- t1.EnSeconds(40);
- //t1++;
- Console.WriteLine(t1);
- Console.Read();
- }
- }
- class MyTime
- {
- public MyTime(int H, int M, int S)
- {
- Hours = H;
- Minutes = M;
- Seconds = S;
- }
- public int Seconds
- {
- get;
- private set;
- }
- public int Minutes
- {
- get;
- private set;
- }
- public int Hours
- {
- get;
- private set;
- }
- public static MyTime operator++(MyTime obj)
- {
- return new MyTime(obj.Hours, obj.Minutes, obj.Seconds + 1);
- }
- public void EnSeconds(int s)
- {
- Seconds += s;
- if (Seconds >= 60)
- {
- Seconds = Math.Abs(Seconds-60);
- Minutes++;
- if (Minutes >= 60)
- {
- Hours++;
- Minutes = 0;
- Seconds = Math.Abs(Seconds - 60);
- if (Hours >= 24)
- {
- Hours = 0;
- Minutes = 0;
- Seconds = Math.Abs(Seconds - 60);
- }
- }
- }
- }
- public override string ToString()
- {
- return string.Format("{0:d2}:{1:d2}:{2:d2}", Hours, Minutes, Seconds);
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д