• 数据结构-栈(二)模板-C++实现-2,8,16进制转换


    本文目的:
    实现可以输出16进制带A,B,C,D,E,F等符号的进制转换;

    //栈的定义.h文件,与栈(一)略有不同
    #pragma once
    #include<iostream>
    #include<string>
    using namespace std;
    template<class T>
    class MyStack
    {
    public:
    	MyStack(int size);//构造
    	~MyStack();//析构
    	bool s_empty();//判空
    	bool s_full();//判满
    	void s_in(T t);//进栈
    	T s_out();//出栈
    	void s_traverse(bool n);//遍历
    	void s_clear();//清空
    	int s_length();//长度
    
    private:
    	T* sta;
    	int s_num;
    	int s_size;
    };
    template<class T>
    MyStack<T>::MyStack(int size)
    {
    	s_size = size;
    	s_num = 0;
    	sta = new T[size];
    }
    template<class T>
    MyStack<T>::~MyStack()
    {
    	delete []sta;
    	sta = NULL;
    }
    template<class T>
    bool MyStack<T>::s_empty()//判空
    {
    	return s_num == 0 ? true : false;
    }
    template<class T>
    bool MyStack<T>::s_full()//判满
    {
    	return s_num == s_size ? true : false;
    }
    template<class T>
    void MyStack<T>::s_in(T t)//进栈
    {
    	if (!s_full())
    	{
    		sta[s_num] = t;
    		s_num++;
    	}
    	else
    	{
    		cout << "Can not add any more ingredients!" << endl;
    	}	
    }
    
    template<class T>
    T MyStack<T>::s_out()//出栈
    {
    	if (!s_empty())
    	{
    		s_num--;
    		//cout << sta[s_num] << endl;
    		return sta[s_num];
    	}
    	else
    	{
    		cout << "There is no ingredient!" << endl;
    	}
    }
    
    template<class T>
    void MyStack<T>::s_traverse(bool n)
    {
    	for (int i = 0; i < s_num; i++)
    	{
    		if (n)
    		{
    			cout << sta[(s_num-i-1)];
    		}
    		else
    		{
    			cout << sta[i];
    		}
    		
    	}
    }//遍历
    
    template<class T>
    void MyStack<T>::s_clear()
    {
    	s_num = 0;
    }//清空
    
    template<class T>
    int MyStack<T>::s_length()
    {
    	return s_num;
    }//长度
    
    //main.cpp应用16进制转换
    #include"MyStack.h"
    /***********************************************
    描述:输入任意十进制数字,分别输出该整数的2进制、8进制、和16进制数;
    公式:N=(N div d)*d + N mod d (div表示整除,mod表示求余)
    短除法
    	N		N div 8			N mod 8
       1348	     168				4
       168		  21				0
       21		  2				    5
       2		  0					2
       其中最右侧一列数即为所求,需要利用栈的后进先出原则从下到上输出。
    ************************************************/
    #define binary 2;
    #define octonary 8;
    #define hexadecimal 16;
    int main(int argc,char* argv[])
    {
    	//16进制转换示例
    	//栈使用-进制转换
    	MyStack<int> s(30);
    	char num[] = "0123456789ABCDEF";//作为16进制输出值符号索引
    	int N; 
    	cout << "Which number do you want to change? input it:" << endl;
    	cin >> N;
    	cout << "Its corresponding number is : " << endl;
    	while (N)
    	{
    		int mod = N % hexadecimal;
    		s.s_in(mod);
    		N = N / hexadecimal;
    	}	
    	while (!s.s_empty())//输出时按照输出值找num中索引对应的符号
    	{
    		int n = s.s_out();
    		cout << num[n];
    	}
    	return 0;
    }
    
    /*输出
    Which number do you want to change? input it:
    2019
    Its responding octonary number is :
    7E3
    
    */
    
    Higher you climb, more view you will see.
  • 相关阅读:
    Codeforces Round #709 (Div. 2, based on Technocup 2021 Final Round)
    Codeforces Round #708 (Div. 2)
    Educational Codeforces Round 106 (Rated for Div. 2)
    ccf csp 202012-1
    带配额的文件系统 (带模拟)
    阔力梯的树
    Codeforces Round #707 (Div. 2, based on Moscow Open Olympiad in Informatics)
    如何获取某个网站IP地址?
    C++开发者眼中的Java关键字abstract
    Java代码中如何获文件名和行号等源码信息?
  • 原文地址:https://www.cnblogs.com/yyfighting/p/12500618.html
Copyright © 2020-2023  润新知