rpn计算器功能问题?

时间:2018-10-17 22:09:34

标签: c# rpn

试图在C#中制作一个rpn计算器,不确定为什么它不起作用。我的程序或类文件中有错误吗?

class Program
    {
        static void Main(string[] args)
        {
            IntStack mystack = new IntStack();

            mystack.Push(10);
            System.Console.WriteLine(mystack.Pop());

            mystack.Push(20);
            mystack.Push(30);
            mystack.Push(40);

            System.Console.WriteLine(mystack.Pop());
            System.Console.WriteLine(mystack.Pop());
            System.Console.WriteLine(mystack.Pop());


            System.Console.ReadKey();
        }
     }

1 个答案:

答案 0 :(得分:-1)

尝试以下修改:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {

        static void Main(string[] args)
        {

            IntStack mystack = new IntStack();

            mystack.Push(10);
            System.Console.WriteLine(mystack.Pop());

            mystack.Push(20);
            mystack.Push(30);
            mystack.Push(40);

            System.Console.WriteLine(mystack.Pop());
            System.Console.WriteLine(mystack.Pop());
            System.Console.WriteLine(mystack.Pop());


            System.Console.ReadKey();

        }


    }
    public class IntStack
    {
        private List<int> stack = new List<int>();

        public int? Pop()
        {
            if (stack.Count == 0) return null;

            int value = stack[0];
            stack.RemoveAt(0);
            return value;
        }
        public void Push(int value)
        {
            stack.Insert(0, value);
        }

    }

}