zoukankan      html  css  js  c++  java
  • 简单的用堆栈实现的表达式计算

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    
    namespace 简单表达式求解
    {
        class Program
        {
            static void Main(string[] args)
            {
                Stack numbs = new Stack();
                Stack ops = new Stack();
    
                String expression = "5 + 10 - 15 * 20 * 2";
    
                Calculate(numbs, ops, expression);
    
                Console.WriteLine(numbs.Pop());
            }
    
            public static bool IsNumeric(string input)
            {
                bool flag = true;
    
                string pattern = "^\\d+$";
    
                Regex validate = new Regex(pattern);
    
                if (!validate.IsMatch(input))
                {
                    flag = false;
                }
    
                return flag;
            }
    
            public static void Calculate(Stack n, Stack o, string exp)
            {
                string ch, token = "";
    
                for (int p = 0; p < exp.Length; p++)
                {
                    ch = exp.Substring(p, 1);
                    if (IsNumeric(ch))
                    {
                        token += ch;
                    }
    
                    if (ch == " " || p == (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);
                    }
                }
            }
    
            public static void Compute(Stack n, Stack o)
            {
                int oper1, oper2;
    
                string oper;
    
                oper1 = Convert.ToInt32(n.Pop());
                oper2 = Convert.ToInt32(n.Pop());
    
                oper = Convert.ToString(o.Pop());
                switch (oper)
                {
                    case "+":
                        n.Push(oper1 + oper2);
                        break;
                    case "-":
                        n.Push(oper1 - oper2);
                        break;
                    case "*":
                        n.Push(oper1 * oper2);
                        break;
                    case "/":
                        n.Push(oper1 / oper2);
                        break;
                }
    
            }
        }
    }
    

  • 相关阅读:
    PAT B1027 打印沙漏 (20 分)
    PAT B1025 反转链表 (25 分)
    PAT B1022 D进制的A+B (20 分)
    PAT B1018 锤子剪刀布 (20 分)
    PAT B1017 A除以B (20 分)
    PAT B1015 德才论 (25 分)
    PAT B1013 数素数 (20 分)
    PAT B1010 一元多项式求导 (25 分)
    HDU 1405 The Last Practice
    HDU 1165 Eddy's research II
  • 原文地址:https://www.cnblogs.com/wjchang/p/3671521.html
Copyright © 2011-2022 走看看