十进制转二进制、八进制、十六进制的经典程序

来源:互联网 发布:淘宝不能发布游戏账号 编辑:程序博客网 时间:2024/06/10 02:45

#include <iostream.h>

//十进制数转换成二进制数字
void fun_1(int n)
{
   if(n<2)   
       cout<<n;
   if(n>=2)  
   {
     fun_1(n/2);
     cout<<n%2;
   }
}

//十进制数字转换成八进制数字
void fun_2(int n)
{
  if(n<8)   
      cout<<n;
  if(n>=8)  
  {
    fun_2(n/8);
    cout<<n%8;
  }
}

//十进制数转换成十六进制数字
void fun_3(int n)
{
  switch(n)
  {
     case 10:   cout<<"A"; break;
     case 11:   cout<<"B"; break;
     case 12:   cout<<"C"; break;
     case 13:   cout<<"D"; break;
     case 14:   cout<<"E"; break;
     case 15:   cout<<"F"; break;
     default:   cout<<n;   
  }
}     

void fun_4(int n)
{
   if(n<16)   
       fun_3(n);
   if(n>=16)  
   {
     fun_4(n/16);
     fun_3(n%16);
   }
}

//主函数
void main()

    int n;
    cout<<"请输入n的值:  "<<endl;
    cin>>n;
    cout<<"十进制数字转换成二进制,八进制,十六进制数字结果如下:"<<endl;
    fun_1(n);        
    cout<<endl;      //输出结果换行
    fun_2(n);        
    cout<<endl;      
    fun_4(n);       
    cout<<endl;    





不错,我也有一个和大家分享下。可以从十进制转换成2至16进制。不过用处不大了
#include<iostream.h>
#include<string.h>
#include<ctype.h>
#include<malloc.h> /* malloc()等 */
#include<limits.h> /* INT_MAX等 */
#include<stdio.h> /* EOF(=^Z或F6),NULL */
#include<stdlib.h>
#include<process.h> /* exit() */

#define TURE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define STACK_INIT_SIZE 100;
#define STACKINCREMENT 10;

typedef int Status;
typedef int SElemType;
typedef int Boolean;


typedef struct{
    SElemType *base;
    SElemType *top;
    int stacksize;
}SqStack;  // 栈是由栈底指针和栈顶指针以及栈的容量组成。top-1便是栈的长度

Status InitStack(SqStack &S)
{
    S.base = (SElemType *)malloc(100 * sizeof(SElemType));
    if(!S.base) exit(OVERFLOW);
    S.top = S.base;
    S.stacksize = STACK_INIT_SIZE;
    return OK;
}

Status PushStack(SqStack &S, SElemType e)
{
    if(S.top - S.base >= S.stacksize)
    {
        S.base = (SElemType *)realloc(S.base,(S.stacksize+10) * sizeof(SElemType));
        if(!S.base)  exit(OVERFLOW);
        S.top = S.base + S.stacksize;
        S.stacksize += STACKINCREMENT;
    }
    *S.top++ = e;
    return OK;
}

Status PopStack(SqStack &S, SElemType &e)
{
    if(S.top == S.base) return ERROR;
     e = * --S.top;
     return OK;
}

Status StackEmpty(SqStack S)
{
    if(S.top == S.base)
        return TURE;
    else  return FALSE;
    return OK;
}

void conversion(int N,int r)
{
    SqStack S;
    SElemType e;
    InitStack(S);
    if(N<0)
    {
        cout<<"您输入的数据不在范围之内!";
        cout<<endl;
        return;
    }
    /*if(!N) 
        PushStack(S,0);*/
    while(N)
    {
        PushStack(S,N%r);
        N = N/r;
    }
    while(!StackEmpty(S))
    {
        PopStack(S,e);
        if(e<=9)
            cout<<e;
        else
            cout<<char(e+55);
    }
    cout<<endl;
}

void main()
{
    int N,r;
    do
    {
    cout<<"请输入任意一个整数:   ";
            cin>>N;
            cout<<endl;
    cout<<"请输入要转换的进制:  ";
            cin>>r;
            cout<<endl;
    cout<<"转换数据为:";
        conversion(N,r);
        system("cls");
    }while(N);
}


以下是用栈的思想,但是用一维数组写的。
#include <stdio.h>
#define L 20
void Converse(int n,int base)
{
    int s[L],x,top;   
    top=-1;
    while(n)
    {
        s[++top]=n%base;
        n=n/base;
    }
    while(top!=-1)
    {
        x=s[top--];
        printf("%d",x);
    }
}
void main()
{
    int n,base;
    printf("请输入需要转换的数值:\n");
    scanf("%d",&n);
    printf("请输入转换的进制:\n");
    scanf("%d",&base);
    Converse(n,base);
}

原创粉丝点击