• 堆栈


    [Serializable]
    [ComVisible(true)]
    [DebuggerDisplay("Count = {Count}")]
    [DebuggerTypeProxy(typeof(Stack.StackDebugView))]
    public class Stack : ICollection, IEnumerable, ICloneable
    {}

    [Serializable]
    [ComVisible(false)]
    [DebuggerDisplay("Count = {Count}")]
    public class Stack<T> : IEnumerable<T>, IEnumerable, ICollection, IReadOnlyCollection<T>
    {}

    堆栈 判断回文数

            static void Main(string[] args)
            {
                string str = "abcba";
                bool flag = true;
                //for (int i = 0; i <= str.Length/2; i++)
                //{
                //    if (str[i]!=str[str.Length-1-i])
                //    {
                //        flag = false;
                //    }
                //}
               
                Stack list = new Stack();
                string ch;
                for (int i = 0; i < str.Length; i++)
                {
                    list.Push(str[i]);
                }
                int pos = 0;
                while(list.Count>0)
                {
                    ch = list.Pop().ToString();
                    if (ch!=str.Substring(pos,1))
                    {
                        flag = false;
                        break;
                    }
                    pos++;
                }
                Console.WriteLine(flag);
                Console.ReadLine();
            }
    View Code

     下面这段代码有bug,

        class Program
        {
            static void Main(string[] args)
            {
                //string[] names = new string[] { "abc","def","ghi"};
                //Stack nameStack = new Stack(names);
                //foreach (var item in nameStack)
                //{
                //    Console.WriteLine(item);
                //}
                //Console.ReadLine();
    
                //Stack nums = new Stack();
                //Stack ops = new Stack();
                //string expression = "5+6+7+8";//+10";
                //Calculate(nums,ops,expression);
                //Console.WriteLine(nums.Pop());
    
                Stack myStack = new Stack();
                for (int i = 0; i < 20; i++)
                {
                    myStack.Push(i);
                }
                object[] myArray=new object[myStack.Count+5];
                myStack.CopyTo(myArray,5);
                //object ss=myArray[0];
                //Console.WriteLine(myArray[0]);
                foreach (var item in myArray)
                {
                    Console.Write(item+",");
                }
                Console.WriteLine();
                object[] myArray1=new object[myStack.Count+5];
                myArray1 = myStack.ToArray();
                foreach (var item in myArray1)
                {
                    Console.Write(item+",");
                }
                Console.WriteLine();
                Console.ReadLine();
            }
    
            static bool IsNumeric(string input)
            {
                bool flag = true;
                string pattern = @"^d+$";
                Regex reg = new Regex(pattern);
                if (!reg.IsMatch(input))
                {
                    flag = false;
                }
                return flag;
            }
    
            static void Calculate(Stack N,Stack O,string exp)
            {
                string ch, token = "";
                for (int i = 0; i < exp.Length; i++)
                {
                    ch = exp.Substring(i,1);
                    if (IsNumeric(ch))
                    {
                        token += ch;
                    }
                    if (ch!="" || i==(exp.Length-1))
                    {
                        if (IsNumeric(token))
                        {
                            N.Push(token);
                            token = "";
                        }
                        else if (ch == "+" || ch == "-" || ch == "*" || ch == "/")
                        {
                            O.Push(ch);
                        }
                    }
                    if (N.Count==2)
                    {
                        Compute(N,O);
                    }
                }
            }
    
            static void Compute(Stack N,Stack O)
            {
                int op1, op2;
                string op;
                op1 = Convert.ToInt32(N.Pop());
                op2 = Convert.ToInt32(N.Pop());
                op = Convert.ToString(O.Pop());
    
                switch (op)
                {
                    case"+":
                        N.Push(""+(op1+op2));
                        break;
                    case "-":
                        N.Push(op1 - op2);
                        break;
                    case "*":
                        N.Push(op1 * op2);
                        break;
                    case "/":
                        N.Push(op1 / op2);
                        break;
                    default:
                        break;
                }
            }
        }
    View Code

     利用堆栈进制转换

            static void Main(string[] args)
            {
                //int num = Convert.ToInt32(Console.ReadLine());
                //int numbase = Convert.ToInt32(Console.ReadLine());
                //MulBase(num,numbase);
                //Console.ReadLine();
                Console.WriteLine(Convert.ToString(4,2));
                Console.ReadLine();
            }
    
            private static void MulBase(int num, int numbase)
            {
                Stack Digit = new Stack();
                do
                {
                    Digit.Push(num%numbase);
                    num /= numbase;
                } while (num!=0);
                while (Digit.Count>0)
                {
                    Console.WriteLine(Digit.Pop());
                }
            }
    View Code
  • 相关阅读:
    依赖注入与控制反转
    Java NIO(三)
    Java NIO(二)
    Java NIO(一)
    Jetty 的基本架构
    spring IOC
    java代理
    spring AOP
    linux 添加用户并授权可以操作文件夹
    Centos6.5 yum 安装jdk1.8
  • 原文地址:https://www.cnblogs.com/futengsheng/p/7842830.html
Copyright © 2020-2023  润新知