Beispiel #1
0
char * _add_str(char * a, char * b, unsigned long p, unsigned long q) {
    unsigned long len = _maxval(p,q)+1;
    char * m = malloc(len);
    int carry=0;
    unsigned long ai,bi,t;
    memset(m,0,len);
    bi = q-1;
    ai = p-1;

    for( t=len-1;t!=0;--t) {
        if( ai != -1 && bi != -1 ) {
            m[t] = _ctoi(a[ai--]) + _ctoi(b[bi--]) + carry;
        } else if( ai != -1 ) {
            m[t] = _ctoi(a[ai--]) + carry;
        } else if( bi != -1 ) {
            m[t] = _ctoi(b[bi--]) + carry;
        } else {
            break;
        }
        if( m[t] >= 10 ) {
            carry = m[t]/10;
            m[t] -= 10;
        } else {
            carry = 0;
        }
    }
    if( carry > 0 ) {
        m[0] = carry;
    }
    return _to_ascii(m, len);
}
Beispiel #2
0
dComparisonResult
dString::compare(const dString* inStr, dEStringCompareOptions options, dRange range) const {
	if (!inStr) return kCompareLessThan;
	if (dZeroRange.location == range.location && dZeroRange.length == range.length)
		range = dMakeRange(0, length());
	
	dUInteger inStrLen = inStr->length();
	TCHAR selfBuff[range.length + 1];
	TCHAR inStrBuff[inStrLen + 1];
	
	getCharacters(selfBuff, range);
	inStr->getCharacters(inStrBuff);
	
	if (kECaseInsensitiveSearch & options) {
		_upperCaseString(selfBuff);
		_upperCaseString(inStrBuff);
	}
	
	dInteger numResult;
	dUInteger i, j, il, jl;
	if (kENumericSearch & options) {
		for (i = 0, j = 0; i < range.length && j < inStrLen; ++i, ++j) {
			if (_isNumChar(selfBuff[i]) && _isNumChar(inStrBuff[j])) {
				il = range.length;
				jl = inStrLen; 
				numResult = _ctoi(&selfBuff[i], &il) - _ctoi(&inStrBuff[j], &jl);
				if (numResult < 0 ) return kCompareGreaterThan;
				else if (numResult > 0) return kCompareLessThan;
				i += il;
				j += jl;
			}
			
			if (selfBuff[i] < inStrBuff[j]) return kCompareGreaterThan;
			else if (selfBuff[i] > inStrBuff[j]) return kCompareLessThan;
		}
	} else {
		for (i = 0; i < range.length && i < inStrLen; ++i) {
			if (selfBuff[i] < inStrBuff[i]) return kCompareGreaterThan;
			else if (selfBuff[i] > inStrBuff[i]) return kCompareLessThan;
		}
	}
	
	if (range.length == inStrLen)
		return kCompareEqualTo;
	
	return i < inStrLen ? kCompareGreaterThan : kCompareLessThan;
}
Beispiel #3
0
char * _mul_str(char * a, char * b, unsigned long p, unsigned long q) {
    unsigned long len = p+q;
    char * m = malloc(len);
    int carry=0;
    unsigned long ai,bi;
    memset(m,0,len);
    for( bi = q-1; bi != -1; --bi ) {
        for( ai = p-1; ai != -1; --ai ) {
            m[ai + bi+1] += carry + _ctoi(a[ai]) * _ctoi(b[bi]);
            carry = m[ai + bi+1] / 10;
            m[ai + bi+1] %= 10;
        }
        m[bi] += carry;
        carry=0;
    }
    return _to_ascii(m, len);
}