コード例 #1
0
        static void Main(string[] args)
        {
            Stack s = new Stack();

            Console.WriteLine("enter your input");
            string input = Console.ReadLine();

            string[] arr = input.Split(' ');
            foreach (string token in arr)
            {
                int val;
                if (int.TryParse(token, out val))
                {
                    s.push(val);
                }
                else
                {
                    int rhs = s.pop();
                    int lhs = s.pop();
                    switch (token)
                    {
                    case "+":
                        s.push(lhs + rhs);
                        break;

                    case "-":
                        s.push(Math.Abs(lhs - rhs));
                        break;

                    case "*":
                        s.push(Math.Abs(lhs * rhs));
                        break;

                    case "/":
                        s.push(Math.Abs(lhs / rhs));
                        break;

                    default:
                        break;
                    }
                }
            }
            Console.WriteLine("the result is\t" + s.pop());
        }
コード例 #2
0
        // Driver code
        public static void Main(String[] args)
        {
            Stack s = new Stack();

            s.push(1);
            s.push(2);
            s.push(3);
            Console.WriteLine("current size: " + s.size());
            Console.WriteLine(s.top());
            s.pop();
            Console.WriteLine(s.top());
            s.pop();
            Console.WriteLine(s.top());
            Console.WriteLine("current size: " + s.size());

            /*
             * Output :
             * current size: 3
             * 3
             * 2
             * 1
             * current size: 1
             */
        }
コード例 #3
0
        public static void Reverse_Sentence()
        {
            int   size = Convert.ToInt32(Console.ReadLine());
            Stack s    = new Stack();

            while (size > 0)
            {
                int      cnt   = 0;
                string[] input = Console.ReadLine().Trim().Split(' ');
                for (int i = 0; i < input.Length; i++)
                {
                    s.push(input[i]);
                    cnt++;
                }
                while (cnt > 0)
                {
                    s.pop();
                    cnt--;
                }
                Console.Write('\n');
            }
        }