Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > .NET Frameworks (точка нет фреймворки) > C# (си шарп)
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 01.01.2016, 10:10   #1
quqeiqa2
Пользователь
 
Регистрация: 16.10.2014
Сообщений: 27
По умолчанию C#: ConstNumberChecker

Код:
public static class ConstNumberChecker {
		public static bool Check(ConstNumber number) {
			char minDigit = '\0', maxDigit = '\0';
			char minDigitX = '\0', maxDigitX = '\0';
			char minDigitY = '\0', maxDigitY = '\0';
			bool moreBounds = false;
			bool dotFound = true;

			switch(number.NumeralSystem) {
			case NumeralSystem.Binary:
				minDigit = '0';
				maxDigit = '1';
				break;
			case NumeralSystem.Octal:
				minDigit = '0';
				maxDigit = '7';
				break;
			case NumeralSystem.Hexadecimal:
				minDigit = '0';
				maxDigit = '9';
				minDigitX = 'A';
				maxDigitX = 'F';
				minDigitY = 'a';
				maxDigitY = 'f';
				moreBounds = true;
				break;
			case NumeralSystem.Decimal:
				minDigit = '0';
				maxDigit = '9';
				dotFound = false;
				break;
			}

			bool digitResult = false;
			char digit = '\0';
			for(int i = 0; i < number.Value.Length; i++) {
				digit = number.Value[i];
				if(digit == '.') {
					if(dotFound) return false;
					dotFound = true;
					continue;
				}
				
				digitResult = false;
				if(digit >= minDigit && digit <= maxDigit)
					digitResult=true;

				if(moreBounds) {
					if(digit >= minDigitX && digit <= maxDigitX)
						digitResult = true;
					if(digit >= minDigitY && digit <= maxDigitY)
						digitResult = true;
				}
				if(!digitResult) return false;
			}

			return true;
		}


	}
quqeiqa2 вне форума Ответить с цитированием
Старый 01.01.2016, 10:35   #2
come-on
Участник клуба
 
Регистрация: 21.10.2015
Сообщений: 1,361
По умолчанию

Утром 01/01 и не такое бывает.
come-on вне форума Ответить с цитированием
Старый 01.01.2016, 10:38   #3
quqeiqa2
Пользователь
 
Регистрация: 16.10.2014
Сообщений: 27
По умолчанию

а что, норм))
quqeiqa2 вне форума Ответить с цитированием
Старый 01.01.2016, 10:48   #4
Stilet
Белик Виталий :)
Старожил
 
Аватар для Stilet
 
Регистрация: 23.07.2007
Сообщений: 57,097
По умолчанию

Цитата:
а что, норм))
Норм. А что это?
I'm learning to live...
Stilet вне форума Ответить с цитированием
Старый 01.01.2016, 12:40   #5
quqeiqa2
Пользователь
 
Регистрация: 16.10.2014
Сообщений: 27
По умолчанию

ура! я усовершенствовал! теперь намного быстрее!
в 1.5 раза!

Код:
	public static class ConstNumberChecker {
		 sealed class CheckRule {
			public bool[] EnabledChars;
			public bool DotIsError;

			public void AllocEnabledChars() {
				EnabledChars = new bool[256];
				for(int i = 0; i < EnabledChars.Length; i++) {
					EnabledChars[i] = false;
				}
			}

			public void EnableChars(char min, char max) {
				for(char i = min; i <= max; i++) {
					EnabledChars[(int)i] = true;
				}
			}
		}

		static CheckRule hexRule,decRule,octRule,binRule;
		static CheckRule[] rules;

		static ConstNumberChecker() {
			int[] nss = {
				(int)NumeralSystem.Binary,
				(int)NumeralSystem.Octal,
				(int)NumeralSystem.Decimal,
				(int)NumeralSystem.Hexadecimal
			};	
			int minRule=nss[0], maxRule=nss[nss.Length-1];
			for(int n = 0; n < nss.Length; n++) {
				if(nss[n] < minRule) minRule = nss[n];
				if(nss[n] > maxRule) maxRule = nss[n];
			}
			rules = new CheckRule[maxRule+1];
			rules[(int)NumeralSystem.Binary] = CreateBinaryRule();
			rules[(int)NumeralSystem.Octal] = CreateOctalRule();
			rules[(int)NumeralSystem.Decimal] = CreateDecimalRule();
			rules[(int)NumeralSystem.Hexadecimal] = CreateHexadecimalRule();
		}


		static CheckRule CreateBinaryRule() {
			return CreateRule(true, '0','1');
		}

		static CheckRule CreateOctalRule() {
			return CreateRule(true, '0','7');
		}

		static CheckRule CreateDecimalRule() {
			return CreateRule(false, '0','9');
		}

		static CheckRule CreateHexadecimalRule() {
			return CreateRule(true, '0','9','A','F','a','f');
		}

		static CheckRule CreateRule(bool dotIsError, char min,char max,
			char minX='\0',char maxX='\0', char minY='\0',char maxY='\0') {
			CheckRule rule = new CheckRule();
				rule.AllocEnabledChars();
				rule.EnableChars(min,max);
				if(minX!='\0' && maxX!='\0') rule.EnableChars(minX,maxX);
				if(minY!='\0' && maxY!='\0') rule.EnableChars(minY,maxY);
			return rule;
		}



		public static bool Check(ConstNumber number) {
			CheckRule rule = rules[(int)number.NumeralSystem];

			bool dotIsError = rule.DotIsError;
			string value = number.Value;
			int length = value.Length;
			char digit = '\0';

			for(int i = 0; i < length; i++) {
				digit = value[i];
				if(digit == '.') {
					if(dotIsError) return false;
					dotIsError = true;
					continue;
				}
				if((digit < 0 || digit > 255) || (!rule.EnabledChars[ digit ]))
					return false;
			}

			return true;
		}


	}
quqeiqa2 вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц