新手求助。帮忙讲一下这个程序……
#include <stdio.h>#include <stdlib.h>
#include <string.h>
#define PROG_VERSION "v1.0"
#define AUTHOR_NAME "your_name"
#define CLASS "your_class"
#define FINISH_DATE "2012.12.13"
int conv(const char *num, int radix)
{
int p = 1, n = 0;
int i;
for(i = strlen(num) - 1; i >= 0; i--)
{
n += (num[i] - '0') * p;
p *= radix;
}
return n;
}
void write(int dec, int radix)
{
int arr[1000], p;
p = 0;
while(dec) {
arr[p++] = dec % radix;
dec /= radix;
}
while(--p >= 0) putchar((arr[p] < 10) ? (arr[p] + '0') : (arr[p] - 10 + 'A'));
printf("\n");
}
int main()
{
char str[100], num[100];
const int func[6][2] = {{10, 2}, {10, 16}, {10, 8}, {2, 10}, {2, 8}, {2, 16}};
printf("Number Converter %s\n", PROG_VERSION);
printf("%s @ Class %s, %s\n\n", AUTHOR_NAME, CLASS, FINISH_DATE);
while(true) {
printf("A. 10进制 -> 2进制\n");
printf("B. 10进制 -> 16进制\n");
printf("C. 10进制 -> 8进制\n");
printf("D. 2进制 -> 10进制\n");
printf("E. 2进制 -> 8进制\n");
printf("F. 2进制 -> 16进制\n");
printf("Your choice: ");
scanf("%s", str);
getchar();
if(*str >= 'A' && *str <= 'F') {
printf("Number: ");
scanf("%s", num);
getchar();
write(conv(num, func[*str - 'A'][0]), func[*str - 'A'][1]);
} else {
printf("Incorrect choice.\n");
}
printf("\n");
}
}