递归(十进制到十六进制)C ++

时间:2014-03-13 14:19:21

标签: c++ recursion hex decimal

我有这段代码。

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;
}

4 个答案:

答案 0 :(得分:2)

hexa函数有两个参数,而dec内只传递main

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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

你还需要递归调用函数来实际进行递归。

if (dec % 16, ++y)也不会做你认为它做的事情。 (这是一种可怕的编码风格)

else之前缺少cout << deci;,你可能想在deci s中使用if而不是dec % 16 ..... < / p>

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

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

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

会比长if-elseif

好得多

octal内,您正在调用binary(dec)而不是octal(dec)

相关问题