• 大小: 65KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-15
  • 语言: C#
  • 标签: C#  

资源简介

c#四则混合运算算法,利用二叉树中序表达式和后序表达式原理,和编译原理的词法规则,栈数据结构完成的四则混合运算

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.Regularexpressions;
using System.Collections;

namespace Calculate
{
    class Calculate
    {
        //判断是否为操作符
        public bool isOperateors(string input)
        {
            if (input == “+“ || input == “-“ || input == “*“ || input == “/“
                || input == “(“ || input == “)“ || input == “#“)
            {
                return true;
            }
            else return false;
        }

        /// 
        /// 分割表达式,并入队列
        /// 

        /// 
        /// Queue
        public Queue SplitExpress(string express)
        {
            express += “#“;
            Queue q = new Queue();
            string tempNum=string.Empty;
            char[] arryExpress = express.ToArray();
            int i = 0;
            int j = 0;
            while (j            {
                if (isOperateors(arryExpress[j].ToString()))
                {
                    if (i != j)
                    {
                        tempNum = express.Substring(i j - i);
                        q.Enqueue(tempNum);
                        q.Enqueue(arryExpress[j].ToString());
                        i = j + 1;
                    }
                    else
                    {
                        q.Enqueue(arryExpress[j].ToString());
                        i++;
                    }
                }
                j++;
            }
            //q.Enqueue(“#“);
            return q;
        }

        /// 
        /// 中序表达式转换为后序表达式
        /// 

        /// 
        /// string:后序表达式
        public List InorderToPostorder(Queue q)
        {
            List posterOrder = new List();
            Stack inOrder = new Stack();
            inOrder.Push(“#“);
            int count = q.Count;
            for (int i = 0; i < count;i++ )
            {
                string item = q.Dequeue();
                if (isOperateors(item))
                {
                    string m = inOrder.First();
                    int n = Priority.isPriority(Priority.dicOperators[inOrder.First()]
                        Priority.dicOperators[item]);
                    while (n == 1)
                    {
                        string temp = inOrder.Pop();
                        if (temp != “(“ && temp != “)“)
                        {
                            posterOrder.Add(temp);
                        }
                        n = Priority.isPriority(Priority.dicOperators[inOrder.First()]
                        Priority.dicOperators[item]);
                    }
                    if (n == 2)
                   

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2015-12-04 23:59  四则运算算法\
     目录           0  2015-12-13 01:53  四则运算算法\Calculate\
     文件         187  2015-12-04 23:59  四则运算算法\Calculate\App.config
     目录           0  2015-12-05 14:33  四则运算算法\Calculate\bin\
     目录           0  2015-12-12 20:11  四则运算算法\Calculate\bin\Debug\
     文件        8704  2015-12-12 20:53  四则运算算法\Calculate\bin\Debug\Calculate.exe
     文件         187  2015-12-04 23:59  四则运算算法\Calculate\bin\Debug\Calculate.exe.config
     文件       22016  2015-12-12 20:53  四则运算算法\Calculate\bin\Debug\Calculate.pdb
     文件       24224  2015-12-12 20:54  四则运算算法\Calculate\bin\Debug\Calculate.vshost.exe
     文件         187  2015-12-04 23:59  四则运算算法\Calculate\bin\Debug\Calculate.vshost.exe.config
     文件         490  2010-03-17 22:39  四则运算算法\Calculate\bin\Debug\Calculate.vshost.exe.manifest
     目录           0  2015-12-05 14:33  四则运算算法\Calculate\bin\Release\
     文件        5976  2015-12-13 01:53  四则运算算法\Calculate\Calculate.cs
     文件        2639  2015-12-05 17:25  四则运算算法\Calculate\Calculate.csproj
     目录           0  2015-12-04 23:59  四则运算算法\Calculate\obj\
     目录           0  2015-12-12 20:53  四则运算算法\Calculate\obj\Debug\
     文件         460  2015-12-12 20:54  四则运算算法\Calculate\obj\Debug\Calculate.csproj.FileListAbsolute.txt
     文件        1755  2015-12-05 17:25  四则运算算法\Calculate\obj\Debug\Calculate.csprojResolveAssemblyReference.cache
     文件        8704  2015-12-12 20:53  四则运算算法\Calculate\obj\Debug\Calculate.exe
     文件       22016  2015-12-12 20:53  四则运算算法\Calculate\obj\Debug\Calculate.pdb
     文件        6574  2015-12-05 14:33  四则运算算法\Calculate\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     文件           0  2015-12-04 23:59  四则运算算法\Calculate\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
     文件           0  2015-12-04 23:59  四则运算算法\Calculate\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
     文件           0  2015-12-04 23:59  四则运算算法\Calculate\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
     目录           0  2015-12-04 23:59  四则运算算法\Calculate\obj\Debug\TempPE\
     文件        1200  2015-12-05 20:56  四则运算算法\Calculate\Priority.cs
     文件        1255  2015-12-12 20:53  四则运算算法\Calculate\Program.cs
     目录           0  2015-12-04 23:59  四则运算算法\Calculate\Properties\
     文件        1360  2015-12-04 23:59  四则运算算法\Calculate\Properties\AssemblyInfo.cs
     目录           0  2015-12-04 23:57  四则运算算法\四则运算算法\
     文件         996  2015-12-05 02:38  四则运算算法\四则运算算法.sln
............此处省略17个文件信息

评论

共有 条评论