用C++编程:定义一个四进制的类,重定义“+”号实现四进制数的累加。

定义一个四进制的类,重定义“+”号实现四进制数的累加。

输入
第一行输入所需要的四进制数的个数
第二行开始,依次输入四进制数

输出
所有输入四进制数累加的和

又是作业贴啊,要是做的过程中有问题,LZ直接说遇到的问题吧

我写了个简单的,你试试好用不,没有做输入判断,你输入一定不能输4或者4以上的数
123+321+333=2103

 #include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <string>

#define HEXDEF 4

class Hex4
{
public:
    int m_Hex;
    Hex4(int iHex = 0)
    {
        m_Hex = iHex;
    };
    ~Hex4(){};
public:
    Hex4& operator = (const Hex4& hex4C)
    {
        this->m_Hex = hex4C.m_Hex;
        return *this;
    };

    Hex4& operator = (const int ihex)
    {
        this->m_Hex = ihex;
        return *this;
    };

    Hex4 operator + (const Hex4& hex4C)
    {
        Hex4 hexDes(0);
        int iCount = hex4C.m_Hex + this->m_Hex;
        char hex[32] = {0};
        _itoa_s(iCount, hex, 10);
        int iflag = 0;
        std::string strHex = hex;
        std::string strDes;
        for (int i = strHex.length() - 1; i >= 0; i--)
        {
            int icur = atoi(strHex.substr(i, 1).c_str());
            icur += iflag;
            if (icur >= HEXDEF)
            {
                icur = icur - HEXDEF;
                iflag = 1;
            }
            else
            {
                iflag = 0;
            }

            char hexT[2] = {0};
            _itoa_s(icur, hexT, 10);
            strDes.append(hexT);

            if (i == 0 && iflag == 1)
            {
                strDes.append("1");
            }
        }

        std::string strDesT(strDes.rbegin(), strDes.rend());

        hexDes.m_Hex = atoi(strDesT.c_str());

        return hexDes;
    };
};

int _tmain(int argc, _TCHAR* argv[])
{
    Hex4 hex[3];
    int b[3];
    for(int i = 0; i < 3; i++)
    {
        printf("请输入第%d四进制数:", i + 1);
        scanf_s("%d",&b[i]);
        hex[i] = b[i];
    }

    Hex4 hexSum;
    for (int i = 0; i < 3; i++)
    {
        hexSum = hexSum + hex[i];
    }

    printf("总和=%d\n", hexSum.m_Hex);
    system("pause");
    return 0;
}