程序代码:
/** * @文件名:Program.cs * @作 者:寒风中的细雨 * @时 间:2012/5/11/ * @概 述:使用顺序链表实现进制转换 */ using System; public interface IStack<T> { int GetLength();//求栈的长度 bool IsEmpty();//判断栈是否为空 void Clear();//清空栈操作 void Push(T nItem);//入栈 void Pop();//出栈 T GetTop();//取栈顶元素 } //顺序栈 public class SeqStack<T> : IStack<T> { private int m_MaxSize;//顺序栈的容量 private T[] m_Data; //数组 private int m_Top;//指示顺序栈的栈顶 //索引器 public T this[int nIndex] { get { return m_Data[nIndex];} set { m_Data[nIndex] = value; } } //容量属性 public int MaxSize { get { return m_MaxSize; } set { m_MaxSize = value; } } //构造器 public SeqStack(int nSize) { m_Data = new T[nSize]; m_MaxSize = nSize; m_Top = -1; } //求栈的长度 public int GetLength() { return (m_Top + 1); } //判断栈是否为空 public bool IsEmpty() { return (-1 == m_Top); } //判断栈是否为满 public bool IsFull() { return (m_MaxSize - 1 == m_Top); } //清空栈操作 public void Clear() { m_Top = -1; } //进栈 public void Push(T nItem) { if (IsFull()) { Console.WriteLine("压栈失败,栈满..."); return; } m_Data[++m_Top] = nItem; } //出栈 public void Pop() { if (IsEmpty()) { Console.WriteLine("出栈失败,栈空..."); return; } --m_Top; } //取栈顶元素 public T GetTop() { return m_Data[m_Top]; } } public class SamplesArrayList { static SeqStack<int> stack = new SeqStack<int>(80); public static void Main() { int N;//要转换的进制 int getConsoleInput;// Console.Write("输入要变换的数字(十进制):"); getConsoleInput = int.Parse(Console.ReadLine()); Console.Write("输入要转换的进制:"); N = int.Parse(Console.ReadLine()); Conversion(getConsoleInput, N); Console.Write("转换成"+N+"进制后的结果:"); while (!stack.IsEmpty()) { if (stack.GetTop() >= 10) { Console.Write((char)(87 + stack.GetTop())); } else { Console.Write(stack.GetTop()); } stack.Pop(); } Console.WriteLine(); } public static void Conversion(int a, int b) { do { stack.Push(a%b); a = a / b; } while (0 != a); } } 输入要变换的数字(十进制):250 输入要转换的进制:16 转换成16进制后的结果:fa 请按任意键继续. . .