数制转换是将任意一个非负的十进制数转换为其他进制的数,一般的方法是采用辗转相除法。参考《C#数据结构》

              N          N/8        N%8

            5142        642        6

             642         80         2

             80           10         0

             10           1           2

              1            0           1

      从上面可得出:5142转换为八进制为12026

     实现上述的代码如下:书中是封装为一个方法,因为我先前的类是泛型类,在里面不能直接转换为整型类型,希望网友提出改进意见。

     

 1              //栈的使用:数制转换
 2                SeqStack<int> list=new SeqStack<int>(10);
 3                int item=0;
 4                Console.Write("请输入一个整型值:");
 5                item=Convert.ToInt32(Console.ReadLine());
 6                while (item > 0)
 7                {
 8                    list.Push(item%8);
 9                    item = item / 8;
10                }

11                while (!list.IsEmpty())
12                {
13                    item = list.Pop();
14                    Console.WriteLine("{0}",item);
15                }

 

完整代码如下:

 

  1using System;
  2using System.Collections.Generic;
  3using System.Text;
  4
  5namespace 栈和队列
  6{
  7    class Program
  8    {
  9        public interface IStack<T>
 10        
 11           //求栈的长度
 12             int GetLength();
 13            //判断是否为空
 14            bool IsEmpty(); 
 15            //判断是否已满
 16            bool IsFull();
 17            //清空操作
 18            void Clear();
 19            //入栈
 20            void Push(T item);
 21            //出栈
 22            T Pop();
 23            //取栈顶元素
 24            T GetTop();
 25           
 26            
 27
 28        }

 29        public class SeqStack<T> : IStack<T>
 30        
 31           //栈的容量
 32            private int maxsize;
 33           //存储栈中的数据
 34            private T[] data;
 35           //指定栈的栈顶
 36            private int top;
 37
 38            //索引器
 39            public T this[int index]
 40            {
 41                get
 42                
 43                   return data[index];
 44                }

 45                set
 46                {
 47                    data[index] = value;
 48                }

 49            }

 50
 51            //容量属性
 52            public int Maxsize
 53            {
 54                get
 55                {
 56                    return maxsize;
 57                }

 58                set
 59                {
 60                    maxsize = value;
 61                }

 62            }

 63
 64            //栈顶属性
 65            public int Top
 66            {
 67                get
 68                {
 69                    return top;
 70                }

 71            }

 72
 73            //构造器
 74            public SeqStack(int size)
 75            {
 76                data = new T[size];
 77                maxsize = size;
 78                top = -1;
 79            }

 80
 81            //求栈的长度
 82            public int GetLength()
 83            {
 84                return top + 1;
 85            }

 86
 87            //清空栈
 88            public void Clear()
 89            {
 90                top = -1;
 91            }

 92
 93            //判断是否为空
 94            public bool IsEmpty()
 95            {
 96                if (top == -1)
 97                {
 98                    return true;
 99                }

100                else
101                {
102                    return false;
103                }

104            }

105
106            //判断是否以满
107            public bool IsFull()
108            {
109                if (top == maxsize - 1)
110                {
111                    return true;
112                }

113                else
114                {
115                    return false;
116                }

117            }

118
119            //入栈
120            public void Push(T item)
121            {
122                if (IsFull())
123                {
124                    Console.WriteLine("栈满啦,要清空啦!");
125                    return;
126                }

127                data[++top] = item;
128            }

129            //出栈
130            public T Pop()
131            
132               T tmp=default(T);
133               if (IsEmpty())
134               {
135                   Console.WriteLine("栈已空!");
136                   return tmp;
137               }

138               tmp = data[top];
139               --top;
140               return tmp;
141            }

142            //获取栈顶数据元素
143            public T GetTop()
144            {
145                if (IsEmpty())
146                {
147                    Console.WriteLine("表已空!");
148                    return default(T);
149                }

150                return data[top];
151            }

152            
153            
154
155        }

156
157        static void Main(string[] args)
158        {
159           
160            SeqStack<int> list=new SeqStack<int>(10);
161            //栈的使用:数制转换
162                int item=0;
163                Console.Write("请输入一个整型值:");
164                item=Convert.ToInt32(Console.ReadLine());
165                while (item > 0)
166                {
167                    list.Push(item%8);
168                    item = item / 8;
169                }

170                while (!list.IsEmpty())
171                {
172                    item = list.Pop();
173                    Console.WriteLine("{0}",item);
174                }

175            }

176
177        }

178    }

179
180
posted on 2009-03-09 19:53  甲乙丙丁  阅读(1439)  评论(0编辑  收藏  举报