0

以下の単純なコード ブロックを考えると、これを C# でコーディングするためのより良い方法があるかどうか疑問に思っていました。

        int lowIndex = 0;
        int highIndex = 1;
        if (end[0].X.ConvertToMillimetres() == end[1].X.ConvertToMillimetres())
        {
            if (end[0].Y.ConvertToMillimetres() > end[1].Y.ConvertToMillimetres())
            {
                lowIndex = 1;
                highIndex = 0;
            }
        }
        else
        {
            if (end[0].X.ConvertToMillimetres() > end[1].X.ConvertToMillimetres())
            {
                lowIndex = 1;
                highIndex = 0;
            }
        }
4

7 に答える 7

4

のようなものはどうですか

int lowIndex = 0; 
int highIndex = 1; 
if ((end[0].X.ConvertToMillimetres() == end[1].X.ConvertToMillimetres() && end[0].Y.ConvertToMillimetres() > end[1].Y.ConvertToMillimetres()) ||
    (end[0].X.ConvertToMillimetres() > end[1].X.ConvertToMillimetres()))
{ 
    lowIndex = 1; 
    highIndex = 0; 
} 
于 2012-09-12T05:33:02.150 に答える
1

多分このようなもの:

int X0mm = end[0].X.ConvertToMillimetres();
int X1mm = end[1].X.ConvertToMillimetres();
int Y0mm = end[0].Y.ConvertToMillimetres();
int Y1mm = end[1].Y.ConvertToMillimetres();

int lowIndex = (X0mm == X1mm && Y0mm > Y1mm) || (X0mm > X1mm) ? 1 : 0;
int highIndex = lowIndex == 1 ? 0 :1;
于 2012-09-12T05:34:39.233 に答える
1

あなたがやろうとしているのは、2本の線を2回セットすることを排除することだと思いlowIndexますhighIndexIFこのようにステートメントを組み合わせることができます。

int lowIndex = 0;
int highIndex = 1;
if ( (end[0].X.ConvertToMillimetres() == end[1].X.ConvertToMillimetres() &&
      end[0].Y.ConvertToMillimetres() > end[1].Y.ConvertToMillimetres()) ||
      end[0].X.ConvertToMillimetres() > end[1].X.ConvertToMillimetres() )
{
    lowIndex = 1;
    highIndex = 0;
}
于 2012-09-12T05:36:55.683 に答える
1

そうです:

int lowIndex = 0;
int highIndex = 1;
if (   end[0].X.ConvertToMillimetres() == end[1].X.ConvertToMillimetres() 
    && end[0].Y.ConvertToMillimetres() >  end[1].Y.ConvertToMillimetres()  
    || end[0].X.ConvertToMillimetres() != end[1].X.ConvertToMillimetres()
    && end[0].X.ConvertToMillimetres() >  end[1].X.ConvertToMillimetres())
{
    lowIndex = 1;
    highIndex = 0;
}

end[0].X.ConvertToMillimetres() != end[1].X.ConvertToMillimetres() && end[0].X.ConvertToMillimetres() > end[1].X.ConvertToMillimetres()は常にと同等end[0].X.ConvertToMillimetres() > end[1].X.ConvertToMillimetres()であるため、次のようになります。

int lowIndex = 0;
int highIndex = 1;
if (   end[0].X.ConvertToMillimetres() == end[1].X.ConvertToMillimetres() 
    && end[0].Y.ConvertToMillimetres() >  end[1].Y.ConvertToMillimetres()  
    || end[0].X.ConvertToMillimetres() >  end[1].X.ConvertToMillimetres())
{
    lowIndex = 1;
    highIndex = 0;
}

最後に、ConvertToMillimetresの結果がどのようなものか、それがどれほど複雑かはわかりません/ ConvertToMillimetresが、計算を減らすためにこれらのメソッドの値をキャプチャするためにいくつかのローカル変数を使用するのに時間がかかる場合は、理にかなっているかもしれません。少し時間を節約するためにローカルスコープを汚染する価値はないかもしれません。おそらく、それはかなり些細な機能なので、あまり有利ではありません。(ただし、クリシュナが言ったように、end[0]とend1ローカル変数としてより適切に機能する可能性があります。または、 end1.Xとend1.Yなどです。ただし、そうすると、結果が保存される可能性があります。)

//capture values

var end0Xm = end[0].X.ConvertToMillimetres();
var end1Xm = end[1].X.ConvertToMillimetres();
var end0Ym = end[0].Y.ConvertToMillimetres();
var end1Ym = end[1].Y.ConvertToMillimetres();

//define proper lowIndex, highIndex
int lowIndex = 0;
int highIndex = 1;
if (   end0Xm  == end1Xm 
    && end0Ym  >  end1Ym  
    || end0Xm  >  end1Xm )
{
    lowIndex = 1;
    highIndex = 0;
}

テストの結果を将来の使用のために保存しておくと便利な場合があります。これにより、ifブロックが排除され、将来誰かが混乱する可能性が低くなります。ただし、それでも条件付きで何かを行う必要があります。この次のコードブロックは、 C#の三項演算子の存在と理解があることを前提としています。

var end0Xm = end[0].X.ConvertToMillimetres();
var end1Xm = end[1].X.ConvertToMillimetres();
var end0Ym = end[0].Y.ConvertToMillimetres();
var end1Ym = end[1].Y.ConvertToMillimetres();

//define proper lowIndex, highIndex
bool testCase = (end0Xm  == end1Xm 
    && end0Ym  >  end1Ym  
    || end0Xm  >  end1Xm);

int lowIndex = testCase? 1 : 0;
int highIndex = testCase? 0 : 1; 

または多分あなたは好むhighIndex = !testcase? 1: 0、あるいはhighIndex = 1 - lowIndex

Etceteraなど。

于 2012-09-12T05:53:28.037 に答える
1

私はコンパクトなコードよりも読みやすさを好みます! :) コードに最適なように変数の名前を変更します...

int xComparison = end[0].X.ConvertToMillimetres().CompareTo(end[1].X.ConvertToMillimetres());
int yComparison = end[0].Y.ConvertToMillimetres().CompareTo(end[1].Y.ConvertToMillimetres());

bool isMatch = ((xComparison == 0 && yComparison > 0) || xComparison > 0);

int lowIndex = (isMatch ? 1 : 0);
int highIndex = (isMatch ? 0 : 1);
于 2012-09-12T08:13:45.707 に答える
0

私はメソッドを実行します:(コードは保守可能)

private void GetValueM(List<EndType> end,out int  lowIndex,out int  highIndex)
    {
         lowIndex = 0;
         highIndex = 1;

         if ((end != null) && (end.Count > 2))
         {
             var x0 = end[0].X;
             var x1 = end[1].X;
             var y0 = end[0].Y;
             var y1 = end[1].Y;

             if (x0 != null && x1 != null && y0 != null && y1 != null)
             {
                 if ((x0.ConvertToMillimetres() == x1.ConvertToMillimetres() && y0.ConvertToMillimetres() > y1.ConvertToMillimetres()) ||
                     (x0.ConvertToMillimetres() > x1.ConvertToMillimetres()))
                 {
                     lowIndex = 1;
                     highIndex = 0;
                 }

             }
             else
             {
                 //Any is null  set your value or throw exception
             }
         }


    }
于 2012-09-12T06:02:28.970 に答える
0

これが C# 4.0 固有のものだとは思わないでください。

var endOne = end[0];
var endTwo = end[1];

//now, if you would override the == operator for the type of X and Y to compare using ConvertToMillimetres(), you can have something like:

int lowIndex = (endOne.X == endTwo.X && endOne.Y > endTwo.Y) || (endOne.X > endTwo.X) ? 1 : 0;
int highIndex = lowIndex == 1 ? 0 : 1;
于 2012-09-12T05:39:04.903 に答える