栈和队列应用之数制转换

  数制转换是将任意一个非负的十进制数转换为其他进制的数,一般的方法是采用辗转相除法。参考《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 ExpandedBlockStart.gifContractedBlock.gif                 {
 8                    list.Push(item%8);
 9                    item = item / 8;
10                }

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

 

完整代码如下:

 

  1 using  System;
  2 using  System.Collections.Generic;
  3 using  System.Text;
  4
  5 namespace  栈和队列
  6 ExpandedBlockStart.gifContractedBlock.gif {
  7    class Program
  8ExpandedSubBlockStart.gifContractedSubBlock.gif    {
  9        public interface IStack<T>
 10ExpandedSubBlockStart.gifContractedSubBlock.gif        
 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>
 30ExpandedSubBlockStart.gifContractedSubBlock.gif        
 31           //栈的容量
 32            private int maxsize;
 33           //存储栈中的数据
 34            private T[] data;
 35           //指定栈的栈顶
 36            private int top;
 37
 38            //索引器
 39            public T this[int index]
 40ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 41                get
 42ExpandedSubBlockStart.gifContractedSubBlock.gif                
 43                   return data[index];
 44                }

 45                set
 46ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 47                    data[index] = value;
 48                }

 49            }

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

 58                set
 59ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 60                    maxsize = value;
 61                }

 62            }

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

 71            }

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

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

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

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

100                else
101ExpandedSubBlockStart.gifContractedSubBlock.gif                {
102                    return false;
103                }

104            }

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

113                else
114ExpandedSubBlockStart.gifContractedSubBlock.gif                {
115                    return false;
116                }

117            }

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

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

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

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

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

150                return data[top];
151            }

152            
153            
154
155        }

156
157        static void Main(string[] args)
158ExpandedSubBlockStart.gifContractedSubBlock.gif        {
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)
166ExpandedSubBlockStart.gifContractedSubBlock.gif                {
167                    list.Push(item%8);
168                    item = item / 8;
169                }

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

175            }

176
177        }

178    }

179
180

转载于:https://www.cnblogs.com/zhangq0309/archive/2009/03/09/1407260.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值