zoukankan      html  css  js  c++  java
  • C# 10以内的加减乘除

        class Program
        {
            static void Main(string[] args)
            {
                var source = "5+2*2-3*4/2";
                var result = Cal(source);
                Console.WriteLine(result);
            }
    
            static int Cal(string str)
            {
                var numStack = new Stack<int>();//存储数字的栈
                var opStack = new Stack<char>();//存储操作符的栈
                int result;
                foreach (var c in str)
                {
                    if (IsNumeric(c, out var i))
                    {
                        numStack.Push(i);
                        continue;
                    }
    
                    while (opStack.Count > 0)
                    {
                        var f = Check(c, opStack);
                        if (f)
                        {
                            break;
                        }
                        result = Cal(numStack, opStack);
                        numStack.Push(result);
                    }
                    opStack.Push(c);
                }
                result = Cal(numStack, opStack);
                numStack.Push(result);
                return result;
            }
    
            /// <summary>
            /// 判断是否是数字
            /// </summary>
            /// <param name="c"></param>
            /// <param name="i"></param>
            /// <returns></returns>
            static bool IsNumeric(char c, out int i)
            {
                var pattern = @"^d+$";
                var rgx = new Regex(pattern);
                var flag = rgx.IsMatch(c.ToString());
                i = 0;
                if (flag)
                {
                    i = Convert.ToInt32(c.ToString());
                }
                return flag;
            }
    
            /// <summary>
            /// 判断当前操作符的优先级是否高于栈顶的操作符
            /// </summary>
            /// <param name="c"></param>
            /// <param name="opStack"></param>
            /// <returns></returns>
            static bool Check(char c, Stack<char> opStack)
            {
                char op = opStack.Peek();
                if (op == '*' || op == '/')
                {
                    return false;
                }
    
                if (c == '*' || c == '/')
                {
                    return true;
                }
                return false;
            }
    
            /// <summary>
            /// 计算
            /// </summary>
            /// <param name="numStack"></param>
            /// <param name="opStack"></param>
            /// <returns></returns>
            static int Cal(Stack<int> numStack, Stack<char> opStack)
            {
                var num1 = numStack.Pop();
                var num2 = numStack.Pop();
                char op = opStack.Pop();
                switch (op)
                {
                    case '+':
                        return num2 + num1;
                    case '-':
                        return num2 - num1;
                    case '*':
                        return num2 * num1;
                    case '/':
                        return num2 / num1;
                    default:
                        throw new Exception("not valid op");
                }
            }
        }
  • 相关阅读:
    mysql_config 问题
    软考倒计时3天
    软考倒计时5天
    Pdf 解密后复制文字乱码
    软考倒计时7天:题目书中的易混点
    应急储备和管理储备
    软考倒计时9天:100个主要知识点
    软考倒计时10天
    软考倒计时15天
    软考倒计时18天
  • 原文地址:https://www.cnblogs.com/refuge/p/13340778.html
Copyright © 2011-2022 走看看