O
O
Oleg Ostapchuk2019-05-26 19:48:49
Mathematics
Oleg Ostapchuk, 2019-05-26 19:48:49

C# What does this relation operator overload mean?

Here are the coordinates; I know, for example, Sqrt ( pow(x,2) - pow(y,2) ) gives the length of the segment, and in the considered Schildt code, the squares of the coordinates of one point are added, from which the root is then extracted ... I can not remember && will be useful everyone.

// Перегрузить операторы < и >.
using System;
// Класс для хранения трехмерных координат.
class ThreeD {
  int х, у, z; // трехмерные координаты
  public ThreeD() { х = у = z = 0; }
  public ThreeD(int i, int j, int k) { x = i; у = j; z = k; }

  // Перегрузить оператор <.
  public static bool operator <(ThreeD op1, ThreeD op2)
  {
    if(Math.Sqrt(op1.x * op1.x + op1.y * op1.y + op1.z * op1.z) <
       Math.Sqrt(op2.x * op2.x + op2.у * op2.y + op2.z * op2.z))
    return true;

    else return false;
  }

  // Перегрузить оператор >.
  public static bool operator >(ThreeD op1, ThreeD op2)
  {
    if(Math.Sqrt(op1.x * op1.x + op1.y * op1.y + op1.z * op1.z) >
       Math.Sqrt(op2.x * op2.x + op2.у * op2.у + op2.z * op2.z))
      return true;
    
    else return false;
  }

  // Вывести координаты X, Y, Z.
  public void Show()
  {
    Console.WriteLine(x + ", " + у + ", " + z);
  }
}


class ThreeDDemo {
  static void Main() {
    ThreeD a = new ThreeD(5, 6, 7);
    ThreeD b = new ThreeD(10, 10, 10);
    ThreeD с = new ThreeD(1, 2, 3);
    ThreeD d = new ThreeD(6, 7, 5);

    Console.Write("Координаты точки a: ");    a.Show();
    Console.Write("Координаты точки b: ");    b.Show();
    Console.Write("Координаты точки с: ");    c.Show();
    Console.Write("Координаты точки d: ");    d.Show();

    Console.WriteLine();//Глава 9. Перегрузка операторов 283

    if(а > с) Console.WriteLine("а > с истинно");
    if(а < с) Console.WriteLine("а < с истинно");
    if(а > b) Console.WriteLine("а > b истинно");
    if(а < b) Console.WriteLine("а < b истинно");
    if(а > d) Console.WriteLine("а > d истинно");

    else if(а < d) Console.WriteLine("a < d истинно");
      else Console.WriteLine("Точки a и d находятся на одном расстоянии " +
                   "от начала отсчета");
  }
}

Thank you :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Silin, 2019-05-26
@number_118

Check which vector is longer

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question