【代码向】简易计算器逻辑

来源:互联网 发布:知柏地黄丸主治 编辑:程序博客网 时间:2024/05/19 13:23
    class Program    {        static void Main(string[] args)        {            Queue<string> OperationQueue = new Queue<string>();            {                string baseMove = Console.ReadLine();                //string baseMove = "1+2+1";                Stack<char> OperatorStack = new Stack<char>();                string currentCount = "";                foreach (char c in baseMove)                {                    if (char.IsDigit(c)                        || c == '.')                        currentCount += c;                    else                    {                        EnCount(OperationQueue, ref currentCount);                        if (OperatorStack.Count == 0)                            OperatorStack.Push(c);                        else                        {                            //取出运算符优先的运算符                            while (OperatorStack.Count != 0 &&                                (OperatorStack.Peek() == '*' ||                                OperatorStack.Peek() == '/') &&                                (c == '+' || c == '-'))                                OperationQueue.Enqueue(OperatorStack.Pop().ToString());                            //取出右括号至左括号之间的运算符                            if (c == ')')                            {                                while (OperatorStack.Peek() != '(')                                    OperationQueue.Enqueue(OperatorStack.Pop().ToString());                                OperatorStack.Pop();                            }                            else if (c == ')')//添加中文括号支持                            {                                while (OperatorStack.Peek() != '(')                                    OperationQueue.Enqueue(OperatorStack.Pop().ToString());                                OperatorStack.Pop();                            }                            else                                OperatorStack.Push(c);                        }//运算栈中已有数据时                    }//非数字的时候                }//遍历运算字符串                EnCount(OperationQueue, ref currentCount);                while (OperatorStack.Count != 0)                    OperationQueue.Enqueue(OperatorStack.Pop().ToString());            }//处理运算字符串            {                Stack<double> CountStack = new Stack<double>();                while (OperationQueue.Count != 0)                {                    IO io;                    string deQueue = OperationQueue.Dequeue();                    if (OF.GO(deQueue, out io))                    {                        double count2 = CountStack.Pop();                        double count1 = CountStack.Pop();                        CountStack.Push(io.Operator(count1, count2));                    }                    else                        CountStack.Push(double.Parse(deQueue));                }                Console.WriteLine("Result = " + CountStack.Pop());            }            Console.Read();        }        private static void EnCount(Queue<string> OperationQueue, ref string currentCount)        {            if (currentCount != "")//防止括号的特殊情况            {                OperationQueue.Enqueue(currentCount);                currentCount = "";            }        }    }//简单工厂模式的运算逻辑,便于日后拓展namespace Structure.Operator{    public interface IO//IOperator    {        double Operator(double d1, double d2);    }}namespace Structure.Operator{    public class AO : IO    {        public double Operator(double d1, double d2)        {            return d1 + d2;        }    }}namespace Structure.Operator{    public class SO : IO    {        public double Operator(double d1, double d2)        {            return d1 - d2;        }    }}namespace Structure.Operator{    public class MO : IO    {        public double Operator(double d1, double d2)        {            return d1 * d2;        }    }}namespace Structure.Operator{    public class DO : IO    {        public double Operator(double d1, double d2)        {            return d1 / d2;        }    }}namespace Structure.Operator{    public static class OF    {        public static bool GO(string tag, out IO io)        {            switch (tag)            {                case "+":                    io = new AO();                    break;                case "-":                    io = new SO();                    break;                case "*":                    io = new MO();                    break;                case "/":                    io = new DO();                    break;                default:                    io = null;                    return false;            }            return true;        }    }}
算法为将中缀表达式转换为后缀表达式然后计算
0 0
原创粉丝点击