static void Main(string[] args) { Aircraft Aircraft = new Aircraft(); Aircraft.Fuel = 100M; Aircraft boing = new Aircraft(); boing.Fuel = 150M; Console.WriteLine(Aircraft); Console.WriteLine(boing.Fuel); Console.WriteLine(Aircraft.Weight); Console.WriteLine(NumeralSystem.ToDecimal(15M, 2)); Point pt = new Point(); pt.X = 5; pt.Y = 10; pt.Z = 12; Point secondPoint = new Point(1, 2, 3); Console.WriteLine(secondPoint); List <int> someList = new List <int>(); Stack <int> someStack = new Stack <int>(); }
private List <bool> convertToBinary(int digit, NumeralSystem system) { List <bool> result = new List <bool>(); switch (system) { case NumeralSystem.Binary: result.Add(digit == 1); return(result); case NumeralSystem.Octal: result = convertDecimalToBinary(digit.ToString()); while (result.Count < 3) { result.Add(false); } return(result); case NumeralSystem.Hexadecimal: int dig = digit; if (dig > 9) { dig = dig + '0' - 'A' + 10; } result = convertDecimalToBinary(dig.ToString()); while (result.Count < 4) { result.Add(false); } return(result); } return(result); }
private static void Check(NumeralSystem system, string[] expected) { for (int i = 1; i <= expected.Length; i++) { Assert.Equal(expected[i - 1], system.ToPresentation(i)); } }
public Number(string number, NumeralSystem system, int precision = 0) { maxPrecision = precision; string integerPart = ""; double fractionalPart = 0; int i = 0; bool neg = false; if (number[0] == '-') { ++i; neg = true; } for (; i < number.Length; ++i) { if (number[i] == '.') { break; } integerPart += number[i]; } ++i; double pow = 1.0 / (int)system; for (; i < number.Length; ++i) { int c = number[i]; if (c >= 'A') { c = c - 'A' + 10; } else { c = c - '0'; } fractionalPart += c * pow; pow /= (int)system; } setInteger(integerPart, system); setFractional(fractionalPart); if (neg) { Number n2 = Number.Zero(); n2.Sub(this); Copy(n2); } negative = neg; }
public static string ToPresentation(this NumeralSystem system, int value) { switch (system) { case NumeralSystem.Arabic: return(value.ToString()); case NumeralSystem.Roman: return(ToRoman(value)); case NumeralSystem.Stars: return(new string('*', value)); default: throw new ArgumentOutOfRangeException(nameof(system)); } }
public static ulong ParseToULong(string value, NumeralSystem numeralSystem = NumeralSystem.Decimal) { if (numeralSystem == NumeralSystem.Hexadecimal) { bool sign; ulong resultHex; if (TryParseUInt64Core(value, true, out resultHex, out sign)) return resultHex; } else { ulong resultULong; if (TryParseULong(value, out resultULong)) return resultULong; } throw new Exception(); }
/// <summary> /// Converts <paramref name="number"/> to the new numeral system. /// </summary> /// <param name="number">The number.</param> /// <param name="numeralSystem">The numeral system.</param> /// <returns>String that contains the number in the new numeral system.</returns> public static string ToNewBase(int number, NumeralSystem numeralSystem) { switch (numeralSystem) { case NumeralSystem.Decimal: return(number.ToString(CultureInfo.InvariantCulture)); case NumeralSystem.Binary: return("0b" + Convert.ToString(number, 2)); case NumeralSystem.Octal: return("0" + Convert.ToString(number, 8)); case NumeralSystem.Hexidecimal: return("0x" + Convert.ToString(number, 16)); default: return(null); } }
private void setInteger(string number, NumeralSystem system) { if (system == NumeralSystem.Decimal) { digits = convertDecimalToBinary(number); } for (int i = number.Length - 1; i >= 0; --i) { char c = number[i]; List <bool> part = convertToBinary(c - '0', system); digits.AddRange(part); } if (negative) { digits.Add(true); } removeLeadingZeros(ref digits); if (digits.Count == 0) { digits.Add(false); } }
public static bool TryParseUInt(string value, out uint result, NumeralSystem numeralSystem = NumeralSystem.Decimal) { bool sign; ulong tmp; bool bresult = TryParseUInt64Core(value, numeralSystem == NumeralSystem.Hexadecimal, out tmp, out sign); result = (UInt32)tmp; return bresult && !sign; }
public RankColumnAttribute(NumeralSystem system = NumeralSystem.Arabic) : base(new RankColumn(system)) { }
/// <summary> /// Converts <paramref name="number"/> to the new numeral system. /// </summary> /// <param name="number">The number.</param> /// <param name="numeralSystem">The numeral system.</param> /// <returns>String that contains the number in the new numeral system.</returns> public static string ToNewBase(int number, NumeralSystem numeralSystem) { switch (numeralSystem) { case NumeralSystem.Decimal: return number.ToString(CultureInfo.InvariantCulture); case NumeralSystem.Binary: return "0b" + Convert.ToString(number, 2); case NumeralSystem.Octal: return "0" + Convert.ToString(number, 8); case NumeralSystem.Hexidecimal: return "0x" + Convert.ToString(number, 16); default: return null; } }
public RankColumn(NumeralSystem system) { this.system = system; }
public RankColumn(NumeralSystem system) => numeralSystem = system;
public string ConvertTo(NumeralSystem system) { if (system == NumeralSystem.Binary) { return(ToString()); } if (system == NumeralSystem.Decimal) { return(ConvertToDecimal()); } string result = ""; int partCount = system == NumeralSystem.Octal ? 3 : 4; int digit = 0; int pow = 1; for (int i = 0; i < digits.Count;) { digit = 0; pow = 1; for (int j = 0; j < partCount; ++i, ++j) { if (i < digits.Count) { digit += (digits[i] ? 1 : 0) * pow; } pow *= 2; } result = digitToChar(digit) + result; } if (fractionalDigits.Count == 0) { return(result); } result = result + "."; digit = 0; int maxPow = 1; for (int i = 1; i < partCount; ++i) { maxPow *= 2; } for (int i = 0; i < fractionalDigits.Count;) { digit = 0; pow = maxPow; for (int j = 0; j < partCount && i < fractionalDigits.Count; ++i, ++j) { digit += (fractionalDigits[i] ? 1 : 0) * pow; pow /= 2; } result = result + digitToChar(digit); } result = (negative ? "-" : "") + result; return(result); }
private static void Check(NumeralSystem system, string[] expected) { for (int i = 1; i <= expected.Length; i++) Assert.Equal(expected[i - 1], system.ToPresentation(i)); }