我有一个程序,你们中的一些人可能已经从我过去的问题中看到了。这是关于将十进制转换为二进制和八进制,将二进制转换为十进制和八进制,以及将八进制转换为十进制和二进制。
#include<stdio.h>
void MainMenu();
int UserInput();
unsigned long Dec2BinOct(unsigned long n);
void Dec2BinOct2(unsigned long n);
unsigned long Bin2DecOct(unsigned long n);
unsigned long Bin2DecOct2(unsigned long dec);
unsigned long Oct2BinDec(unsigned long n, int base, int base2);
unsigned long main()
{
char choice;
unsigned long n;
clrscr();
do{
unsigned long bin, o, dec;
MainMenu();
printf("enter your choice: ");
scanf(" %c", &choice);
printf("\n");
switch(choice){
case 'A':
case 'a':
printf("Conversion: Decimal to binary and octal.\n");
printf("Enter number: ");
n = UserInput();
o = Dec2BinOct(n);
printf("%lu in Decimal is ", n);
Dec2BinOct2(n);
printf(" in Binary Form.\n");
printf("%lu in Decimal is %lu in Octal Form.\n\n", n, o);
break;
case 'B':
case 'b':
printf("Conversion: Binary to decimal and octal.\n");
printf("Enter number: ");
n = UserInput();
dec = Bin2DecOct(n);
o = Bin2DecOct2(dec);
printf("%lu in Binary is %lu in Decimal Form.\n", n, dec);
printf("%lu in Binary is %lu in Octal Form.\n\n", n, o);
break;
case 'c':
case 'C':
printf("Conversion: Octal to decimal and binary.\n");
printf("Enter number: ");
n = UserInput();
bin = Oct2BinDec(n, 2, 10);
dec = Oct2BinDec(n, 10, 8);
printf("%lu in Octal is %lu in Binary Form\n", n, bin);
printf("%lu in Octal is %lu in Decimal Form.\n\n", n, dec);
case 'd':
case 'D':
printf("Exit.\n\n");
break;
default:
break;
}
}while(choice != 'd'&& choice != 'D');
getch();
}
void MainMenu()
{
printf("Choices:\na. Decimal to binary and octal\nb. Binary to decimal and octal\nc. Octal to decimal and binary\nd. Exit.\n\n");
}
int UserInput(n)
{
scanf("%lu", &n);
return n;
}
unsigned long Dec2BinOct(unsigned long n)
{
unsigned long place=1, bin=0;
for(; n != 0; place *= 10)
{
bin += n % 8 * place;
n = n / 8;
}
return(bin);
}
void Dec2BinOct2(unsigned long n)
{
int x=25, y=24;
unsigned long p=n;
while(x)
{
if(n<pow(2, x))
{
if(n>=pow(2, y))
{
printf("1");
n-=pow(2, y);
}
else if(n<p)
{
printf("0");
}
}
x--;
y--;
}
}
unsigned long Bin2DecOct(unsigned long n)
{
unsigned long ans=1, dec=0;
for(; n != 0; ans *= 2)
{
dec = dec + n % 10 * ans;
n = n / 10;
}
return(dec);
}
unsigned long Bin2DecOct2(unsigned long dec)
{
unsigned long o=0, place=1;
while (dec != 0)
{
o = o + (dec % 8) * place;
dec = dec / 8;
place = place * 10;
}
return(o);
}
unsigned long Oct2BinDec(unsigned long n, int base, int base2)
{
unsigned long bin=0, place=1;
for(; n != 0; place *= base2)
{
bin += n % base * place;
n = n / base;
}
return(bin);
}但是当我选择选项字母a/A时,函数Dec2BinOct2(n)就不能正常工作了。打印不出来。
Choices:
a. Decimal to binary and octal
b. Binary to decimal and octal
c. Octal to decimal and binary
d. Exit.
enter your choice: >>>a
Conversion: Decimal to binary and octal.
Enter number: >>>123
123 in Decimal is in Binary Form.
123 in Decimal is 173 in Octal Form.但在dcoder中,它工作得很好。我很感谢你的帮助。
发布于 2021-10-25 13:12:56
我现在知道答案了。
#include<math.h>这就是为什么电源函数不能工作,也不能打印的原因。不管怎样,谢谢你们!
https://stackoverflow.com/questions/69705603
复制相似问题