【问题标题】:Recursive(Decimal to Hexadecimal) C++递归(十进制到十六进制)C++
【发布时间】:2014-04-18 08:30:33
【问题描述】:

我有这个代码。

void hexa(int dec, int y)
{

    int deci = dec % 16;
    dec /= 16;

    if (dec % 16, ++y)
    {
        cout << "A";
    }
    else if (dec % 16 == 11)
    {
        cout << "B";
        return;
    }
    else if (dec % 16 == 12)
    {
        cout << "C";
    }
    else if (dec % 16 == 13)
    {
        cout << "D";
    }
    else if (dec % 16 == 14)
    {
        cout << "E";
    }
    else if (dec % 16 == 15)
    {
        cout << "F";
    }
    cout << deci;
}

我在int main 中将其称为hexa(dec);,并且出现“函数调用中的参数太少”的错误。有什么想法我错了吗?

编辑:这是我正在处理的全部代码。这是一个程序,可让您将十进制转换为二进制、八进制和六进制。它包括阶乘转换。如果有人有兴趣帮助或改进我的代码:

#include <iostream>
#include <Windows.h>

using namespace std;

void countdown();
void binary(int dec);
void octal(int dec);
void hexa(int dec, int y);
int a, y;

int factorial(int);

int main()
{
    int choice, choice2, dec;
    unsigned int n;
    cout << "[1] Factorial\n"
        << "[2] Coversion\n"
        << "[3] Exit\n";
    cin >> choice;
    switch(choice)
    {
    case 1:
        cout << "Enter a value: ";
        cin >> n;
        if(n <= 0)
        {
            cout << "Invalid input";
        }
        else
        {
            cout << "\nThe Factorial of " << n << " is " << factorial(n);
        }
        break;
    case 2:
        cout << "Conversion\n"
            << "[4] Decimal to Octal\n"
            << "[5] Decimal to Binary\n"
            << "[6] Decimal to Hexadecimal\n";
        cin >> choice2;
        switch(choice2)
        {
        case 4:
            cout << "Enter the Decimal number: ";
            cin >> dec;
            if(dec < 0)
            {
                cout << "You entered a negative number\n";
            }
            octal(dec);
            system("pause>0");
            break;
        case 5:
            cout << "Enter the Decimal number: ";
            cin >> dec;
            if(dec < 0)
            {
                cout << "You entered a negative number\n";
            }
            binary(dec);
            system("pause>0");
            break;
        case 6:
            cout << "Enter the Decimal Number: ";
            cin >> dec;
            if(dec < 0)
            {
                cout << "You entered a negative number\n";
            }
            hexa(dec, y);
            break;
        }
    case 3: 
        cout << "\nThank you for using this program!\n"
            << "Press any key to exit...";
        system("exit");
        break;

    }
    system("pause>0");
}

/*int factorial(int a);
{
int i = a, rslt = 0;
if (n==1)
{
return 1;
}
else
{
n + factorial(n-1);
}
}*/

int factorial(int n)
{
   if (n == 0)
      return 1;
   return n * factorial(n - 1);
}

void binary(int dec) 
{
   int deci = dec % 2;
    dec /= 2;

    if (dec > 0)
    {
        binary(dec);
    }
    else if (dec = 0)
    {
        cout << "0";
        return;
    }

    cout << deci;
}

void octal(int dec)
{
    int deci = dec % 8;
    dec /= 8;

    if (dec > 0)
    {
        binary(dec);
    }
    else if (dec = 0)
    {
        cout << "0";
        return;
    }

    cout << deci;
}

void hexa(int dec, int y)
{

    int deci = dec % 16;
    dec /= 16;

    if (dec % 16 == 10)
    {
        cout << "A";
    }
    else if (dec % 16 == 11)
    {
        cout << "B";
        return;
    }
    else if (dec % 16 == 12)
    {
        cout << "C";
    }
    else if (dec % 16 == 13)
    {
        cout << "D";
    }
    else if (dec % 16 == 14)
    {
        cout << "E";
    }
    else if (dec % 16 == 15)
    {
        cout << "F";
    }
    cout << deci;
}

【问题讨论】:

  • 我不是故意的,但这段代码看起来很疯狂。为什么不只是像"0123456789ABCDEF"[dec % 16] 这样的东西?
  • 也没有递归。
  • 我的想法是函数调用中的参数太少。不过只是猜测。
  • 我仍然是如何将十进制转换为十六进制的新手,但我已经完成了十进制到八进制、十进制到二进制的操作,现在我很困惑。大声笑

标签: c++ recursion hex decimal


【解决方案1】:

您的hexa 函数有两个参数,而您在main 中只传递dec

【讨论】:

    【解决方案2】:

    你的函数接受参数,你用一个来调用它。

    【讨论】:

      【解决方案3】:

      void hexa(int dec, int y) 需要 2 个参数,而您使用 hexa(dec) 调用并仅传递一个参数

      【讨论】:

        【解决方案4】:

        您应该重载hexa,使用一个只接受一个参数的版本(因为您在主方法中需要它)并使用正确初始化的int y 调用另一个版本。 (我不知道你想用那个 y 做什么,所以我不能告诉你一开始它应该有什么价值)

        您还需要递归调用函数才能真正进行递归。

        if (dec % 16, ++y) 也不会像您认为的那样做。 (这是一种糟糕的编码风格)

        cout &lt;&lt; deci; 之前缺少一个else,您可能想在ifs 中使用deci 而不是dec % 16 .....

        此外,您在switch 中有一个switch,这是相当邪恶的。你想要一个围绕单个 switch 语句的while(true) 循环(重复代码删除)(或者如果你需要多个菜单,则将它们放入单独的函数中)。

        cmets std::string("0123456789ABCDEF")[deci] 中的版本当然是最好的解决方案,但即使是 switch 语句

        switch (deci) { 
        case 10:
            std::cout << 'A';
            break;
        case 11:
            ...
        }
        

        会比长的 if-elseif 更好

        octal 内部,您调用的是binary(dec) 而不是octal(dec)

        【讨论】:

        • 我还是编码新手,非常感谢您的建议。
        猜你喜欢
        • 2011-12-09
        • 1970-01-01
        • 2011-06-01
        • 2018-07-26
        • 2014-11-30
        • 1970-01-01
        • 1970-01-01
        • 2014-08-24
        • 1970-01-01
        相关资源
        最近更新 更多