示例#1
0
文件: intcell.cpp 项目: WoxAlex/TaskK
std::shared_ptr<ICell> IntCell::operator +(const IntCell &in) const
{
    std::shared_ptr<IntCell> out = std::shared_ptr<IntCell>(new IntCell());
    out->setCellAccess(Computed);


    if  (overflowAdd(this->val, in.val))
        return std::shared_ptr<ErrorCell>(new ErrorCell("Add overflow"));
    out->val = this->val + in.val;
    //todo:add overflow detect and return ErrorCell
    return out;
}
示例#2
0
    // Subtraction
    signed long int overflowSubtract(const signed long int& num,
                                     const signed long int& modifier,
                                     const signed long int& minNum,
                                     const signed long int& maxNum) {

        // If they gave a negative modifier, then it is like
        // 3 - (-3) = 6. It is the same as 3 + 3 = 6, so send it to the
        // addition function
        if (modifier < 0) {
            return overflowAdd(num, abs(modifier), minNum, maxNum);

        // Check for integer overflows
        } else if (num - modifier > num
            // Check if it goes under the min number
                || num - modifier < minNum) {
            // It overflowed, so make it the min possible number that doesn't
            // overflow
            return minNum;
        } else {
            // It won't overflow, so it is safe to perform the operation
            return (num - modifier);
        }
    }