示例#1
0
static const char *skipnum(const char *s, const char *e, readui_base base) {
	for (;s<e;s++) {
		unsigned int c = (unsigned char)*s;
		
		if (cdigit(c)) {
			if ( c-'0' >= (base & 0xFF) &&
			    !(base & READUI_ALLOWHIGHERDIGITS) )
				break;
		} else if (c>='A' && c<='Z') {
			if (!(base & READUI_ALLOWCAPLETTERS))
				break;
			if ( c-'A'+10 >= (base & 0xFF) &&
			    !(base & READUI_ALLOWHIGHERDIGITS))
				break;
		} else if (c>='a' && c<='z') {
			if (!(base & READUI_ALLOWLCASELETTERS))
				break;
			if ( c-'a'+10 >= (base & 0xFF) &&
			    !(base & READUI_ALLOWHIGHERDIGITS))
				break;
		} else
			break;
	}
	
	return s;
}
示例#2
0
int main(void)
{
	int a;
	printf("Enter an integer: ");
	scanf("%d",&a);
	printf("Number of digit 2:%d",cdigit(a,2));
	return 0;
}
示例#3
0
int cdigit(int num,int dig)
{
	if(num)
	{
		if(num%10==dig)
		{	
		    num/=10;
			return 1+cdigit(num,dig);
		}
		else{
			num/=10;
				return cdigit(num,dig);
		}
	
	}
	else
	{
		return 0;
	}
}
示例#4
0
/*
 * Scans past all the characters in a number token, fills the struct, and
 * returns one of TOK_INTEGER or TOK_FLOATING to indicate the type.
 *
 * First character must be [0-9 '.']
 */
static enum token_type scan_number(struct scan_number *sn,
					const char *s, const char *e) {
	enum token_type type;
	
	sn->dots_found = 0;
	
	sn->prefix = s;
	sn->digits = s;
	if (s+3<=e && s[0]=='0') {
		if (s[1]=='X' || s[1]=='x') {
		//hexadecimal
			s += 2;
			sn->digits = s;
			for (;s<e;s++) {
				if (*s == '.')
					sn->dots_found++;
				else if (!chex(*s))
					break;
			}
			goto done_scanning_digits;
		} else if (s[1]=='B' || s[1]=='b') {
		//binary
			s += 2;
			if (*s!='0' && *s!='1')
				s -= 2;
			sn->digits = s;
		}
	}
	
	//binary, decimal, or octal
	for (;s<e;s++) {
		if (*s == '.')
			sn->dots_found++;
		else if (!cdigit(*s))
			break;
	}

done_scanning_digits:
	
	sn->exponent = s;
	if (s<e && (
		(sn->prefix==sn->digits && (*s=='E' || *s=='e')) ||
		(sn->prefix < sn->digits && (*s=='P' || *s=='p'))
	)) {
		s++;
		if (s<e && (*s=='+' || *s=='-'))
			s++;
		while (s<e && cdigit(*s)) s++;
	}
	
	sn->suffix = s;
	while (s<e && (cdigit(*s) || cletter(*s) ||
		*s=='.' || *s=='_' || *s=='$')) s++;
	
	sn->end = s;
	
	//Now we're done scanning, but now we want to know what type this is
	type = TOK_INTEGER;
	if (sn->dots_found)
		type = TOK_FLOATING;
	if (sn->exponent < sn->suffix)
		type = TOK_FLOATING;
	
	//if this is an octal, make the leading 0 a prefix
	if (type==TOK_INTEGER && sn->prefix==sn->digits &&
			sn->digits < s && sn->digits[0]=='0')
		sn->digits++;
	
	return type;
}