如何使用无符号short int和short int减去C++;? 我尽我所能在C++中编码一些东西。当我使用无符号int和int来减去时,没关系。然而,使用无符号短整型和短整型进行减法,我有一个问题。那么我需要在代码中做什么呢

如何使用无符号short int和short int减去C++;? 我尽我所能在C++中编码一些东西。当我使用无符号int和int来减去时,没关系。然而,使用无符号短整型和短整型进行减法,我有一个问题。那么我需要在代码中做什么呢,c++,c++11,visual-c++,c++14,subtraction,C++,C++11,Visual C++,C++14,Subtraction,非常感谢 测试值: 217586122232416725117133766166700 1758612232416725117155428849047 一开始,我必须定义 *这个>op2 当然 template< typename T > HugeInteger< T > HugeInteger< T >::operator-(const HugeInteger &op2)const // subtraction operator; HugeInteg

非常感谢

测试值:

217586122232416725117133766166700 1758612232416725117155428849047

一开始,我必须定义

*这个>op2

当然

template< typename T >
HugeInteger< T > HugeInteger< T >::operator-(const HugeInteger &op2)const // subtraction operator; HugeInteger - HugeInteger
{
    int size = integer.getSize();
    int op2Size = op2.integer.getSize();
    int differenceSize = size;
    HugeInteger < T > difference(differenceSize);

    difference = *this;

    Vector<T>::iterator it = difference.integer.begin();

    int counter = 0;

    for (Vector<T>::iterator i = difference.integer.begin(), j = op2.integer.begin(); j < op2.integer.end(); i++, j++) {
        if ((*i - *j - counter) < 10) {
            *i -= (*j + counter);
            counter = 0;
        }
        else {
            *i += 10;
            *i -= (*j + counter);
            counter = 1;
        }
    }

    while (counter == 1) {
        if ((*(it + op2Size) - counter) < 10) {
            *(it + op2Size) -= counter;
            counter = 0;
            op2Size++;
        }

        else {
            *(it + op2Size) += 10;
            *(it + op2Size) -= counter;
            counter = 1;
            op2Size++;
        }
    }

    if (*this == op2) {
        HugeInteger<T> zero(1);
        return zero;
    }

    for (Vector<T>::iterator i = difference.integer.end() - 1; i > difference.integer.begin(); i--) {
        if (*i == 0) {
            differenceSize--;
        }

        else {
            break;
        }
    }

    difference.integer.resize(differenceSize);
    return difference;
}
模板
HugeIntegerHugeInteger:运算符-(const HugeInteger&op2)常量//减法运算符;HugeInteger-HugeInteger
{
int size=integer.getSize();
int op2Size=op2.integer.getSize();
int differenceSize=大小;
HugeInteger差异(差异大小);
差异=*这;
向量::迭代器it=difference.integer.begin();
int计数器=0;
对于(向量::迭代器i=difference.integer.begin(),j=op2.integer.begin();jdifference.integer.begin();i--){
如果(*i==0){
差异化--;
}
否则{
打破
}
}
difference.integer.resize(differenceSize);
收益差;
}

您发布的代码中没有足够的信息来确定出现了什么问题。我怀疑这与“short”类型如何被转换成
HugeInt
有关

下面,我展示了一个类似于
HugeInt
的类定义。它跟踪值的符号,但不幸的是,它只定义了几个成员,足以演示减法

这个类有一个nutty模板转换构造函数。如果其他构造函数都不能处理某个类型,它将尝试以与大小无关的方式转换该类型的值,就像它们是某种整数值一样

main()
有两个减法运算示例,都涉及短值和“大”值

#包括
#包括
#包括
//基数-10任意大小的整数类
模板
HugInt类{
//整数存储在向量容器中
//该示例使用派生的类向量(静态)
//from std::vector以模拟来自
//问题中的代码。通常,我只会使用std::vector。
类向量:public std::Vector{//virtual从不需要
公众:
int getSize()常量{
返回静态_cast(std::vector::size());
}   };
//二元变量
向量整数;
整数符号;
//对于封装性和紧凑性,这个类使用了很多
//如果考虑使用自己的函数
//内联好友,必须始终称为至少传递一个
//对象,该对象来自定义函数的类。
//否则,编译器将无法找到它。
//仅比较两个Hugint的幅值(无符号)部分
//返回a>b:1,a==b:0,相对湿度大小?1:-1;
对于(整数i=lh_大小-1;i+1;--i){
if(左整数[i]!=右整数[i]){
返回左整数[i]>右整数[i]?1:-1;
}   }
返回0;
}
//从*的幅值中减去op2的幅值
//不考虑符号,但
//如果op2的幅值大于*此,则翻转*此符号
无效次重力(常量HugInt和op2){
常量int cm=cmpMagnitude(*本,op2);
如果(cm==0){
//大小相等,所以结果为零
integer.clear();
符号=0;
返回;
}
如果(厘米<0){
//如果op2的幅值大于此值
//从op2中减去这个量级,
//然后将其设置为否定结果
HugInt temp{op2};
温度次重力(*本);
整数=临时整数;
符号=-符号;
返回;
}
//执行数字幅度减法
//在这里,这个量值总是大于或等于
//等于op2的
T=0;
const int min_size=op2.integer.getSize();
int i;
对于(i=0;i
#include <iostream>
#include <vector>
#include <string>

// radix-10 arbitrary size integer class
template<typename T>
class HugInt {
    // integer digits are stored in a Vector container
    // The example uses the class Vector, derived (statically)
    // from std::vector<T> in order to mimic the class from
    // code in the Question.  Normally, I would just use a std::vector.
    class Vector : public std::vector<T> { // virtual never needed
    public:
        int getSize() const {
            return static_cast<int>(std::vector<T>::size());
    }   };
    // two member variables
    Vector integer;
    int    sign;

    // For encapsulation and compactness, this class uses a lot
    // of inline-friend functions.  If considering using your own
    // inline-friends, it must always be called passing at least one 
    // object from the class where the function is defined.
    // Otherwise, the compiler cannot find it.

    // compares just the magnitude (unsigned) parts of two HugInts
    // returns a>b:1, a==b:0, a<b:-1
    friend int cmpMagnitude(const HugInt& lh, const HugInt& rh) {
        const int lh_size = lh.integer.getSize();
        const int rh_size = rh.integer.getSize();
        if(lh_size != rh_size) return lh_size > rh_size ? 1 : -1;
        for(int i=lh_size-1; i+1; --i) {
            if(lh.integer[i] != rh.integer[i]) {
                return lh.integer[i] > rh.integer[i] ? 1 : -1;
        }   }
        return 0;
    }
    // subtract the magnitude of op2 from the magnitude of *this
    // does not take signs into account, but
    // flips sign of *this if op2 has a greater magnitude than *this
    void subMagnitude(const HugInt& op2) {
        const int cm = cmpMagnitude(*this, op2);
        if(cm == 0) {
            // magnitudes are equal, so result is zero
            integer.clear();
            sign = 0;
            return;
        }
        if(cm < 0) {
            // If op2's magnitude is greater than this's
            // subtract this's Magnitude from op2's,
            // then set this to the negated result
            HugInt temp{op2};
            temp.subMagnitude(*this);
            integer = temp.integer;
            sign = -sign;
            return;
        }
        // perform digit-wise Magnitude subtraction
        // here, this's Magnitude is always greater or
        // equal to op2's
        T borrow = 0;
        const int min_size = op2.integer.getSize();
        int i;
        for(i=0; i<min_size; ++i) {
            const T s = op2.integer[i] + borrow;
            if(borrow = (integer[i] < s)) {
                integer[i] += T(10);
            }
            integer[i] -= s;
        }
        // propagate borrow to upper words (beyond op2's size)
        // i is guaranteed to stay in bounds
        while(borrow) {
            if(borrow = (integer[i] < 1)) {
                integer[i] += T(10);
            }
            --integer[i++];
        }
        // remove zeroes at end until a nonzero
        // digit is encountered or the vector is empty
        while(!integer.empty() && !integer.back()) {
            integer.pop_back();
        }
        // if the vector is empty after removing zeroes,
        // the object's value is 0, fixup the sign
        if(integer.empty()) sign = 0;
    }

    void addMagnitude(const HugInt& op2) {
        std::cout << "addMagnitude called but not implemented\n";
    }
    // get_sign generically gets a value's sign as an int
    template <typename D>
    static int get_sign(const D& x) { return int(x > 0) - (x < 0); }
public:
    HugInt() : sign(0) {}  // Default ctor
    // Conversion ctor for template type
    // Handles converting from any type not handled elsewhere
    // Assumes D is some kind of integer type
    // To be more correct, narrow the set of types this overload will handle,
    // either by replacing with overloads for specific types,
    // or use <type_traits> to restrict it to integer types.
    template <typename D>
    HugInt(D src) : sign(get_sign(src)) {
        // if src is negative, make absolute value to store magnitude in Vector
        if(sign < 0) src = D(0)-src; // subtract from zero prevents warning with unsigned D
        // loop gets digits from least- to most-significant 
        // Repeat until src is zero: get the low digit, with src (mod 10)
        // then divide src by 10 to shift all digits down one place.
        while(src >= 1) {
            integer.push_back(T(src % D(10)));
            src /= D(10);
    }   }
    // converting floating-point values will cause an error if used with the integer modulo
    // operator (%). New overloads could use fmod. But for a shorter example, do something cheesy:
    HugInt(double src) : HugInt(static_cast<long long>(src)) {}
    HugInt(float src)  : HugInt(static_cast<long long>(src)) {}
    // conversion ctor from std::string
    HugInt(const std::string& str) : sign(1) {
        for(auto c : str) {
            switch(c) {
                // for simple, short parsing logic, a '-'
                // found anywhere in the parsed value will
                // negate the sign.  If the '-' count is even,
                // the result will be positive.
                case '-': sign = -sign; break;
                case '+': case ' ': case '\t': break; // '+', space and tab ignored
                case '0': if(integer.empty()) break;  // ignore leading zeroes or fallthrough
                case '1': case '2': case '3': case '4': case '5':
                case '6': case '7': case '8': case '9':
                    integer.insert(integer.begin(), T(c - '0'));
                    break;  
        }   }
        if(integer.empty()) sign = 0; // fix up zero value if no digits between '1' and '9' found
    }
    // conversion ctor from C-String (dispatches to std::string ctor)
    HugInt(const char* str) : HugInt(std::string(str)) {}

    // The default behavior, using value semantics to copy the
    // two data members, is correct for our copy/move ctors/assigns
    HugInt(const HugInt& src)              = default;
    HugInt& operator = (const HugInt& src) = default;
    // some "C++11" compilers refuse to default the moves
    // HugInt(HugInt&& src)                   = default;
    // HugInt& operator = (HugInt&& src)      = default;

    // cmp(a, b) returns 1 if a>b, 0 if a==b or -1 if a<b
    friend int cmp(const HugInt& lh, const HugInt& rh) {
        if(lh.sign != rh.sign) return lh.sign > rh.sign ? 1 : -1;
        const int cmpmag = cmpMagnitude(lh, rh);
        return lh.sign < 0 ? -cmpmag : cmpmag;
    }
    friend bool operator == (const HugInt& lh, const HugInt& rh) {
        return cmp(lh, rh) == 0;
    }
    friend bool operator != (const HugInt& lh, const HugInt& rh) {
        return cmp(lh, rh) != 0;
    }
    friend bool operator > (const HugInt& lh, const HugInt& rh) {
        return cmp(lh, rh) == 1;
    }
    friend bool operator < (const HugInt& lh, const HugInt& rh) {
        return cmp(lh, rh) == -1;
    }
    friend bool operator >= (const HugInt& lh, const HugInt& rh) {
        return cmp(lh, rh) != -1;
    }
    friend bool operator <= (const HugInt& lh, const HugInt& rh) {
        return cmp(lh, rh) != 1;
    }
    // negate operator
    HugInt operator - () const {
        HugInt neg;
        neg.integer = integer;
        neg.sign = -sign;
        return neg;
    }
    // subtract-assign operator
    HugInt& operator -= (const HugInt &op2) {
        if(op2.sign == 0) { // op2 is zero, do nothing
            return *this;
        }
        if(sign == 0) { // *this is zero, set *this to negative op2
            integer = op2.integer;
            sign = -op2.sign;
            return *this;
        }
        if(sign == op2.sign) { // same signs: use magnitude subtratction
            subMagnitude(op2);
            return *this;
        }
        // opposite signs here: needs magnitude addition (but not implemented)
        addMagnitude(op2);
        return *this;
    }

    friend HugInt operator - (const HugInt& lh, const HugInt& rh) {
        // a - b uses the -= operator to avoid duplicate code
        HugInt result{lh};
        return result -= rh;
    }

    // overload stream output operator for HugInt values
    friend std::ostream& operator << (std::ostream& os, const HugInt& hi) {
        // assumes decimal output and class radix is 10
        if(hi.integer.getSize() == 0) return os << '0';
        if(hi.sign < 0) os << '-';
        for(int i=hi.integer.getSize()-1; i+1; --i) {
            os << char('0' + int(hi.integer[i]));
        }
        return os;
    }
};

int main() {
    using HugeInt = HugInt<char>;
    {
        HugeInt a = "21758612232416725117133766166700 1758612232416725117155428849047";
        unsigned short b = 55427;
        HugeInt c = a - b;
        std::cout << a << " - " << b << " = \n" << c << '\n';
    }
        std::cout << '\n';
    {
        short a = 6521;
        HugeInt b = 1234567;
        HugeInt c = a - b;
        std::cout << a << " - " << b << " = \n" << c << '\n';
    }
}