Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 布尔数组到计数器的转换_C#_Arrays_Boolean_Counter - Fatal编程技术网

C# 布尔数组到计数器的转换

C# 布尔数组到计数器的转换,c#,arrays,boolean,counter,C#,Arrays,Boolean,Counter,我有一个大小为12的布尔数组。在任何时候,只有一个索引为真。阵列连接到旋钮上的12个位置,从0到11。如果我们顺时针将旋钮从位置5移动到位置6,数组中的索引5将变为假,索引6将变为真。11后,位置旋钮将移回0 我想创建一个随着旋钮顺时针或逆时针移动而增加或减少的计数器。如果位置从11变为0,计数器也将继续增加。同样,从0移动到12时,计数器将减小 以非常简单的方式,我希望布尔数组的行为类似于旋转编码器。我不会使用数组。创建具有此特定功能的对象 public class RotaryEncoder

我有一个大小为12的布尔数组。在任何时候,只有一个索引为真。阵列连接到旋钮上的12个位置,从0到11。如果我们顺时针将旋钮从位置5移动到位置6,数组中的索引5将变为假,索引6将变为真。11后,位置旋钮将移回0

我想创建一个随着旋钮顺时针或逆时针移动而增加或减少的计数器。如果位置从11变为0,计数器也将继续增加。同样,从0移动到12时,计数器将减小


以非常简单的方式,我希望布尔数组的行为类似于旋转编码器。

我不会使用数组。创建具有此特定功能的对象

public class RotaryEncoder {
     private int totalPositions:
     public int ActiveIndex = { get; private set; }

     public RotaryEncoder(int totalPositions) {
          this.totalPositions = totalPositions;
          this.ActiveIndex = 0;
     }

     public void IncreasePosition() {
         ActiveIndex = ActiveIndex + 1 == totalPositions ? 0 : ActiveIndex + 1;
     }
}
你知道如何减少位置。布尔数组本身没有作为活动索引的额外属性

另一种方法是,创建一个包含布尔值和索引的新对象集合,并同时更改活动项和新活动项。(效率肯定较低)


当然,您还必须将其封装到一个类中,因为您不希望实现细节泄露出去,让使用者随意处理您的数据结构。

我不会使用数组。创建具有此特定功能的对象

public class RotaryEncoder {
     private int totalPositions:
     public int ActiveIndex = { get; private set; }

     public RotaryEncoder(int totalPositions) {
          this.totalPositions = totalPositions;
          this.ActiveIndex = 0;
     }

     public void IncreasePosition() {
         ActiveIndex = ActiveIndex + 1 == totalPositions ? 0 : ActiveIndex + 1;
     }
}
    public static void Main(string[] args)
    {
        Console.WriteLine(GetCounter(new[] { false, false, false, true, false }));
        Console.WriteLine(GetCounter(new[] { false, false, false, false, true }));
        Console.WriteLine(GetCounter(new[] { true, false, false, false, false }));
        Console.WriteLine(GetCounter(new[] { false, true, false, false, false }));
    }

    public static int GetCounter(bool[] array)
    {
        if (array.Length == 0)
            throw new ArgumentException("Array must at least be of size 1");

        int counter = -1;
        for (var i = 0; i < array.Length; i++)
        {
            if (array[i])
            {
                if (counter != -1)
                    throw new Exception("Multiple positions are true in the array");
                counter = i;
            }
        }

        if (counter == -1)
            throw new Exception("No positions are true in the array");
        return counter;

    }
你知道如何减少位置。布尔数组本身没有作为活动索引的额外属性

另一种方法是,创建一个包含布尔值和索引的新对象集合,并同时更改活动项和新活动项。(效率肯定较低)

当然,您还必须将其封装到一个类中,因为您不希望实现细节泄露出去,让使用者随心所欲地处理您的数据结构

    public static void Main(string[] args)
    {
        Console.WriteLine(GetCounter(new[] { false, false, false, true, false }));
        Console.WriteLine(GetCounter(new[] { false, false, false, false, true }));
        Console.WriteLine(GetCounter(new[] { true, false, false, false, false }));
        Console.WriteLine(GetCounter(new[] { false, true, false, false, false }));
    }

    public static int GetCounter(bool[] array)
    {
        if (array.Length == 0)
            throw new ArgumentException("Array must at least be of size 1");

        int counter = -1;
        for (var i = 0; i < array.Length; i++)
        {
            if (array[i])
            {
                if (counter != -1)
                    throw new Exception("Multiple positions are true in the array");
                counter = i;
            }
        }

        if (counter == -1)
            throw new Exception("No positions are true in the array");
        return counter;

    }
(我在示例中使用了长度为5的数组,但它适用于任何长度大于0的数组)


(我在示例中使用了长度为5的数组,但它适用于任何长度大于0的数组)

以下是布尔数组的解决方案:

    public static void Main()
    {
        var values = new[]
        {
            false, false, false, false, false, false,
            false, false, false, false, false, false
        };
        var length = values.Length;
        var counter = 0;
        var previous = 0;
        var current = 0;

        while (counter < 25) // or any condition
        {
            current = counter++ % length;
            values[previous] = false;
            values[current] = true;
            previous = current;

            Console.WriteLine($"Count: {counter}\tIndex: {current}\t{string.Join(",\t", values)}");
        }
    }
publicstaticvoidmain()
{
var值=新[]
{
假,假,假,假,假,假,假,
假,假,假,假,假,假,假
};
变量长度=值。长度;
var计数器=0;
var-previous=0;
无功电流=0;
而(计数器<25)//或任何条件
{
当前=计数器+++%长度;
值[先前]=假;
值[当前]=真;
先前=当前;
WriteLine($“Count:{counter}\t索引:{current}\t{string.Join(“,\t”,values)}”);
}
}
以及生产的产量:
下面是布尔数组的解决方案:

    public static void Main()
    {
        var values = new[]
        {
            false, false, false, false, false, false,
            false, false, false, false, false, false
        };
        var length = values.Length;
        var counter = 0;
        var previous = 0;
        var current = 0;

        while (counter < 25) // or any condition
        {
            current = counter++ % length;
            values[previous] = false;
            values[current] = true;
            previous = current;

            Console.WriteLine($"Count: {counter}\tIndex: {current}\t{string.Join(",\t", values)}");
        }
    }
publicstaticvoidmain()
{
var值=新[]
{
假,假,假,假,假,假,假,
假,假,假,假,假,假,假
};
变量长度=值。长度;
var计数器=0;
var-previous=0;
无功电流=0;
而(计数器<25)//或任何条件
{
当前=计数器+++%长度;
值[先前]=假;
值[当前]=真;
先前=当前;
WriteLine($“Count:{counter}\t索引:{current}\t{string.Join(“,\t”,values)}”);
}
}
以及生产的产量:

您可以这样做:

public class RotaryEncoder
{
    private const int DefaultTotalPositions = 11;
    
    private readonly int _totalPositions;

    public bool[] Knobs => GetArray();
    
    public int Position { get; private set; }
    
    public RotaryEncoder() : this(DefaultTotalPositions)
    {
        
    }
    
    public RotaryEncoder(int totalPositions)
    {
        if(totalPositions <= 0) 
        {           
            totalPositions = DefaultTotalPositions; 
        }
        
        _totalPositions = totalPositions;       
    }
    
    public void Increase() => Increase(1);
    
    public void Decrease() => Decrease(1);
    
    public void Increase(int positions)
    {
        var adjustedPosition = AdjustBeforeAddOrSubtract(Position + positions);
        Position = adjustedPosition;            
    }
    
    public void Decrease(int positions)
    {
        var adjustedPosition = AdjustBeforeAddOrSubtract(Position - positions);
        Position = adjustedPosition;                
    }
    
    
    private bool[] GetArray()
    {
        var knobs = new bool[_totalPositions];
        knobs[Position] = true;         
        return knobs;
    }

    private int AdjustBeforeAddOrSubtract(int positions)
    {
        return positions > _totalPositions || positions < 0 ? 0 : positions;
    }
}

然后,您可以扩展类以满足您的需要。

您可以执行以下操作:

public class RotaryEncoder
{
    private const int DefaultTotalPositions = 11;
    
    private readonly int _totalPositions;

    public bool[] Knobs => GetArray();
    
    public int Position { get; private set; }
    
    public RotaryEncoder() : this(DefaultTotalPositions)
    {
        
    }
    
    public RotaryEncoder(int totalPositions)
    {
        if(totalPositions <= 0) 
        {           
            totalPositions = DefaultTotalPositions; 
        }
        
        _totalPositions = totalPositions;       
    }
    
    public void Increase() => Increase(1);
    
    public void Decrease() => Decrease(1);
    
    public void Increase(int positions)
    {
        var adjustedPosition = AdjustBeforeAddOrSubtract(Position + positions);
        Position = adjustedPosition;            
    }
    
    public void Decrease(int positions)
    {
        var adjustedPosition = AdjustBeforeAddOrSubtract(Position - positions);
        Position = adjustedPosition;                
    }
    
    
    private bool[] GetArray()
    {
        var knobs = new bool[_totalPositions];
        knobs[Position] = true;         
        return knobs;
    }

    private int AdjustBeforeAddOrSubtract(int positions)
    {
        return positions > _totalPositions || positions < 0 ? 0 : positions;
    }
}

然后,您可以扩展该类以满足您的需要。

您尝试了什么?您至少需要展示一些代码或想法,以便我们为您提供帮助。您尝试了什么?您需要展示至少一些代码或想法,以便我们为您提供帮助。