数制的转换

/*
  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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值