【发布时间】: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]这样的东西? -
也没有递归。
-
我的想法是函数调用中的参数太少。不过只是猜测。
-
我仍然是如何将十进制转换为十六进制的新手,但我已经完成了十进制到八进制、十进制到二进制的操作,现在我很困惑。大声笑