/// <summary> /// Convert Numeral System value into new Numeral System /// </summary> /// <param name="aimSystem">Target Numeral System instance</param> /// <returns></returns> public virtual string ConvertToAny(NumeralSystem aimSystem) { var dec = int.Parse(ConvertToDecimal());//переводим число в десятичную var intnum = 0; var targetPower = aimSystem._alphabet.Count; var remainder = 0; var result = "";// остаток intnum = Math.DivRem(dec, targetPower, out remainder); result = $"{FindByValue(remainder, aimSystem)}"; while (intnum >= targetPower) { //получаем целое число от деления intnum = Math.DivRem(intnum, targetPower, out remainder); result = $"{FindByValue(remainder, aimSystem)}" + result; } Math.DivRem(intnum, targetPower, out remainder); if (remainder != 0) { result = $"{FindByValue(intnum, aimSystem)}" + result; } return(result); }
public ResultWithResponse Convert(string val1, string ns1, string ns2) { int alphabet1; int alphabet2; if (val1 == null || ns1 == null || ns2 == null) { return(new ResultWithResponse("Неверные значения", ConverterResponse.Error)); } if (!int.TryParse(ns1, out alphabet1)) { return(new ResultWithResponse("Неверные значения", ConverterResponse.Error)); } if (!int.TryParse(ns2, out alphabet2)) { return(new ResultWithResponse("Неверные значения", ConverterResponse.Error)); } try { var quaternarySystem = new NumeralSystem(CreateAlphabet(alphabet1), val1); var aimSystem = new NumeralSystem(CreateAlphabet(alphabet2)); if (ns2 == "10") { return(new ResultWithResponse(quaternarySystem.ConvertToDecimal(), ConverterResponse.Ok)); } return(new ResultWithResponse(quaternarySystem.ConvertToAny(aimSystem), ConverterResponse.Ok)); } catch (WrongNumberException) { return(new ResultWithResponse("Число не принадлежит системе счисления", ConverterResponse.Error)); } catch (FormatException) { return(new ResultWithResponse("Неверные значения", ConverterResponse.Error)); } }
private string FindByValue(int value, NumeralSystem sys) => sys._alphabetDictionary .First(pair => pair.Value == value).Key.ToString();