标签:ice result 自己的 避免 选择 void 怎么 逻辑 number
//2进制转10进制
void two_ten(int a)
{
int i, s = 0;
int result = 0;
for (i = 1; a != 0; i *= 2)
{
if (a % 10 > 1)
{
s = 1;break;
}
else
{
result += (a % 10) * i;
a /= 10;
}
}
if (s == 1)
printf("Error!\n");
else
printf("\n二进制转换十进制为:%d\n", result);
}
//2进制转8进制
void two_eight(int a)
{
int i, j, k, s = 0;
int p[30];
int result = 0;
for (i = 1; a != 0; i *= 2)
{
if (a % 10 > 1)
{
s = 1;break;
}
else
{
result += (a % 10) * i;
a /= 10;
}
}
for (j = 0; result != 0; j++)
{
p[j] = result % 8;
result /= 8;
}
if (s == 1)
printf("Error!\n");
else
{
printf("\n二进制转换八进制为:");
for (k = j - 1; k >= 0; k--)
printf("%d", p[k]);
printf("\n");
}
}
//2进制转16进制
void two_sixteen(int a)
{
int i, j, k, s = 0;
char p[30];
int result = 0;
for (i = 1; a != 0; i *= 2)
{
if (a % 10 > 1)
{
s = 1;break;
}
else
{
result += (a % 10) * i;
a /= 10;
}
}
for (j = 0; result != 0; j++)
{
p[j] = result % 16;
result /= 16;
if (p[j] >= 10)
{
switch (p[j])
{
case 10: p[j] = 'A';break;
case 11: p[j] = 'B';break;
case 12: p[j] = 'C';break;
case 13: p[j] = 'D';break;
case 14: p[j] = 'E';break;
case 15: p[j] = 'F';break;
}
}
else
p[j] += 48;
}
if (s == 1)
printf("Error!\n");
else
{
printf("\n二进制转换十六进制为:");
for (k = j - 1; k >= 0; k--)
printf("%c", p[k]);
printf("\n");
}
}
void eight_two(int a)
{
int i, j, k, s = 0;
int result = 0;
int p[30];
for (i = 1; a != 0; i *= 8)
{
if (a % 10 > 7)
{
s = 1;
break;
}
else
{
result += (a % 10) * i;
a /= 10;
}
}
for (j = 0; result != 0; j++)
{
p[j] = result % 2;
result /= 2;
}
if (s == 1)
printf("Error!\n");
else
{
printf("\n八进制转换二进制为:");
for (k = j - 1; k >= 0; k--)
printf("%d", p[k]);
printf("\n");
}
}
//8进制转10进制
void eight_ten(int a)
{
int i, s = 0;
int result = 0;
for (i = 1; a != 0; i *= 8)
{
if (a % 10 > 7)
{
s = 1;
break;
}
else
{
result += (a % 10) * i;
a /= 10;
}
}
if (s == 1)
printf("Error!\n");
else
{
printf("\n八进制转换十进制为:%d\n", result);
}
}
//8进制转16进制
void eight_sixteen(int a)
{
int i, j, k, s = 0;
char p[30];
int result = 0;
for (i = 1; a != 0; i *= 8)
{
if (a % 10 > 7)
{
s = 1;
break;
}
else
{
result += (a % 10) * i;
a /= 10;
}
}
for (j = 0; result != 0; j++)
{
p[j] = result % 16;
result = result / 16;
if (p[j] < 10)
p[j] += 48;
else
{
switch (p[j])
{
case 10: p[j] = 'A';
break;
case 11: p[j] = 'B';
break;
case 12: p[j] = 'C';
break;
case 13: p[j] = 'D';
break;
case 14: p[j] = 'E';
break;
case 15: p[j] = 'F';
break;
}
}
}
if (s == 1)
printf("Error!\n");
else
{
printf("\n八进制转换十六进制为:");
for (k = j - 1; k >= 0; k--)
printf("%c", p[k]);
printf("\n");
}
}
void ten_two(int a)//十进制转换为二进制
{
int j, k;
int p[30];
for (j = 0; a != 0; j++)
{
p[j] = a % 2;
a /= 2;
}
printf("\n十进制转换二进制为:");
for (k = j - 1; k >= 0; k--)
printf("%d", p[k]);
printf("\n");
}
void ten_eight(int a)//十进制转换为八进制:
{
int j, k;
int p[30];
for (j = 0; a != 0; j++)
{
p[j] = a % 8;
a /= 8;
}
printf("\n十进制转换八进制为:");
for (k = j - 1; k >= 0; k--)
printf("%d", p[k]);
printf("\n");
}
void ten_sixteen(int a)//十进制转换为十六进制:
{
int j, k;
int p[30];
for (j = 0; a != 0; j++)
{
p[j] = a % 16;
a /= 16;
if (p[j] < 10)
p[j] += 48;
else
{
switch (p[j])
{
case 10: p[j] = 'A'; break;
case 11: p[j] = 'B'; break;
case 12: p[j] = 'C'; break;
case 13: p[j] = 'D'; break;
case 14: p[j] = 'E'; break;
case 15: p[j] = 'F'; break;
}
}
}
printf("\n十进制转换十六进制为:");
for (k = j - 1; k >= 0; k--)
printf("%c", p[k]);
printf("\n");
}
int main()
{
int n = 0;
int l = 1,a;
while (l)
{
printf("请选择:");
scanf_s("%d", &n);
printf("请输入你的数:\n");
scanf_s("%d", &a);
switch (n)
{
case 1:two_ten(a);break; two_eight;
case 2:two_eight(a);break;
case 3:two_sixteen(a);break;
case 4:eight_two(a);break;
case 5:eight_ten(a);break;
case 6:eight_sixteen(a);break;
case 7:sixteen_ten(); break;
case 8:sixteen_two(); break;
case 9:sixteen_eight(); break;
case 10:ten_two(a); break;
case 11:ten_eight(a); break;
case 12:ten_sixteen(a); break;
default:
printf("Error!!!\n");break;
}
}
return 0;
}
我太难了我,经历了九九八十一难:
int One(int choice, int number)
{
int sum = 0, count = 0;
int value, j;
int b;
value = number;
do
{
value /= 10;
count++;
} while (value > 0);
value = number;
for (j = 0;j < count; j++)
{
b = value % 10;
sum = sum + b * pow(choice, j);
value /= 10;
}
return sum;
}
这是雷雯雯同学的代码,真的非常简洁明了,避免了又长又臭的代码,满眼的羡慕,羡慕。
而且写的代码也是独辟蹊径,很有自己的想法,崇拜,崇拜!很有技巧性的写法!
看来我要加加油了!!!
标签:ice result 自己的 避免 选择 void 怎么 逻辑 number
原文地址:https://www.cnblogs.com/fzhyxc520/p/11831759.html