Exemplo n.º 1
0
/**
 * Extract the numeric value from a string.
 * @param str  The string containing the value.
 * @param base The base in which the number is represented. It must be >1 and
 *             <36. In all bases, preceding whitespaces (space, tab,
 *             newline) will be ignored. For values in hex (base=16), a
 *             preceding "0x" will be ignored; however, preceding signs (-,+)
 *             also will be ignored in such case.
 * @return     The value. It will also be 0 if the string is malformed.
 */
int
AyeString::aton(char const* str, int base)
{
    int buffer;
    int start = 0;
    int length = 0;
    bool negative = false;
    bool preceding = true;
    for(size_t i = 0; i < strlen(str); i++) {
        // ignore whitespaces if preceding:
        if((str[i] == 32 || str[i] == 9 || str[i] == 10) && preceding)
            continue;

        // adjust negative flag if preceding and stop preceding:
        if((str[i] == 45 || str[i] == 43) && preceding) {
            preceding = false;
            start = i+1;
            negative = (str[i] == 45);
            continue;
        }

        /* If there's an "x" on the way, check if we're in hex and if it's the
         * second symbol after a preceding "0".
         * NOTE: So this would actually treat a sequence like "0x0x0x0x0x45" as
         *       valid. But honestly, who would do that?
         */
        if((str[i] == 120) && base == 16 && length == 1 && str[i-1] == 48) {
            length = 0;
            preceding = true;
            continue;
        }

        // get the one-byte-value and store if valid:
        buffer = cton(str[i], base);
        if(buffer == -1)
            break;

        if(preceding) {
            preceding = false;
            start = i;
        }
        length++;
    }

    // after copying, check number size (supposed to be malformed if negative):
    if(length == 0)
        return 0;

    // sum up and adjust negative flag:
    int sum = 0;
    for(int i = start+length-1; i >= start; i--)
        sum += (cton(str[i], base))*pow(base, start+length-1-i);
    return negative ? -sum : sum;
}
Exemplo n.º 2
0
int main() {
	int T;
	
	scanf("%d", &T);
	while (T--) {
		int K, N, i, len, prod, res = 0;
		scanf("%d %d", &N, &K);
		scanf(" %s ", s);
		len = strlen(s);
		for (i = 0; i < len-K; ++i) {
			prod = 1;
			switch (K) {
				default: prod = 0;
				case 7: prod *= cton(s[i+6]);
				case 6: prod *= cton(s[i+5]);
				case 5: prod *= cton(s[i+4]);
				case 4: prod *= cton(s[i+3]);
				case 3: prod *= cton(s[i+2]);
				case 2: prod *= cton(s[i+1]);
				case 1: prod *= cton(s[i]);
			}
			res = MAX(res, prod);
		}
		printf("%d\n", res);
	}

	return 0;
}
Exemplo n.º 3
0
/* copy: copy the letter and number of s
 * to cow */
void copy(Name *cow, char *s)
{
    int i;

    /* copy letters */
    strcpy(cow->name, s);
    /* make letter to number */
    for (cow->number = i = 0; s[i] != '\0'; i++) {
        if (i > 0)
            cow->number *= 10;
        cow->number += cton(s[i]);
    }
}
Exemplo n.º 4
0
void load()
{FILE *file;
file=fopen("C:/TC/Pntsav.div","rt");
fseek(file, SEEK_SET, 0);
for(p=0;p<640;p++)
{for(q=0;q<480;q++)
{fread(&r,sizeof(r),1,file);
cton();
putpixel(p,q,z);
//file<<z;
}}
fclose(file);
}