Beispiel #1
0
        static void Main(string[] args)
        {
            var store = new NumberStore();

            Console.WriteLine($"Original sequence: {store.ToString()}");
            int     number = 16;
            ref var value  = ref store.FindNumber(number);
Beispiel #2
0
        public static void Tests()
        {
            // <Snippet2>
            var store = new NumberStore();

            Console.WriteLine($"Original sequence: {store.ToString()}");
            int     number = 16;
            ref var value  = ref store.FindNumber(number);
 public ClassicRoulette(List <Player> players, IInputManager inputManager, IOutputManager outputManager, CancellationToken cancellationToken)
 {
     _players           = players;
     _inputManager      = inputManager;
     _outputManager     = outputManager;
     _strategyProcessor = new StrategyProcessor();
     _strategyManager   = new StrategyManager(_players);
     _cancellationToken = cancellationToken;
     _rouletteWheel     = new RouletteWheel();
     _numberStore       = new NumberStore();
 }
Beispiel #4
0
    static void Format(NumberStore ns, int precision)
    {
        precision = precision > 0 ? precision : ns.DefaultPrecision;

        int  exponent = 0;
        bool expMode  = (ns.IsDecimalSource && precision == ns.DefaultPrecision ? false : (ns.IntegerDigits > precision || ns.DecimalPointPosition <= -4));

        if (expMode)
        {
            while (!(ns.DecimalPointPosition == 1 && ns.GetChar(0) != '0'))
            {
                if (ns.DecimalPointPosition > 1)
                {
                    ns.Divide10(1);
                    exponent++;
                }
                else
                {
                    ns.Multiply10(1);
                    exponent--;
                }
            }
        }
    }
		internal static string FormatCustom(string format, NumberStore ns, NumberFormatInfo nfi)
		{
			bool p = ns.Positive;
			int offset = 0;
			int length = 0;
			CustomInfo.GetActiveSection(format, ref p, ns.ZeroOnly, ref offset, ref length);
			if (length == 0)
			{
				return ns.Positive ? String.Empty : nfi.NegativeSign;
			}
			ns.Positive = p;

			CustomInfo info = CustomInfo.Parse(format, offset, length, nfi);
#if false
			Console.WriteLine("Format : {0}",format);
			Console.WriteLine("DecimalDigits : {0}",info.DecimalDigits);
			Console.WriteLine("DecimalPointPos : {0}",info.DecimalPointPos);
			Console.WriteLine("DecimalTailSharpDigits : {0}",info.DecimalTailSharpDigits);
			Console.WriteLine("IntegerDigits : {0}",info.IntegerDigits);
			Console.WriteLine("IntegerHeadSharpDigits : {0}",info.IntegerHeadSharpDigits);
			Console.WriteLine("IntegerHeadPos : {0}",info.IntegerHeadPos);
			Console.WriteLine("UseExponent : {0}",info.UseExponent);
			Console.WriteLine("ExponentDigits : {0}",info.ExponentDigits);
			Console.WriteLine("ExponentTailSharpDigits : {0}",info.ExponentTailSharpDigits);
			Console.WriteLine("ExponentNegativeSignOnly : {0}",info.ExponentNegativeSignOnly);
			Console.WriteLine("DividePlaces : {0}",info.DividePlaces);
			Console.WriteLine("Percents : {0}",info.Percents);
			Console.WriteLine("Permilles : {0}",info.Permilles);
#endif
			StringBuilder sb_int = new StringBuilder(info.IntegerDigits * 2);
			StringBuilder sb_dec = new StringBuilder(info.DecimalDigits * 2);
			StringBuilder sb_exp = (info.UseExponent ? new StringBuilder(info.ExponentDigits * 2) : null);

			int diff = 0;
			if (info.Percents > 0)
			{
				ns.Multiply10(2 * info.Percents);
			}
			if (info.Permilles > 0)
			{
				ns.Multiply10(3 * info.Permilles);
			}
			if (info.DividePlaces > 0)
			{
				ns.Divide10(info.DividePlaces);
			}

			bool expPositive = true;
			if (info.UseExponent && (info.DecimalDigits > 0 || info.IntegerDigits > 0))
			{
				if (!ns.ZeroOnly)
				{
					while (true)
					{
						while (ns.IntegerDigits > info.IntegerDigits)
						{
							ns.Divide10(1);
							diff--;
							if (ns.IntegerDigits == 1 && ns.GetChar(0) == '0')
								break;
						}
						while (ns.IntegerDigits < info.IntegerDigits || (ns.IntegerDigits == info.IntegerDigits && ns.GetChar(0) == '0'))
						{
							ns.Multiply10(1);
							diff++;
						}

						if (!ns.RoundDecimal(info.DecimalDigits))
							break;
					}
				}

				expPositive = diff <= 0;
				NumberStore.AppendIntegerStringFromUInt32(sb_exp, (uint)(diff >= 0 ? diff : -diff));
			}
			else
			{
				ns.RoundDecimal(info.DecimalDigits);
				if (ns.ZeroOnly)
					ns.Positive = true;
			}

			if (info.IntegerDigits != 0 || !ns.CheckZeroOnlyInteger())
			{
				ns.AppendIntegerString(ns.IntegerDigits, sb_int);
			}
			/* if (sb_int.Length > info.IntegerDigits) {
				int len = 0;
				while (sb_int.Length > info.IntegerDigits && len < sb_int.Length) {
					if (sb_int [len] == '0')
						len ++;
					else
						break;
				}
				sb_int.Remove (0, len);
			} */

			ns.AppendDecimalString(ns.DecimalDigits, sb_dec);

			if (info.UseExponent)
			{
				if (info.DecimalDigits <= 0 && info.IntegerDigits <= 0)
					ns.Positive = true;

				if (sb_int.Length < info.IntegerDigits)
					sb_int.Insert(0, "0", info.IntegerDigits - sb_int.Length);

				while (sb_exp.Length < info.ExponentDigits - info.ExponentTailSharpDigits)
					sb_exp.Insert(0, '0');

				if (expPositive && !info.ExponentNegativeSignOnly)
					sb_exp.Insert(0, nfi.PositiveSign);
				else if (!expPositive)
					sb_exp.Insert(0, nfi.NegativeSign);
			}
			else
			{
				if (sb_int.Length < info.IntegerDigits - info.IntegerHeadSharpDigits)
					sb_int.Insert(0, "0", info.IntegerDigits - info.IntegerHeadSharpDigits - sb_int.Length);
				if (info.IntegerDigits == info.IntegerHeadSharpDigits && NumberStore.IsZeroOnly(sb_int))
					sb_int.Remove(0, sb_int.Length);
			}

			ZeroTrimEnd(sb_dec, true);
			while (sb_dec.Length < info.DecimalDigits - info.DecimalTailSharpDigits)
				sb_dec.Append('0');
			if (sb_dec.Length > info.DecimalDigits)
				sb_dec.Remove(info.DecimalDigits, sb_dec.Length - info.DecimalDigits);

			return info.Format(format, offset, length, nfi, ns.Positive, sb_int, sb_dec, sb_exp);
		}
		internal static string FormatExponential(NumberStore ns, int precision, NumberFormatInfo nfi, bool upper)
		{
			if (precision < 0)
				precision = 6;

			if (ns.ZeroOnly)
			{
				StringBuilder sb = new StringBuilder(precision + nfi.PositiveSign.Length + 6);
				sb.Append('0');
				if (precision > 0)
				{
					sb.Append('.');
					sb.Append('0', precision);
				}

				if (upper)
					sb.Append('E');
				else
					sb.Append('e');

				sb.Append(nfi.PositiveSign);
				sb.Append('0', 3);

				return sb.ToString();
			}

			int exponent = 0;
			while (!(ns.DecimalPointPosition == 1 && ns.GetChar(0) != '0'))
			{
				if (ns.DecimalPointPosition > 1)
				{
					ns.Divide10(1);
					exponent++;
				}
				else
				{
					ns.Multiply10(1);
					exponent--;
				}
			}

			if (ns.RoundDecimal(precision))
			{
				ns.Divide10(1);
				exponent++;
			}

			StringBuilder cb = new StringBuilder(ns.DecimalDigits + 1 + 8);

			if (!ns.Positive)
			{
				cb.Append(nfi.NegativeSign);
			}

			ns.AppendIntegerString(ns.IntegerDigits > 0 ? ns.IntegerDigits : 1, cb);

			if (precision > 0)
			{
				cb.Append(nfi.NumberDecimalSeparator);
				ns.AppendDecimalString(precision, cb);
			}

			if (upper)
				cb.Append('E');
			else
				cb.Append('e');

			if (exponent >= 0)
				cb.Append(nfi.PositiveSign);
			else
			{
				cb.Append(nfi.NegativeSign);
				exponent = -exponent;
			}

			if (exponent == 0)
			{
				cb.Append('0', 3);
			}
			else if (exponent < 10)
			{
				cb.Append('0', 2);
				cb.Append(digitLowerTable[exponent]);
			}
			else if (exponent < 100)
			{
				cb.Append('0', 1);
				cb.Append(digitLowerTable[exponent / 10 % 10]);
				cb.Append(digitLowerTable[exponent % 10]);
			}
			else if (exponent < 1000)
			{
				cb.Append(digitLowerTable[exponent / 100 % 10]);
				cb.Append(digitLowerTable[exponent / 10 % 10]);
				cb.Append(digitLowerTable[exponent % 10]);
				/*} else { // exponent range is 0...+-324
					int pos = cb.Length;
					int count = 3;
					while (exponent > 0 || --count > 0) {
						cb.Insert (pos, digitLowerTable [exponent % 10]);
						exponent /= 10;
					}*/
			}

			return cb.ToString();
		}
		internal static string FormatPercent(NumberStore ns, int precision, NumberFormatInfo nfi)
		{
			precision = (precision >= 0 ? precision : nfi.PercentDecimalDigits);
			ns.Multiply10(2);
			ns.RoundDecimal(precision);
			bool needNegativeSign = (!ns.Positive && !ns.ZeroOnly);

			StringBuilder sb = new StringBuilder(ns.IntegerDigits * 2 + precision + 16);

			if (!needNegativeSign)
			{
				if (nfi.PercentPositivePattern == 2)
				{
					sb.Append(nfi.PercentSymbol);
				}
			}
			else
			{
				switch (nfi.PercentNegativePattern)
				{
					case 0:
						sb.Append(nfi.NegativeSign);
						break;
					case 1:
						sb.Append(nfi.NegativeSign);
						break;
					case 2:
						sb.Append(nfi.NegativeSign);
						sb.Append(nfi.PercentSymbol);
						break;
				}
			}

			ns.AppendIntegerStringWithGroupSeparator(sb, nfi.PercentGroupSizes, nfi.PercentGroupSeparator);

			if (precision > 0)
			{
				sb.Append(nfi.PercentDecimalSeparator);
				ns.AppendDecimalString(precision, sb);
			}

			if (!needNegativeSign)
			{
				switch (nfi.PercentPositivePattern)
				{
					case 0:
						sb.Append(' ');
						sb.Append(nfi.PercentSymbol);
						break;
					case 1:
						sb.Append(nfi.PercentSymbol);
						break;
				}
			}
			else
			{
				switch (nfi.PercentNegativePattern)
				{
					case 0:
						sb.Append(' ');
						sb.Append(nfi.PercentSymbol);
						break;
					case 1:
						sb.Append(nfi.PercentSymbol);
						break;
				}
			}

			return sb.ToString();
		}
		internal static string FormatNumber(NumberStore ns, int precision, NumberFormatInfo nfi)
		{
			precision = (precision >= 0 ? precision : nfi.NumberDecimalDigits);
			StringBuilder sb = new StringBuilder(ns.IntegerDigits * 3 + precision);

			ns.RoundDecimal(precision);
			bool needNegativeSign = (!ns.Positive && !ns.ZeroOnly);

			if (needNegativeSign)
			{
				switch (nfi.NumberNegativePattern)
				{
					case 0:
						sb.Append('(');
						break;
					case 1:
						sb.Append(nfi.NegativeSign);
						break;
					case 2:
						sb.Append(nfi.NegativeSign);
						sb.Append(' ');
						break;
				}
			}

			ns.AppendIntegerStringWithGroupSeparator(sb, nfi.NumberGroupSizes, nfi.NumberGroupSeparator);

			if (precision > 0)
			{
				sb.Append(nfi.NumberDecimalSeparator);
				ns.AppendDecimalString(precision, sb);
			}

			if (needNegativeSign)
			{
				switch (nfi.NumberNegativePattern)
				{
					case 0:
						sb.Append(')');
						break;
					case 3:
						sb.Append(nfi.NegativeSign);
						break;
					case 4:
						sb.Append(' ');
						sb.Append(nfi.NegativeSign);
						break;
				}
			}

			return sb.ToString();
		}
		private static string FormatGeneral(NumberStore ns, int precision, NumberFormatInfo nfi, bool upper, bool roundtrip)
		{
			if (ns.ZeroOnly)
				return "0";

			precision = precision > 0 ? precision : ns.DefaultPrecision;

			int exponent = 0;
			bool expMode = (ns.IsDecimalSource && precision == ns.DefaultPrecision ? false : (ns.IntegerDigits > precision || ns.DecimalPointPosition <= -4));
			if (expMode)
			{
				while (!(ns.DecimalPointPosition == 1 && ns.GetChar(0) != '0'))
				{
					if (ns.DecimalPointPosition > 1)
					{
						ns.Divide10(1);
						exponent++;
					}
					else
					{
						ns.Multiply10(1);
						exponent--;
					}
				}
			}

			precision = precision < ns.DefaultPrecision + 2 ? (precision < ns.DefaultMaxPrecision ? precision : ns.DefaultMaxPrecision) : ns.DefaultPrecision + 2;
			StringBuilder cb = new StringBuilder(ns.IntegerDigits + precision + 16);
			if (expMode)
			{
				if (ns.RoundDecimal(precision - 1))
				{
					ns.Divide10(1);
					exponent++;
				}
			}
			else if (!roundtrip)
			{
				if (ns.IsDecimalSource)
					ns.RoundPos(precision);
				else
					ns.RoundDecimal(precision, true, false);
			}

			if (!ns.Positive)
			{
				cb.Append(nfi.NegativeSign);
			}

			ns.AppendIntegerString(ns.IntegerDigits > 0 ? ns.IntegerDigits : 1, cb);

			if (ns.DecimalDigits > 0)
			{
				cb.Append(nfi.NumberDecimalSeparator);
				ns.AppendDecimalString(ns.DecimalDigits, cb);
			}

			if (expMode)
			{
				if (upper)
					cb.Append('E');
				else
					cb.Append('e');

				if (exponent >= 0)
					cb.Append(nfi.PositiveSign);
				else
				{
					cb.Append(nfi.NegativeSign);
					exponent = -exponent;
				}

				if (exponent == 0)
				{
					cb.Append('0', 2);
				}
				else if (exponent < 10)
				{
					cb.Append('0');
					cb.Append(digitLowerTable[exponent]);
				}
				else if (exponent < 100)
				{
					cb.Append(digitLowerTable[exponent / 10 % 10]);
					cb.Append(digitLowerTable[exponent % 10]);
				}
				else if (exponent < 1000)
				{
					cb.Append(digitLowerTable[exponent / 100 % 10]);
					cb.Append(digitLowerTable[exponent / 10 % 10]);
					cb.Append(digitLowerTable[exponent % 10]);
				}
			}

			return cb.ToString();
		}
Beispiel #10
0
		internal static string FormatGeneral(NumberStore ns, IFormatProvider provider)
		{
			return FormatGeneral(ns, -1, NumberFormatInfo.GetInstance(provider), true, false);
		}
Beispiel #11
0
		internal static string FormatFixedPoint(NumberStore ns, int precision, NumberFormatInfo nfi)
		{
			precision = precision >= 0 ? precision : nfi.NumberDecimalDigits;
			ns.RoundDecimal(precision);

			StringBuilder cb = new StringBuilder(ns.IntegerDigits + precision + nfi.NumberDecimalSeparator.Length);

			if (!ns.Positive && !ns.ZeroOnly)
				cb.Append(nfi.NegativeSign);

			ns.AppendIntegerString(ns.IntegerDigits > 0 ? ns.IntegerDigits : 1, cb);

			if (precision > 0)
			{
				cb.Append(nfi.NumberDecimalSeparator);
				ns.AppendDecimalString(precision, cb);
			}

			return cb.ToString();
		}
Beispiel #12
0
		internal static string FormatDecimal(NumberStore ns, int precision, NumberFormatInfo nfi)
		{
			if (ns.IsFloatingSource || ns.IsDecimalSource)
				throw new FormatException();

			precision = precision > 0 ? precision : 1;
			precision = ns.IntegerDigits > precision ? ns.IntegerDigits : precision;

			StringBuilder sb = new StringBuilder(precision + nfi.NegativeSign.Length);

			if (!ns.Positive && !ns.CheckZeroOnlyInteger())
			{
				sb.Append(nfi.NegativeSign);
			}

			ns.AppendIntegerString(precision, sb);

			return sb.ToString();
		}
Beispiel #13
0
		internal static string FormatCurrency(NumberStore ns, int precision, NumberFormatInfo nfi)
		{
			precision = (precision >= 0 ? precision : nfi.CurrencyDecimalDigits);
			ns.RoundDecimal(precision);
			StringBuilder sb = new StringBuilder(ns.IntegerDigits * 2 + precision * 2 + 16);
			bool needNegativeSign = !ns.Positive && !ns.ZeroOnly;

			if (!needNegativeSign)
			{
				switch (nfi.CurrencyPositivePattern)
				{
					case 0:
						sb.Append(nfi.CurrencySymbol);
						break;
					case 2:
						sb.Append(nfi.CurrencySymbol);
						sb.Append(' ');
						break;
				}
			}
			else
			{
				switch (nfi.CurrencyNegativePattern)
				{
					case 0:
						sb.Append('(');
						sb.Append(nfi.CurrencySymbol);
						break;
					case 1:
						sb.Append(nfi.NegativeSign);
						sb.Append(nfi.CurrencySymbol);
						break;
					case 2:
						sb.Append(nfi.CurrencySymbol);
						sb.Append(nfi.NegativeSign);
						break;
					case 3:
						sb.Append(nfi.CurrencySymbol);
						break;
					case 4:
						sb.Append('(');
						break;
					case 5:
						sb.Append(nfi.NegativeSign);
						break;
					case 8:
						sb.Append(nfi.NegativeSign);
						break;
					case 9:
						sb.Append(nfi.NegativeSign);
						sb.Append(nfi.CurrencySymbol);
						sb.Append(' ');
						break;
					case 11:
						sb.Append(nfi.CurrencySymbol);
						sb.Append(' ');
						break;
					case 12:
						sb.Append(nfi.CurrencySymbol);
						sb.Append(' ');
						sb.Append(nfi.NegativeSign);
						break;
					case 14:
						sb.Append('(');
						sb.Append(nfi.CurrencySymbol);
						sb.Append(' ');
						break;
					case 15:
						sb.Append('(');
						break;
				}
			}

			ns.AppendIntegerStringWithGroupSeparator(sb, nfi.CurrencyGroupSizes, nfi.CurrencyGroupSeparator);

			if (precision > 0)
			{
				sb.Append(nfi.CurrencyDecimalSeparator);
				ns.AppendDecimalString(precision, sb);
			}

			if (!needNegativeSign)
			{
				switch (nfi.CurrencyPositivePattern)
				{
					case 1:
						sb.Append(nfi.CurrencySymbol);
						break;
					case 3:
						sb.Append(' ');
						sb.Append(nfi.CurrencySymbol);
						break;
				}
			}
			else
			{
				switch (nfi.CurrencyNegativePattern)
				{
					case 0:
						sb.Append(')');
						break;
					case 3:
						sb.Append(nfi.NegativeSign);
						break;
					case 4:
						sb.Append(nfi.CurrencySymbol);
						sb.Append(')');
						break;
					case 5:
						sb.Append(nfi.CurrencySymbol);
						break;
					case 6:
						sb.Append(nfi.NegativeSign);
						sb.Append(nfi.CurrencySymbol);
						break;
					case 7:
						sb.Append(nfi.CurrencySymbol);
						sb.Append(nfi.NegativeSign);
						break;
					case 8:
						sb.Append(' ');
						sb.Append(nfi.CurrencySymbol);
						break;
					case 10:
						sb.Append(' ');
						sb.Append(nfi.CurrencySymbol);
						sb.Append(nfi.NegativeSign);
						break;
					case 11:
						sb.Append(nfi.NegativeSign);
						break;
					case 13:
						sb.Append(nfi.NegativeSign);
						sb.Append(' ');
						sb.Append(nfi.CurrencySymbol);
						break;
					case 14:
						sb.Append(')');
						break;
					case 15:
						sb.Append(' ');
						sb.Append(nfi.CurrencySymbol);
						sb.Append(')');
						break;
				}
			}

			return sb.ToString();
		}
Beispiel #14
0
			public NumberStore GetClone()
			{
				NumberStore ns = new NumberStore();

				ns._decPointPos = this._decPointPos;
				ns._defMaxPrecision = this._defMaxPrecision;
				ns._defPrecision = this._defPrecision;
				ns._digits = (byte[])this._digits.Clone();
				ns._infinity = this._infinity;
				ns._NaN = this._NaN;
				ns._positive = this._positive;

				return ns;
			}
Beispiel #15
0
		public static string NumberToString(string format, NumberStore ns, NumberFormatInfo nfi, char specifier, int precision, bool custom)
		{
			if (ns.IsNaN)
			{
				return nfi.NaNSymbol;
			}
			if (ns.IsInfinity)
			{
				if (ns.Positive)
					return nfi.PositiveInfinitySymbol;
				else
					return nfi.NegativeInfinitySymbol;
			}

			if (nfi == null)
				nfi = NumberFormatInfo.GetInstance(null);

			if (custom)
			{
				if (ns.IsFloatingSource)
					ns.RoundEffectiveDigits(ns.DefaultPrecision);
				return FormatCustom(format, ns, nfi);
			}

			if (ns.IsFloatingSource)
			{
				switch (specifier)
				{
					case 'p':
					case 'P':
					case 'c':
					case 'C':
					case 'f':
					case 'F':
					case 'N':
					case 'n':
						ns.RoundEffectiveDigits(ns.DefaultPrecision);
						break;
					case 'g':
					case 'G':
						if (precision <= 0)
							ns.RoundEffectiveDigits(ns.DefaultPrecision, ns.IsBankerApplicable, true);
						else
							ns.RoundEffectiveDigits(precision);
						break;
					case 'r':
					case 'R':
						ns.RoundEffectiveDigits(ns.DefaultMaxPrecision);
						break;
					default:
						if (precision > ns.DefaultPrecision)
							ns.RoundEffectiveDigits(precision + 1);
						else
							ns.RoundEffectiveDigits(ns.DefaultPrecision + 1);
						break;
				}
			}

			switch (specifier)
			{
				case 'c':
				case 'C':
					return FormatCurrency(ns, precision, nfi);
				case 'd':
				case 'D':
					return FormatDecimal(ns, precision, nfi);
				case 'e':
				case 'E':
					return FormatExponential(ns, precision, nfi, specifier == 'E');
				case 'f':
				case 'F':
					return FormatFixedPoint(ns, precision, nfi);
				case 'g':
				case 'G':
					if (ns.IsFloatingSource || ns.IsDecimalSource || precision != -1)
						return FormatGeneral(ns, precision, nfi, specifier == 'G', false);
					return FormatDecimal(ns, precision, nfi);
				case 'n':
				case 'N':
					return FormatNumber(ns, precision, nfi);
				case 'p':
				case 'P':
					return FormatPercent(ns, precision, nfi);
				case 'r':
				case 'R':
					if (ns.IsFloatingSource)
					{
						return FormatGeneral(ns, ns.DefaultPrecision, nfi, true, true);
					}
					else
					{
						throw new FormatException("The specified format cannot be used in this instance");
					}
				default:
					throw new FormatException("The specified format '" + format + "' is invalid");
			}
		}
Beispiel #16
0
    static void f(int value)
    {
        NumberStore ns = new NumberStore(value);

        Format(ns, -1);
    }
Beispiel #17
0
		internal static string FormatGeneral(NumberStore ns)
		{
			return FormatGeneral(ns, -1, NumberFormatInfo.CurrentInfo, true, false);
		}