// check if line is rule in right shape...
bool
LSystem::isItRule( string *line, char *c, short *prob, string *right)
{
	// rule has to look like:
	// <left side>(<probability(int from 1 to 100)>)=<rightside>
	unsigned short pos= 0;
	int val = 0;
	char ch;

	// check left side
	ch= ( *line)[ pos++];
	if( ! isAlph( ch))		
		return false;
	else
		*c = ch;

	// check probability
	if( ( *line)[ pos++] != '(')
		return false;

	ch= ( *line)[ pos];
	while( isDig( ch))
	{
		val = val * 10 + ( ch - '0');
		pos++;
		ch= ( *line)[ pos];
	}
	// check if next char is ')'
	if( ch != ')')
		return false;

	// assign loaded value to prob
	*prob = val;

	// skip from '(' to '='
	pos++;
	if( ( *line)[ pos++] != '=')
		return false;

	// the rest of line is right side of rule
	// i don't check any error in it cos i don't know
	// what is an error. So copy it to right
	right->clear( );
	while( pos < line->size( ))
	{
		*right += ( *line)[ pos];
		pos++;
	}

	return true;
}
int LSystem::toInt( string s)
{
	int value= 0;
	int pos= 0;
	char c= s[ pos];

	while( isDig( c))
	{
		value = value * 10 + ( c - '0');
		pos++;
		c= s[ pos];
	}
	return value;
}
Beispiel #3
0
bool isFloat(char *&c)
{
	int state = -1;
	if (isDig(*c))
		state = 0;
	else if (*c == '-')
		state = 10;
	else
		return false;
	c++;
	while (true)
	{
		switch (state)
		{
		    case 10:
				if (isDig(*c))
				{
					c++;
					state = 0;
				}
				else
					state = -1;
				break;
/* 0 */    	case 0: 
				if (isDig(*c))
					c++;
				else if (*c == '.')
				{
					state = 1;
					c++;
				} else if (*c == 'e')
				{
					state = 3;
					c++;
				} else if (*c == 0)
					return true;
				else
					return true;
				break;
/* 1 */		case 1:
				if (*c == 0)
					state = -1;
				else if (isDig(*c))
				{
					state = 2;
					c++;
				} else
					state = -1;
				break;
/* 2 */		case 2:
				if (*c == 0)
					return true;
				else if (isDig(*c))
					c++;
				else if (*c == 'e')
				{
					state = 3;
					c++;
				} else
					return true;
				break;
/* 3 */	    case 3:
				if (*c == 0)
					state = -1;
				else if (*c == '+' || *c == '-')
				{
					state = 4;
					c++;
				} else if (isDig(*c))
				{
					state = 5;
					c++;
				} else
					state = -1;
				break;
/* 4 */	  	case 4:
				if (*c == 0)
					state = -1;
				else if (isDig(*c))
				{
					state = 5;
					c++;
				} else 
					state = -1;
				break;
/* 5 */	  	case 5:
				if (*c == 0)
					return true;
				else if (isDig(*c))
					c++;
				else 
					return true;
				break;
/* -1 */  	case -1:
				return false;
		}
	}
}