[Cpp]
/**
1. Use the stack to implement the number conversion, requiring Programming to Implement the following functions:
A. compile the SqStack file. h. Define the stack template SqStack, as well as the inbound, outbound, and read stack top elements on the stack, determine whether the stack is empty, clear the stack, and initialize the stack.
B. Compile an algorithm void MultiBaseOutput (int N, int B) to convert a decimal integer N to another equivalent B-base number and output it to the screen.
If B is 16, it can output A, B, C, D, E, F. For example, decimal 15 should be converted to hexadecimal F.
C. Compile the test main function. Convert a decimal number 30 to a binary number, an octal number, and a hexadecimal number.
*/
# Include "Stack. h"
# Include <iostream>
Using namespace std;
Void MultiBaseOutput (int N, int B)
{
SqStack <char> ST (100 );
Int temp, l;
Char T;
Char t = 'a ';
While (N)
{
Temp = N % B;
If (temp> 9)
{
L = temp-10;
ST. push_stack (char (t + (temp-10 )));
}
Else
{
ST. push_stack (char (temp + '0 '));
}
N/= B;
}
While (! ST. Empty ())
{
ST. pop_stack (T );
Cout <T <"";
}
}
Int main ()
{
Int m = 0, n = 0;
Cout <"this is a demo program for converting a decimal number to a binary number, an octal number, and a hexadecimal number ................. ...... "<endl;
Cout <"Enter the decimal number :";
Cin> m;
Cout <endl;
Cout <"Enter the hexadecimal format you want to convert :";
Cin> n;
Cout <"decimal:" <m <"converts to" <n <"hexadecimal number :";
MultiBaseOutput (m, n );
Cout <endl;
Return 0;
}
************ *************/
// Stack. h
# Include <iostream>
Using namespace std;
Template <class T>
Class SqStack
{
Public:
SqStack ();
SqStack (int size );
// SqStack (const SqStack <T>©);
// Void operator = (const SqStack <T>©);
Virtual ~ SqStack ();
Int Length () const;
Bool Empty () const;
Void Clear ();
Void traverse (void (* visit) (T &));
T & top_stac () const; // unstack top Element
Void push_stack (const T &); // inbound Stack
Void pop_stack (T & e); // output Stack
Void init (int size );
Void top_stack (T & e) const;
Bool full ()
{Www.2cto.com
Return m_top> = m_size;
}
Protected:
Int m_top;
Int m_size;
T * m_base;
};
Template <class T>
SqStack <T>: SqStack ()
{
M_top = 0;
M_size = 100;
M_base = new T [m_size];
}
Template <class T>
SqStack <T>: SqStack (int size)
{
M_top = 0;
M_base = new T [size];
M_size = size;
}
Template <class T>
SqStack <T> ::~ SqStack ()
{
If (m_base! = NULL)
Delete [] m_base;
}
Template <class T>
Int SqStack <T >:: Length () const
{
Return m_top;
}
Template <class T>
Bool SqStack <T >:: Empty () const
{
Return m_top = 0;
}
Template <class T>
Void SqStack <T >:: Clear ()
{
M_top = 0;
}
Template <class T>
Void SqStack <T>: traverse (void (* visit) (T &))
{
For (int curposition = 1; curposition <= length (); curposition ++)
{
(* Visit) (m_base [curposition-1]);
}
}
Template <class T>
Void SqStack <T >:: push_stack (const T & e)
{
If (full () cout <"stack is full ";
Else
{
M_base [m_top ++] = e;
}
}
Template <class T>
Void SqStack <T >:: top_stack (T & e) const
{
If (Empty ())
Cout <"stack is empty ";
Else
{
E = m_base [m_top-1];
}
}
Template <class T>
Void SqStack <T >:: pop_stack (T & e)
{
If (Empty ())
Cout <"stack is empty ";
Else
{
E = m_base [m_top-1];
M_top --;
}
}