ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Stack应用----16进制内 任意进制转化成任意进制

2021-12-03 09:02:53  阅读:132  来源: 互联网

标签:WillBeFormat cout 16 int top DS 任意 Stack 进制


#include<iostream>

using namespace std;
#define dataType char


char ch[17] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B','C', 'D', 'E', 'F','G'};




class Stack
{
private:
    dataType *data;
    int top;
    dataType* bottom;
    int lenth ;
public:
    Stack(int MaxSize = 10000+1){
        data = new dataType[MaxSize];
        lenth = MaxSize-1;
        bottom = data;
        top = 0;
    }
    ~Stack(){
        delete data;
        cout<<"destruct successfully!"<<endl;
    }

    void Get_len(int &e){
        e = top;
    }
    int Get_len(){
        return top;
    }

    void IsEmpty()
    {
        if(top == 0){
            cout<<"Stack is empty"<<endl;
        }
        else{
            cout<<"Stack is not empty"<<endl;
        }
    }
    void PushStack(dataType e)
    {
        if(top == lenth){
            cout<<"Stack is full<<endl";
            exit(-1);
        }
        else{
            top++;
            data[top] = e;
        }
    }
    void PopStack(dataType &e)
    {
        if(top == 0){
            cout<<"Stack is empty"<<endl;
        }
        else{
            e = data[top];
            top--;
        }
    }
    Stack(Stack &_Stack){
        this->top = _Stack.top;
        for(int i = 0; i<=_Stack.lenth; i++){
            this->data[i] = _Stack.data[i];
        }
        this->bottom =this->data;
        lenth = _Stack.lenth;
    }
    
};

int main()
{
    Stack* S = new Stack();

    int DS_WillBeFormat = 0;// DS---"进制"
    int DS_Formated = 0;
    cout<<"The DS you want to format:"<<endl;
    cin>>DS_WillBeFormat;
    cout<<"to what?"<<endl;
    cin>>DS_Formated;

    string num_WillBeFormat ;
    int num_Formated = 0;
    cout<<"The DS number you want to format: "<<endl;
    cin>>num_WillBeFormat;

    int sum = 0;

    int _DS_WillBeFormat = 1;

    for(int i = num_WillBeFormat.length()-1; i>=0; i--){
        
        int n = 0;
        for(int j = 0; j<=DS_WillBeFormat-1; j++){
            if(num_WillBeFormat[i] == ch[j]){
                n = j;
                break;
            }
        }
        sum += n*_DS_WillBeFormat;
        _DS_WillBeFormat *= DS_WillBeFormat;
    }
    cout<<sum<<endl;
    dataType Remainder;
    while(sum){
        Remainder = sum % DS_Formated;
        S->PushStack(ch[Remainder]);
        sum /= DS_Formated;
    }

cout<<"The number you want is :"<<endl;
    int L = 0;
    S->Get_len(L);
    dataType n2 = 0;
    for(int i = 1; i<=L; i++){
        S->PopStack(n2);
        cout<<n2;
    }
    cout<<endl;

    delete S;
    
    cout<<"helloworld";
    system("pause");
    return 0;

}

纯手撸

别骂

标签:WillBeFormat,cout,16,int,top,DS,任意,Stack,进制
来源: https://blog.csdn.net/qq_55342499/article/details/121664751

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有