c语言问题7

来源:百度知道 编辑:UC知道 时间:2024/06/08 07:09:56
1. 利用函数完成组合数的计算,即: 。
2. 完成输入一个二进制数,输出其十进制数结果。
3. 完成输入一个十六进制数,输出其二进制数结果。

2&3:
#include <stdio.h>
#include <string.h>
#include <math.h>

char* Itoa(int num, char *str, int radix)
{
char *digit = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", *p = str;

while (num)
{
*p++ = digit[num % radix];
num /= radix;
}
*p = 0;

strrev(str);
return str;
}

void main()
{
int a, c;
char *p, str1[1024], str2[1024];

printf("Please input a binary: ");
scanf("%s", str1);

strrev(str1);

a = 0;
c = 0;
p = str1;
while (*p)
{
if (*p == '1')
{
a += (int)pow(2, c);
}
else if (*p != '0')
{
break;
}
p++;
c++;
}

printf("%s(2) = %s(10)\n", Itoa(a, str1, 2), Itoa(a, str2, 10));

printf("Please input a hex: ");
scanf(&quo