Ejemplo n.º 1
0
Integer Integer::operator-(const Integer& right) const //вычитание
{
    if (sign > 0 && right.sign > 0)
        if ((*this).abs() >= right.abs()) return (*this).minus(right);
            else return -(right.minus(*this));
    else if (sign > 0 && right.sign < 0)
            return (*this) + (-right);
    else if (sign < 0 && right.sign > 0)
            return - ((-(*this)) + right);
    else
        if ((*this).abs() >= right.abs()) return -(*this).minus(right);
            else return ((-right).minus(*this));
}
Ejemplo n.º 2
0
// Returns the Least Common Multiple of (*this) and n.
Integer Integer::lcm(const Integer& n) const
{
    Integer a = n.abs();
    Integer b = this->abs();
    Integer g = this->gcd(n);
    return a / g * b;
}
Ejemplo n.º 3
0
const DivisionResult Integer::doDivide(const Integer a, const Integer b) throw (DivideByZeroException) {
	if (b.compareTo(Integer::ZERO) == 0) {
		throw DivideByZeroException();
	}	
	if (a.compareTo(Integer::ZERO) == 0) {
		return DivisionResult(Integer::ZERO, Integer::ZERO);
	}
	if (b.compareTo(Integer::ONE) == 0) {
		return DivisionResult(a, Integer::ZERO);
	}
	DivisionResult ret(Integer::ZERO, a.abs());
	int lengthDifference = ret.remainder.bytes.length() - b.abs().bytes.length();

	for (int i = lengthDifference; i >= 0; --i) { 	
		std::stringstream ss;
		ss << i;
		
		Integer base = Integer::TEN.power(ss.str());
		Integer temp = b.abs().multiply(base);
	
			while (temp.compareTo(ret.remainder) <= 0) {    
				ret.remainder = ret.remainder.subtract(temp);
				ret.result = ret.result.add(base);
			}
	}
  if (a.isPositive() != b.isPositive()) {
    ret.result = ret.result.negate();
		if (b.isPositive()) {
			ret.remainder = b.subtract(ret.remainder);
		} else {
			ret.remainder = b.add(ret.remainder);
		}
  } else {
		if (!b.isPositive()) {
			ret.remainder = ret.remainder.negate();
		}
	}
	return ret;
}
Ejemplo n.º 4
0
const Integer Integer::add(const Integer a) const {
	std::stack<int> first;
	std::stack<int> second;
  std::stack<int> val;
  std::stack<int> one;
  std::stack<int> temp;
	std::string ret;

	const int thisLength = this->bytes.length();
	const int aLength = a.bytes.length();
	const int longest = thisLength > aLength ? thisLength : aLength;

	for (int i = 0; i < longest - 1; ++i) {
		one.push(0);
	}
	one.push(1);

	if (this->positive == a.positive) {
		for (int i = longest; i > 0; --i) {
			if (i <= thisLength) {
				first.push(charToInt(this->bytes.at(thisLength - i)));
			} else {
				first.push(0);
			}
			if (i <= aLength) {
				second.push(charToInt(a.bytes.at(aLength - i)));
			} else {
				second.push(0);
			}
	  }
		val = doAdd(first, second);
		if (!val.empty() && val.top() != 1) {
			val.pop();
		}
		ret.push_back(this->positive ? '+' : '-');
	 	while (!val.empty()) {
			ret.push_back(intToChar(val.top()));
			val.pop();
		}
	} else {
		if (this->abs().compareTo(a.abs()) == 0) {
			return Integer::ZERO;
		}
		if (this->positive) {
			for (int i = longest; i > 0; --i) {
				if (i <= thisLength) {
					first.push(ninesComp(charToInt(this->bytes.at(thisLength - i))));
				} else {
					first.push(9);
				}
				if (i <= aLength) {
					second.push(charToInt(a.bytes.at(aLength - i)));
				} else {
					second.push(0);
				}
			}
		} else {
			for (int i = longest; i > 0; --i) {
				if (i <= thisLength) {
					first.push(charToInt(this->bytes.at(thisLength - i)));
				} else {
					first.push(0);
				}
				if (i <= aLength) {
					second.push(ninesComp(charToInt(a.bytes.at(aLength - i))));
				} else {
					second.push(9);
				}		
			}
		}
		val = doAdd(first, second);
		if (!val.empty()) {
			if (val.top() == 1) {
				ret.push_back('-');
				val.pop();
				while (!val.empty()) {
					temp.push(val.top());
					val.pop();
				}
				val = doAdd(temp, one);
				if (!val.empty()) {
					val.pop();
				}
				while (!val.empty()) {
					ret.push_back(intToChar(val.top()));
					val.pop();
				}
			} else {
				ret.push_back('+');
				val.pop();
				while (!val.empty()) {
					ret.push_back(intToChar(ninesComp(val.top())));
					val.pop();
				}
			}
		}
	}
  return Integer(ret);
}