Data structure and algorithm: stack ADT implementation and application

Assuming that the data elements of the stack ADT are integers, the implementation of the stack ADT adopts a sequential storage structure. Now we need to use the stack to assist in the conversion of any non-negative decimal integer to Base (Base not greater than 35). Part of the code has been given, please add and improve the stack overflow handling function and main function. Note: Only submit functions that need to be added, other codes are not allowed to rewrite and modify by themselves.
Stack overflow processing function overflowProcess: When the stack is full, the stack space is doubled on the original basis.
Main function: Input a non-negative decimal integer n and the base base to be converted, and output its converted base form and length. The output format is as follows:
($… $ )10=(#…#) Base
Length=The number of digits after conversion,
where $…$ is the input decimal number n, #…# is the converted Base number, If the bit code after conversion is more than 1 digit, it is represented by capital letters A, B, ... etc., 10-A, 11-B, ...
For example, enter

1024 2

Output

(1024)10=(10000000000)2
Length=11

For another example, enter

25 30

Output

(25)10=(P)30          
Length=1

The preset code is as follows:

#include <iostream>
using namespace std;
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
class SeqStack {//Sequence stack class definition
private:
ElemType *elements; //Array storage stack elements
int top ; //Stack top indicator
int maxSize; //stack maximum capacity
void overflowProcess(); //stack overflow processing
public:
SeqStack(int sz); //constructor
~SeqStack() {delete []elements; }; //Destructor
void Push(ElemType x); //Push the stack
int Pop(ElemType &x); //Pull the stack
int IsEmpty() const {return top == -1;}
int IsFull() const {return top = = maxSize-1;}
int GetSize() const {return top+1;} }; SeqStack::SeqStack(int sz) {elements=new ElemType[sz]; //Apply for continuous space
if(elements==NULL) {cout<<"Space application error!"<<endl;exit(1);}
else {top=-1; //stack top indicator points to the bottom of the stack
maxSize=sz; //stack the maximum spatial
};};
supplementary overflowProcess () function
void SeqStack :: Push (elemType x) {// If the stack is full, the overflow process, the element x stack is inserted into the stack
if (IsFull () == 1) overflowProcess (); //The stack is full
elements[++top] = x; //The pointer on the top of the stack is incremented by 1, and then the elements are pushed onto the stack}; int SeqStack::Pop(ElemType & x) {//If the stack is not empty, function Exit the element at the top of the stack and assign the value of the element at the top of the stack to x,
//return true, otherwise return false if (IsEmpty() == 1) return 0;
x = elements[top–]; //take the element first, the top of the stack Pointer back 1
return 1; //Return to stack successfully);
Supplement main() function

void SeqStack::overflowProcess()
{
    
    
    ElemType *a=new ElemType[2*maxSize];
    int i;
    for(i=0;i<=top;i++)
    {
    
    
        a[i]=elements[i];
    }
    maxSize*=2;
    delete []elements;
    elements=a;
}
int main()
{
    
    
    int n,base,t,len=0;
    SeqStack num(100);
    cin>>n>>base;
    if(n==0)
    {
    
    
        cout<<"(0)10=(0)"<<base<<endl;
        cout<<"Length=1"<<endl;
    }
    else
    {
    
    
        cout<<"("<<n<<")10=(";
        while(n)
        {
    
    
            num.Push(n%base);
            n=n/base;
        }
        while(!num.IsEmpty())
        {
    
    
            if(num.Pop(t)==1)
            {
    
    
                if(t>=0&&t<=9)cout<<t;
                else cout<<(char)(t-10+'A');
                len++;
            }
        }
        cout<<")"<<base<<endl;
        cout<<"Length="<<len;
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/upc122/article/details/105838879