如何操作整数值的特定数字?
How can I manipulate specific digits of an integer value?
我必须创建一个控件,它可以操纵每个 "digit" 的整数值(从 0
到 999,999
)。
我知道如何 "Get" 整数的数字 - 只是 Mod/Div -
public class IntegerModel{
private int _value = 0;
private int _GetValue( int baseValue, int modBy, int divBy ) =>
( baseValue % modBy ) / divBy;
public int Value => _this.Value;
public One => {
get => this._GetValue( this.Value, 10, 1 );
set => Console.WriteLine( "What do I put here?" );
}
public Ten{
get => this._GetValue( this.Value, 100, 10 );
set => Console.WriteLine( "What do I put here?" );
}
}
问题是我不知道如何雄辩地设置数字值
如果我在二进制中工作,它就像使用一些按位运算符一样简单(它可能仍然是但我不知道具体怎么做)。
因此,理想情况下,如果我要使用此 class,执行以下操作,我将获得给定的输出。
IntegerModel Foo = new IntegerModel( );
Foo.One = 7;
Foo.Ten = 3;
Console.WriteLine( Foo.Value ); //Output should be 37
我需要在 One
和 Ten
属性 setter 中添加什么才能实现所需的行为?
1 将您的总体价值分为三个部分 - 高、低、相关数字。丢弃相关数字...我只是提到它来表示"high"和"low"
之间的差距
2 要得到 hi,除以 (10^position + 1),然后乘以 10^(position + 1)
3 将相关数字乘以 (10^position)
4 将其添加到低位(低位现在应该长一位数)
5 从高到低得到最终答案
我的数学很糟糕,所以希望有很多不一。不过,我很确定逻辑是正确的。
我建议对 set
和 get
使用 模块化算法 ;另一个建议是实现 indexer 以访问整数的第 n 位。
public class IntegerModel {
private int _value = 0;
private static int Power10(int value) {
return (int) Math.Pow(10, value);
}
public int Value {
get {
return _value;
}
}
//TODO: Implement ToString, Equals etc.
public int this[int index] {
get {
if (index < 0 || index > 6)
throw new ArgumentOutOfRangeException("index");
return (_value / Power10(index)) % 10;
}
set {
if (index < 0 || index > 6)
throw new ArgumentOutOfRangeException("index");
else if (value < 0 || value > 9)
throw new ArgumentOutOfRangeException("value");
_value = (index / Power10(index + 1)) * Power10(index + 1) +
value * Power10(index) +
_value % Power10(index);
}
}
}
如果您坚持 One
、Ten
等属性,您可以轻松添加它们:
public int One {
get {return this[0];} // 0th digit
set {this[0] = value;} // 0th digit
}
public int Ten {
get {return this[1];} // 1st digit
set {this[1] = value;} // 1st digit
}
public int Hundred {
get {return this[2];} // 2nd digit
set {this[2] = value;} // 2nd digit
}
测试:
IntegerModel test = new IntegerModel();
// 987:
test[0] = 7; // Last (0th digit from the end)
test[1] = 8; // Middle (1st digit from the end)
test[2] = 9; // First (2nd digit from the end)
// 987
Console.WriteLine(test.Value);
我必须创建一个控件,它可以操纵每个 "digit" 的整数值(从 0
到 999,999
)。
我知道如何 "Get" 整数的数字 - 只是 Mod/Div -
public class IntegerModel{
private int _value = 0;
private int _GetValue( int baseValue, int modBy, int divBy ) =>
( baseValue % modBy ) / divBy;
public int Value => _this.Value;
public One => {
get => this._GetValue( this.Value, 10, 1 );
set => Console.WriteLine( "What do I put here?" );
}
public Ten{
get => this._GetValue( this.Value, 100, 10 );
set => Console.WriteLine( "What do I put here?" );
}
}
问题是我不知道如何雄辩地设置数字值
如果我在二进制中工作,它就像使用一些按位运算符一样简单(它可能仍然是但我不知道具体怎么做)。
因此,理想情况下,如果我要使用此 class,执行以下操作,我将获得给定的输出。
IntegerModel Foo = new IntegerModel( );
Foo.One = 7;
Foo.Ten = 3;
Console.WriteLine( Foo.Value ); //Output should be 37
我需要在 One
和 Ten
属性 setter 中添加什么才能实现所需的行为?
1 将您的总体价值分为三个部分 - 高、低、相关数字。丢弃相关数字...我只是提到它来表示"high"和"low"
之间的差距2 要得到 hi,除以 (10^position + 1),然后乘以 10^(position + 1)
3 将相关数字乘以 (10^position)
4 将其添加到低位(低位现在应该长一位数)
5 从高到低得到最终答案
我的数学很糟糕,所以希望有很多不一。不过,我很确定逻辑是正确的。
我建议对 set
和 get
使用 模块化算法 ;另一个建议是实现 indexer 以访问整数的第 n 位。
public class IntegerModel {
private int _value = 0;
private static int Power10(int value) {
return (int) Math.Pow(10, value);
}
public int Value {
get {
return _value;
}
}
//TODO: Implement ToString, Equals etc.
public int this[int index] {
get {
if (index < 0 || index > 6)
throw new ArgumentOutOfRangeException("index");
return (_value / Power10(index)) % 10;
}
set {
if (index < 0 || index > 6)
throw new ArgumentOutOfRangeException("index");
else if (value < 0 || value > 9)
throw new ArgumentOutOfRangeException("value");
_value = (index / Power10(index + 1)) * Power10(index + 1) +
value * Power10(index) +
_value % Power10(index);
}
}
}
如果您坚持 One
、Ten
等属性,您可以轻松添加它们:
public int One {
get {return this[0];} // 0th digit
set {this[0] = value;} // 0th digit
}
public int Ten {
get {return this[1];} // 1st digit
set {this[1] = value;} // 1st digit
}
public int Hundred {
get {return this[2];} // 2nd digit
set {this[2] = value;} // 2nd digit
}
测试:
IntegerModel test = new IntegerModel();
// 987:
test[0] = 7; // Last (0th digit from the end)
test[1] = 8; // Middle (1st digit from the end)
test[2] = 9; // First (2nd digit from the end)
// 987
Console.WriteLine(test.Value);