C ++号码转换系统

使用能够执行以下任务的面向对象编程技能来编写C ++程序。

A.如何在C ++中以数字系统作为基类并使用四个派生类来表示四个数字系统(二进制,十进制,八进制,十六进制)中的每个实现类层次结构。使用类层次结构来实现数字转换计算器。

B.您的计算器应具有以下功能:

  1. 取一个数字(四个基数中的任何一个),然后将其转换为指定的基数。
  2. 要使用运算符重载对数字进行加,减,乘和除(四个基数中的任何一个)。
现在写,我只需要更正转换部分。我已经提交了下面的类代码,但仅在一个类下提交。我希望此代码使用层次继承在更多的类下进行下面只是类而不是int main()。请帮助我将其转换为继承,即基类:数字系统派生类:Binary,Decimal,Octal,HexaDecimal。

码:


class NumberSystem

{
int BinarytoDecimal()
{
    long long num;
    int decimalNum, i, rem;

    cout << "Enter any binary number: ";
    cin >> num;

    decimalNum = 0;
    i = 0;

    //converting binary to decimal
    while (num != 0)
    {
        rem = num % 10;
        num /= 10;
        decimalNum += rem * pow(2, i);
        ++i;
    }

    cout << "Equivalent Decimal number: " << decimalNum << endl;

    return decimalNum;
}


int DecimaltoOctal(int decimal)
{

    long num, temp;
    int oct[50], i = 1, j;
    cout << "Enter a decimal number : ";
    cin >> num;
    temp = num;
    while (temp != 0)
    {
        oct[i++] = temp % 8;
        temp = temp / 8;
    }
    cout << "\nOctal equivalent of " << num << " is : "<<oct[2]<<oct[1]<<endl;
    for (j = i - 1; j >= 0; j--)
        cout << oct[j];
    return 0;
}
void OctaltoHexa()
{
    int ar1[20], ar2[20], ar3[20];
    int h, i, j, k, l, x, fr, flg, rem, n1, n3;
    float rem1, n2, n4, dno;
    char octal_num[20];
    x = fr = flg = rem = 0;
    rem1 = 0.0;
    cout << "\n\n Convert any octal number to a hexadecimal number:\n";
    cout << "------------------------------------------------------\n";
    cout << " Input any octal number: ";
    cin >> octal_num;

    for (i = 0, j = 0, k = 0; i < strlen(octal_num); i++)
    {
        if (octal_num[i] == '.')
        {
            flg = 1;
        }
        else if (flg == 0)
            ar1[j++] = octal_num[i] - 48;
        else if (flg == 1)
            ar2[k++] = octal_num[i] - 48;
    }
    x = j;
    fr = k;
    for (j = 0, i = x - 1; j < x; j++, i--)
    {
        rem = rem + (ar1[j] * pow(8, i));
    }
    for (k = 0, i = 1; k < fr; k++, i++)
    {
        rem1 = rem1 + (ar2[k] / pow(8, i));
    }
    rem1 = rem + rem1;
    dno = rem1;
    n1 = (int)dno;
    n2 = dno - n1;

    i = 0;
    while (n1 != 0)
    {
        rem = n1 % 16;
        ar3[i] = rem;
        n1 = n1 / 16;
        i++;
    }
    j = 0;
    while (n2 != 0.0)
    {
        n2 = n2 * 16;
        n3 = (int)n2;
        n4 = n2 - n3;
        n2 = n4;
        ar1[j] = n3;
        j++;
        if (j == 4)
        {
            break;
        }
    }
    l = i;
    cout << " The hexadecimal value of " << octal_num << " is: ";
    for (i = l - 1; i >= 0; i--)
    {
        if (ar3[i] == 10)
            cout << "A";
        else if (ar3[i] == 11)
            cout << "B";
        else if (ar3[i] == 12)
            cout << "C";
        else if (ar3[i] == 13)
            cout << "D";
        else if (ar3[i] == 14)
            cout << "E";
        else if (ar3[i] == 15)
            cout << "F";
        else
            cout << ar3[i];
    }
    h = j;
    cout << ".";
    for (k = 0; k < h; k++)
    {
        if (ar1[k] == 10)
            cout << "A";
        else if (ar1[k] == 11)
            cout << "B";
        else if (ar1[k] == 12)
            cout << "C";
        else if (ar1[k] == 13)
            cout << "D";
        else if (ar1[k] == 14)
            cout << "E";
        else if (ar1[k] == 15)
            cout << "F";
        else
            cout << ar1[k];
    }
    cout << endl;
};


评论