# C + + stack application: number conversion

The conversion between decimal N and other decimal numbers is a basic problem in computer computing. There are many ways to solve it. One simple algorithm is based on the following principles:
N=(n div d)*d+n mod d
(where: div is the integer division operation, mod is the remainder operation)
For example, (1348) 10 = (2504) 8, the operation process is as follows:

It is not difficult to find that the final remainder 2 is in the highest position (the first digit) of the new octal number, and the first remainder 4 is in the lowest position (the last digit) of the new octal number. However, we usually read from the highest position, so it's a simple way to store the remainder obtained by every mod in the structure with the characteristics of LIFO. In this way, we only need to read out the remainder in turn to get the corresponding number of the new number system. And the stack just has this feature.
The algorithm of number system conversion using stack is as follows:

```void Conversion(int n,int base)
{
s=InitStack();
int remainder;
while(n!=0){ //Carry out modular operation until n is 0 and exit the cycle
remainder=n%base;
Push(s,remainder);
n=n/base;
}
while(s->next!=NULL)  //Pop up the elements at the top of the stack in turn, and the final number is the base number
cout<<Pop(s);
cout<<endl;
}
```

1. The complete program of digital conversion using chain stack

```#include <iostream>
using namespace std;
/*Define node type of chain stack*/
typedef struct Node
{
int data;
Node *next;

int main()
{
void Conversion(int n,int base);

int n,base;
cout<<"Please enter the decimal number to convert:";
cin>>n;
cout<<"Please enter the base number of the new number system:";
cin>>base;
Conversion(n,base);
return 0;
}

/*Empty stack: initialize chain stack*/
{
s->next=NULL;
return(s);
}

/*Read the value x at the top of the stack*/
{
p->data=x;
p->next=s->next;
s->next=p;
}

/*Out of stack: pop up the top element of the stack*/
{
int top;
if(s->next==NULL){
cout<<"Stack space"<<endl;
return 0;
}
else{
q=s->next;
top=q->data;
s->next=q->next;
delete q;
return(top);
}
}

/*Number system transformation*/
void Conversion(int n,int base)
{
s=InitStack();
int remainder;
while(n!=0){ //Carry out modular operation until n is 0 and exit the cycle
remainder=n%base;
Push(s,remainder);
n=n/base;
}
while(s->next!=NULL)  //Pop up the elements at the top of the stack in turn, and the final number is the base number
cout<<Pop(s);
cout<<endl;
}
```

2. The complete program of digital conversion using sequence stack

```#include <iostream>
using namespace std;
#define MaxSize 100
/*Define node type of sequence stack*/
struct SeqStack
{
int data[MaxSize];
int top;
};

int main()
{
SeqStack *InitStack();
void Push(SeqStack *s,int x);
int Pop(SeqStack *s);
void Conversion(int n,int base);

int n,base;
cout<<"Please enter the decimal number to convert:";
cin>>n;
cout<<"Please enter the base number of the new number system:";
cin>>base;
Conversion(n,base);
return 0;
}

/*Empty stack: initialize sequence stack*/
void InitStack(SeqStack *s)
{
s->top=-1;
}

/*Read the value x at the top of the stack*/
void Push(SeqStack *s,int x)
{
if(s->top==MaxSize-1)
cout<<"The stack is full."<<endl;
else{
s->data[++s->top]=x;
}
}

/*Out of stack: pop up the top element of the stack*/
int Pop(SeqStack *s)
{
int x;
if(s->top==-1){
cout<<"The stack is empty."<<endl;
return 0;
}
else{
x=s->data[s->top];
s->top--;
return(x);
}
}

/*Number system transformation*/
void Conversion(int n,int base)
{
SeqStack *s,Stack;
s=&Stack;
InitStack(s);
int remainder;
while(n!=0){ //Carry out modular operation until n is 0 and exit the cycle
remainder=n%base;
Push(s,remainder);
n=n/base;
}
while(s->top!=-1)  //Pop up the elements at the top of the stack in turn, and the final number is the base number
cout<<Pop(s);
}
```

Be careful:
When I use the sequence stack to implement the number system conversion, when I set the InitStack() function and Conversion() function as follows, the program cannot run normally.

```/*Empty stack: initialize sequence stack*/
SeqStack *InitStack()
{
SeqStack *s,Stack;
s=&Stack;
s->top=-1;
return(s);
}
```
```/*Number system transformation*/
void Conversion(int n,int base)
{
SeqStack *s;
s=InitStack();
int remainder;
while(n!=0){ //Carry out modular operation until n is 0 and exit the cycle
remainder=n%base;
Push(s,remainder);
n=n/base;
}
while(s->top!=-1)  //Pop up the elements at the top of the stack in turn, and the final number is the base number
cout<<Pop(s);
}
```

Looking for reasons on the Internet, some people also encounter such problems when using the sequential stack. It seems that the solutions are related to the initialization of the stack. The specific reasons can't be fully figured out for the moment. First, record the problem here.

Published 5 original articles, won praise 4, visited 163

Posted by arianhojat on Wed, 05 Feb 2020 01:53:43 -0800