/*
Name: bodh.c
Author: x-bit
Description: 十进制,二进制,八进制,十六进制的转换
Date: 04/22/2004
Copyright: x-bit@163.com
*/
/*#include <stdio.h>*/
#include <process.h>
#define M 100
/*************************************************************/
void Menu();
int GetSelect();
void ios(int select);
void exchange(long n, int base);
/*************************************************************/
int main()
{
Menu();
while(1)
{
ios(GetSelect());
printf("/n");
}
return 0;
}
/*************************************************************/
void exchange(long n, int base)
{
int arr[M];
int i=0;
do
{
arr[i++]=n%base;
n/=base;
}while(n);
/*************************************************************/
do
{
switch(arr[--i])
{
case 10: printf("A"); break;
case 11: printf("B"); break;
case 12: printf("C"); break;
case 13: printf("D"); break;
case 14: printf("E"); break;
case 15: printf("F"); break;
default: printf("%d", arr[i]);
}
}while(i);
return;
}
/*************************************************************/
void Menu()
{
printf("*****************************************/n");
printf("* 0. Quit; */n");
printf("* 1. Binary to oct, decimal and hex; */n");
printf("* 2. Oct to binary, decimal and hex; */n");
printf("* 3. Decmial to binary, oct and hex; */n");
printf("* 4. Hex to binary, oct and decimal; */n");
printf("* Hex: 12AF; Oct: 17 */n");
printf("*****************************************/n");
}
/*************************************************************/
int GetSelect()
{
int sel;
do
{
printf("Enter a choice(0,1,2,3,4): ");
scanf("%d", &sel);
if(!sel)
exit(1);
}while(sel!=1 && sel!=2 && sel!=3 && sel!=4);
return sel;
}
/*************************************************************/
void ios(int select)
{
int num, base;
switch(select)
{
case 1:
/***********************/
printf("Please do another: 2-3/n");
break;
case 2:
printf("Enter a Oct number: ");
scanf("%o", &num);
do
{
printf("Enter the base(2,8,10,16): ");
scanf("%d", &base);
}while(!base);
exchange(num, base);
break;
case 3:
printf("Enter a Decimal number: ");
scanf("%d", &num);
printf("Enter the base(2,8,10,16): ");
scanf("%o", &num);
do
{
printf("Enter the base(2,8,10,16): ");
scanf("%d", &base);
}while(!base);
exchange(num, base);
break;
case 4:
printf("Enter a Hex number: ");
scanf("%x", &num);
printf("Enter the base(2,8,10,16): ");
scanf("%o", &num);
do
{
printf("Enter the base(2,8,10,16): ");
scanf("%d", &base);
}while(!base);
exchange(num, base);
break;
}
return;
}