棧基本操作(順序棧+數制轉換)


#include<iostream>
#include<cstdlib>
using namespace std;

//定義初始化長度和每次增加的長度
const int STACK_INIT_SIZE=10;
const int STACK_INCREAMENT=2;

struct Stack{
    int* base;  //棧底
    int* top;   //棧頂
    int stacksize;  //已分配棧的大小
};

//函數聲明
void show();//主界面
void InitStack(Stack &S);//初始化棧
void DestroyStack(Stack &S);//銷毀棧
void ClearStack(Stack &S);//清空棧
int StackEmpty(Stack S);//判斷棧是否為空
int StackLength(Stack S);//獲取棧的長度
void GetTop(Stack S,int &e);//獲取頂部元素
void Push(Stack &S,int e);//入棧
void Pop(Stack &S,int &e);//出棧
void StackVisit(Stack S);//從棧頂遍歷棧
void Transfer(int n,int r,Stack &s);//數制轉換


int main(){
    show();
    Stack s;
    int e,action;
    while(cin>>action){
        switch(action){
            case 1://初始化棧
                system("cls");
                InitStack(s);
                break;
            case 2://銷毀棧
                system("cls");
                DestroyStack(s);
                break;
            case 3://清空棧
                system("cls");
                ClearStack(s);
                cout<<"棧已經被清空"<<endl<<endl;
                break;
            case 4://判斷棧是否為空
                system("cls");
                StackEmpty(s);
                break;
            case 5://獲取棧的長度
                system("cls");
                StackLength(s);
                break;
            case 6://獲取頂部元素
                system("cls");
                GetTop(s,e);
                break;
            case 7://入棧
                system("cls");
                cout<<"請輸入將壓入棧的元素個數:"<<endl;
                int l;
                cin>>l;
                cout<<"請依次輸入待壓棧的元素:"<<endl;
                for(int i=0;i<l;i++){
                    cin>>e;
                    Push(s,e);
                }
                break;
            case 8://出棧
                system("cls");
                Pop(s,e);
                break;
            case 9://從棧頂遍歷棧
                system("cls");
                StackVisit(s);
                break;
            case 10://數制轉換
            	system("cls");
            	cout<<"請輸入十進制數和需要轉換為幾進制數:"<<endl;
            	int n,r;
            	cin>>n>>r;
            	Transfer(n,r,s);
            	break;
            default:
                return 0;
        }
        system("pause");
        system("cls");
        show();
    }
    return 0;
}

//主界面
void show(){
    cout<<"+----------------------------------------+"<<endl;
    cout<<"|                                        |"<<endl;
    cout<<"|        1 ->初始化棧                    |"<<endl;
    cout<<"|        2 ->銷毀棧                      |"<<endl;
    cout<<"|        3 ->清空棧                      |"<<endl;
    cout<<"|        4 ->判斷棧是否為空              |"<<endl;
    cout<<"|        5 ->獲取棧長度                  |"<<endl;
    cout<<"|        6 ->獲取棧頂元素                |"<<endl;
    cout<<"|        7 ->壓棧                        |"<<endl;
    cout<<"|        8 ->出棧                        |"<<endl;
    cout<<"|        9 ->遍歷棧                      |"<<endl;
    cout<<"|        10->數制轉換                    |"<<endl;
    cout<<"|        11->退出                        |"<<endl;
    cout<<"|                                        |"<<endl;
    cout<<"+----------------------------------------+"<<endl;
}

//初始化棧
void InitStack(Stack &S){
    S.base=new int[STACK_INIT_SIZE];
    S.top=S.base;
    S.stacksize=STACK_INIT_SIZE;
    cout<<"棧已經初始化!"<<endl<<endl;
}

//銷毀棧
void DestroyStack(Stack &S){
    delete S.base;
    cout<<"棧已經被銷毀"<<endl<<endl;
}

//清空棧
void ClearStack(Stack &S){
    S.top=S.base;
}

//判斷棧是否為空
int StackEmpty(Stack S){
    if(S.base==S.top){
        cout<<"棧為空"<<endl<<endl;
        return 1;
    } else {
        cout<<"棧不為空!"<<endl<<endl;
        return 0;
    }
}

//獲取棧的長度
int StackLength(Stack S){
    cout<<"the length of the stack is "<<S.top-S.base<<endl<<endl;
    return S.top-S.base;
}

//獲取頂部元素
void GetTop(Stack S,int &e){
    if(S.top==S.base){
        cout<<"棧為空,長度為0"<<endl<<endl;
    } else{
        e=*(S.top-1);
        cout<<"棧的長度為 "<<e<<endl<<endl;
    }
}

//入棧
void Push(Stack &S,int e){
    if(S.top-S.base>=S.stacksize){
        //在原有的空間上增加一段空間,用realloc(),具體使用方法請自行百度
        S.base=(int* )realloc(S.base,(S.stacksize+STACK_INCREAMENT)*sizeof(int));
        if(!S.base){
            cout<<"重新分配空間出錯"<<endl<<endl;
        } else {
            S.top=S.base+S.stacksize;
            S.stacksize+=STACK_INCREAMENT;
        }
    }
    *(S.top++)=e;
    cout<<e<<" 已經壓入棧"<<endl;
}

//出棧
void Pop(Stack &S,int &e){
    if(S.top==S.base){
        cout<<"棧為空"<<endl<<endl;
    } else {
        e=*--S.top;
        cout<<e<<" 已經出棧"<<endl;
    }
}

//從棧頂遍歷棧
void StackVisit(Stack S){
    if(S.top==S.base){
        cout<<"棧為空"<<endl<<endl;
    }
    int e;
    while(S.top>S.base){
        //方法一
        cout<<*(--S.top)<<' ';
        //方法二
        //Pop(S,e);
    }
    cout<<endl;
}

//進制轉換,將十進制的n轉換為r進制的數
void Transfer(int n,int r,Stack &S){
    ClearStack(S);
    //n=(n/r)*r+n%r;
    while(n){
        Push(S,n%r);
        n=n/r;
    }
    cout<<"轉換成功!轉換后的"<<r<<"進制數為:"<<endl;
    while(S.top>S.base){
        cout<<*(--S.top);
    }
    cout<<endl<<endl;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM