Описать структуру с именем NOTE - C# (181086)
Формулировка задачи:
- фамилия, имя;
- номер телефона;
дата рождения (массив из трёх чисел).
Написать программу, выполняющие следующие действия:
-ввод с клавиатуры данных в массив, состоящий из восьми элементов типа NOTE; записи должны быть размещены по алфавиту
Решение задачи: «Описать структуру с именем NOTE»
textual
Листинг программы
using System;
using System.Linq;
using System.Collections.Generic;
class Program
{
[Serializable]
public struct Note : IFormattable, IEquatable<Note>, IComparable<Note>
{
public string FullName { get; set; }
public int[] Birthday { get; set; }
public string Phone { get; set; }
public Note(string fullName) :
this(fullName, new int[] {0,0,0}, "")
{ }
public Note(string fullName, int[] birthday) :
this(fullName, birthday, "")
{ }
public Note(string fullName, int[] birthday,
string phone)
{
FullName = fullName;
Birthday = birthday;
Phone = phone;
}
public override string ToString()
{
return FullName;
}
public string ToString(string format, IFormatProvider formatProvider)
{
if (format == null)
return ToString();
switch (format.ToUpper())
{
case "FN":
return FullName;
case "B":
return String.Format("{1,2:D2}:{2,2:D2}:{3,4:D4}", Birthday); ;
case "P":
return Phone.ToString();
case "ALL":
return String.Format(" Full name: {0}, Birthday: {1,2:D2}:{2,2:D2}:{3,4:D4}, Phone: {4}"
, FullName, Birthday[0], Birthday[1], Birthday[2], Phone);
default:
throw new FormatException(String.Format(formatProvider,
"Format {0} is not supported", format));
}
}
public string ToString(string format)
{
return ToString(format, null);
}
public override int GetHashCode()
{
return (FullName.GetHashCode() + Phone.GetHashCode());
}
public bool Equals(Note other)
{
if (other == null)
return false;
if (string.Compare(this.FullName, other.FullName, StringComparison.OrdinalIgnoreCase) == 0
&&
Enumerable.SequenceEqual(this.Birthday, other.Birthday) == true
&&
string.Compare(this.Phone, other.Phone, StringComparison.OrdinalIgnoreCase) == 0)
return true;
else
return false;
}
public override bool Equals(object other)
{
if (other == null)
return false;
return this.Equals((Note)other);
}
public int CompareTo(Note other)
{
if (other == this)
return 0;
return string.Compare(this.FullName,other.FullName, StringComparison.OrdinalIgnoreCase);
}
public static bool operator ==(Note left, Note right)
{
return left.Equals(right);
}
public static bool operator !=(Note left, Note right)
{
return left != right;
}
}
public static void Main()
{
var notes = new List<Note>();
//notes.Add(new Note("Jones I.O.", new int[] { 1990, 2, 15 },"009-888-999-44"));
//notes.Add(new Note("Kimberly A.A.", new int[] {1991, 6, 4 }, "009-887-988-00"));
//notes.Add(new Note("Miller C.H.", new int[] {1990, 11, 1 }, "009-888-999-44"));
//notes.Add(new Note("Morgan B.V.", new int[] {1991, 4, 3 }, "009-088-000-55"));
//notes.Add(new Note("PhilLips P.V.", new int[] {1991, 4, 3 }, "009-888-444-77"));
//notes.Add(new Note("Lynn J.O.", new int[] {1991, 4, 3 }, "009-555-111-44"));
for (int i = 0; i < 8; i++)
{
Note note = new Note();
Console.WriteLine("Full name: ");
note.FullName = Console.ReadLine();
Console.WriteLine("Birthday: (array) ");
var arr = new int[] { 0, 0, 0 };
for (int j = 0; j < arr.Length; j++)
arr[j] = Convert.ToInt32( Console.ReadLine());
note.Birthday = arr;
Console.WriteLine("Phone: ");
note.Phone = Console.ReadLine();
notes.Add(note);
Console.WriteLine();
}
foreach (Note item in notes)
Console.WriteLine("{0:All}", item);
notes.Sort();
Console.WriteLine();
foreach (Note item in notes)
Console.WriteLine("{0:All}", item);
Console.ReadKey(true);
}
}