程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 簡單的用堆棧實現的表達式計算

簡單的用堆棧實現的表達式計算

編輯:C#入門知識

[csharp] 
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; 
            } 
 
        } 
    } 

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;
            }

        }
    }
}


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved