Decimal to binary, octal, Hex Classic Program

Source: Internet
Author: User
Tags decimal to binary

#include <iostream.h>

Converts decimal numbers to binary digits
void fun_1 (int n)
{
if (n<2)
cout<<n;
if (n>=2)
{
Fun_1 (N/2);
cout<<n%2;
}
}

Decimal digits converted into octal digits
void fun_2 (int n)
{
if (n<8)
cout<<n;
if (n>=8)
{
Fun_2 (N/8);
cout<<n%8;
}
}

Converts decimal numbers to hexadecimal digits
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);
}
}

Main function
void Main ()
{
int n;
cout<< "Please enter the value of N:" <<endl;
cin>>n;
cout<< "decimal digits are converted to binary, octal, and hexadecimal digits result as follows:" <<endl;
Fun_1 (n);
cout<<endl; Output line Wrap
Fun_2 (n);
cout<<endl;
Fun_4 (n);
cout<<endl;
}





Yes, I also have a share with you. can be converted from decimal to 2 to 16. But it's not very useful.
#include <iostream.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>/* malloc () and so on * *
#include <limits.h>/* Int_max etc * *
#include <stdio.h>/* EOF (=^z or 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; Stacks are made up of stack-bottom pointers and stack-top pointers as well as the stack's capacity. Top-1 is the length of the stack

Status Initstack (Sqstack &s)
{
S.base = (Selemtype *) malloc (MB * 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<< "The data you entered is not within range. ";
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;
Todo
{
cout<< "Please enter any integer:";
cin>>n;
cout<<endl;
cout<< "Please enter the system to convert:";
cin>>r;
cout<<endl;
cout<< "Convert data to:";
conversion (N,R);
System ("CLS");
}while (N);
}


The

follows the idea of a stack, but it is written in a one-dimensional array.
#include <stdio.h>
#define L
void Converse (int n,int base)
{
    int s[l ],X,TOP;&NBSP;&NBSP;&NBSP
    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 ("Enter the value you want to convert: \ n");
    scanf ("%d", &n);
    printf ("Please enter conversion: \ n");
    scanf ("%d", &base);
    converse (n,base);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.