Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
49
Task/Loops-Wrong-ranges/C-sharp/loops-wrong-ranges-1.cs
Normal file
49
Task/Loops-Wrong-ranges/C-sharp/loops-wrong-ranges-1.cs
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
static class Program
|
||||
{
|
||||
static void Main()
|
||||
{
|
||||
Example(-2, 2, 1, "Normal");
|
||||
Example(-2, 2, 0, "Zero increment");
|
||||
Example(-2, 2, -1, "Increments away from stop value");
|
||||
Example(-2, 2, 10, "First increment is beyond stop value");
|
||||
Example(2, -2, 1, "Start more than stop: positive increment");
|
||||
Example(2, 2, 1, "Start equal stop: positive increment");
|
||||
Example(2, 2, -1, "Start equal stop: negative increment");
|
||||
Example(2, 2, 0, "Start equal stop: zero increment");
|
||||
Example(0, 0, 0, "Start equal stop equal zero: zero increment");
|
||||
}
|
||||
|
||||
static IEnumerable<int> Range(int start, int stop, int increment)
|
||||
{
|
||||
// To replicate the (arguably more correct) behavior of VB.NET:
|
||||
//for (int i = start; increment >= 0 ? i <= stop : stop <= i; i += increment)
|
||||
|
||||
// Decompiling the IL emitted by the VB compiler (uses shifting right by 31 as the signum function and bitwise xor in place of the conditional expression):
|
||||
//for (int i = start; ((increment >> 31) ^ i) <= ((increment >> 31) ^ stop); i += increment)
|
||||
|
||||
// "Naïve" translation.
|
||||
for (int i = start; i <= stop; i += increment)
|
||||
yield return i;
|
||||
}
|
||||
|
||||
static void Example(int start, int stop, int increment, string comment)
|
||||
{
|
||||
// Add a space, pad to length 50 with hyphens, and add another space.
|
||||
Console.Write((comment + " ").PadRight(50, '-') + " ");
|
||||
|
||||
const int MAX_ITER = 9;
|
||||
|
||||
int iteration = 0;
|
||||
foreach (int i in Range(start, stop, increment))
|
||||
{
|
||||
Console.Write("{0,2} ", i);
|
||||
|
||||
if (++iteration > MAX_ITER) break;
|
||||
}
|
||||
|
||||
Console.WriteLine();
|
||||
}
|
||||
}
|
||||
38
Task/Loops-Wrong-ranges/C-sharp/loops-wrong-ranges-2.cs
Normal file
38
Task/Loops-Wrong-ranges/C-sharp/loops-wrong-ranges-2.cs
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
using System;
|
||||
|
||||
static class Program {
|
||||
struct S {
|
||||
public int Start, Stop, Incr;
|
||||
public string Comment;
|
||||
}
|
||||
|
||||
static readonly S[] examples = {
|
||||
new S{Start=-2, Stop=2, Incr=1, Comment="Normal"},
|
||||
new S{Start=-2, Stop=2, Incr=0, Comment="Zero increment"},
|
||||
new S{Start=-2, Stop=2, Incr=-1, Comment="Increments away from stop value"},
|
||||
new S{Start=-2, Stop=2, Incr=10, Comment="First increment is beyond stop value"},
|
||||
new S{Start=2, Stop=-2, Incr=1, Comment="Start more than stop: positive increment"},
|
||||
new S{Start=2, Stop=2, Incr=1, Comment="Start equal stop: positive increment"},
|
||||
new S{Start=2, Stop=2, Incr=-1, Comment="Start equal stop: negative increment"},
|
||||
new S{Start=2, Stop=2, Incr=0, Comment="Start equal stop: zero increment"},
|
||||
new S{Start=0, Stop=0, Incr=0, Comment="Start equal stop equal zero: zero increment"}
|
||||
};
|
||||
|
||||
static int Main() {
|
||||
const int limit = 10;
|
||||
bool empty;
|
||||
for (int i = 0; i < 9; ++i) {
|
||||
S s = examples[i];
|
||||
Console.Write("{0}\n", s.Comment);
|
||||
Console.Write("Range({0:d}, {1:d}, {2:d}) -> [", s.Start, s.Stop, s.Incr);
|
||||
empty = true;
|
||||
for (int j = s.Start, c = 0; j <= s.Stop && c < limit; j += s.Incr, ++c) {
|
||||
Console.Write("{0:d} ", j);
|
||||
empty = false;
|
||||
}
|
||||
if (!empty) Console.Write("\b");
|
||||
Console.Write("]\n\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue