第二の個人的な仕事:使い慣れたツールを使用して

初期化とクローニング倉庫、プロジェクトを作成します

  1. 初期化とクローン化された倉庫

    40184@Rongz MINGW32 ~
    $ cd e:
    
    40184@Rongz MINGW32 /e
    $ cd RongGit/
    
    40184@Rongz MINGW32 /e/RongGit
    $ git init
    Initialized empty Git repository in E:/RongGit/.git/
    
    40184@Rongz MINGW32 /e/RongGit (master)
    $ git clone https://github.com/Cherish599/AchaoCalculator.git
    Cloning into 'AchaoCalculator'...
    remote: Enumerating objects: 6, done.
    remote: Counting objects: 100% (6/6), done.
    remote: Compressing objects: 100% (4/4), done.
    remote: Total 6 (delta 0), reused 4 (delta 0), pack-reused 0
    Unpacking objects: 100% (6/6), done.
    
    40184@Rongz MINGW32 /e/RongGit (master)
  2. プロジェクトを作成します。

コーディング

アイテムのコーディング:

using System;
using System.Collections.Generic;
using System.Text;

/// <summary>
/// 四则运算计算器
/// 
/// @author Rongze Zhao
/// @date 2019-09-15 22:36
/// </summary>
namespace Calculator
{
    public class Calculator
    {
        private static string[] op = new string[] { "+", "-", "*", "/" };

        /// <summary>
        /// 表达式整数范围 1~n </summary>
        private static int maxNum = 5;
        /// <summary>
        /// 表达式整数个数 1~n </summary>
        private static int maxNumCount = 4;
        private static Random random = new Random();

        /// <summary>
        /// 优先级判断器
        /// </summary>
        /// <param name="c"> 符号 </param>
        /// <returns> "+","-"     1
        ///         "*","/"     2
        ///         other      -1 </returns>
        internal static int precedence(string c)
        {
            switch (c)
            {
                case "+":
                case "-":
                    return 1;
                case "*":
                case "/":
                    return 2;
            }
            return -1;
        }

        public static void Main(string[] args)
        {
            Result result = formulaMaker();
            Queue<string> postfixQueue = changeToPostfix(result.FormulaQueue);
            int res = calculate(postfixQueue);
            Console.WriteLine("formula:" + result.infixFormula);
            Console.WriteLine("result:" + res);
            Console.ReadLine();
        }

        /// <summary>
        /// 测试函数
        /// 
        /// 用于JavaScript eval函数调用测试
        /// </summary>
        public static Result test()
        {
            Result result = formulaMaker();
            Queue<string> postfixStack = changeToPostfix(result.FormulaQueue);
            result.setResult(calculate(postfixStack));
            return result;
        }


        /// <summary>
        /// 表达式生成器
        /// </summary>
        /// <returns> 中缀表达式队列 </returns>
        public static Result formulaMaker()
        {
            Queue<string> queue = new Queue<string>();
            StringBuilder sb = new StringBuilder();
            int num;
            for (int i = maxNumCount - 1; i > 0; i--)
            {
                num = random.Next(maxNum) + 1;
                string option = op[random.Next(3)];
                queue.Enqueue(num.ToString());
                queue.Enqueue(option);
                sb.Append(num).Append(option);
            }
            num = random.Next(maxNum) + 1;
            queue.Enqueue(num.ToString());
            sb.Append(num);
            return new Result(sb, queue);
        }

        /// <summary>
        /// 中缀表达式转后缀表达
        /// </summary>
        /// <param name="queue"> 中缀表达式队列 </param>
        /// <returns> 后缀表达式队列 </returns>
        public static Queue<string> changeToPostfix(Queue<string> queue)
        {
            Queue<string> queue2 = new Queue<string>(); // 保存操作数
            Stack<string> stack2 = new Stack<string>(); // 保存操作符
            while (queue.Count > 0)
            {
                string symbol = queue.Dequeue();
                if (precedence(symbol) > 0)
                { //检查symbol是否是一个操作数
                    while (stack2.Count > 0 && precedence(stack2.Peek()) >= precedence(symbol))
                    {
                        queue2.Enqueue(stack2.Pop());
                    }
                    stack2.Push(symbol);
                }
                else
                { //symbol 不是一个操作数
                    queue2.Enqueue(symbol);
                }
            }
            while (stack2.Count > 0)
            {
                queue2.Enqueue(stack2.Pop());
            }
            return queue2;
        }

        /// <summary>
        /// 计算函数
        /// 用于计算后缀表达式的值
        /// </summary>
        /// <param name="queue"> 后缀表达式堆栈 </param>
        /// <returns> 计算结果 </returns>
        public static int calculate(Queue<string> queue)
        {
            Stack<string> stack1 = new Stack<string>(); // 保存操作数
            Stack<string> stack2 = new Stack<string>(); // 保存操作符
            foreach (string symbol in queue)
            {
                if (!symbol.Equals("+") && !symbol.Equals("-") && !symbol.Equals("/") && !symbol.Equals("*"))
                {
                    stack1.Push(symbol);
                }
                else
                {
                    int a = int.Parse(stack1.Pop()), b = int.Parse(stack1.Pop());
                    switch (symbol)
                    {
                        case "+":
                            stack1.Push((a + b).ToString());
                            break;
                        case "-":
                            stack1.Push((b - a).ToString());
                            break;
                        case "*":
                            stack1.Push((a * b).ToString());
                            break;
                        default:
                            stack1.Push((a / b).ToString());
                            break;
                    }
                }
            }
            return int.Parse(stack1.Pop());
        }

        /// <summary>
        /// 程序关键数据封装类
        /// 用于输出和测试
        /// </summary>
        public class Result
        {
            /// <summary>
            /// 前缀表达式字符串 </summary>
            internal StringBuilder infixFormula;
            /// <summary>
            /// 后缀表达式队列 </summary>
            internal Queue<string> formulaQueue;

            /// <summary>
            /// 计算结果 </summary>
            internal int result;

            public Result()
            {
            }

            public Result(StringBuilder stringBuilder, Queue<string> formulaQueue)
            {
                this.infixFormula = stringBuilder;
                this.formulaQueue = formulaQueue;
            }

            public virtual StringBuilder StringBuilder
            {
                get
                {
                    return infixFormula;
                }
                set
                {
                    this.infixFormula = value;
                }
            }


            public virtual Queue<string> FormulaQueue
            {
                get
                {
                    return formulaQueue;
                }
                set
                {
                    this.formulaQueue = value;
                }
            }


            public virtual int getResult()
            {
                return result;
            }

            public virtual void setResult(int result)
            {
                this.result = result;
            }
        }

    }
}

結果の主な機能:

テストを記録します:埋め込まれたJavaScriptエンジンのテストのJavaのバージョンを記述します。1000のランダムに生成された表現、その結果、自分のコードの実行を書くeval関数を使用して計算した結果と同じである、すべきでないバグ:

ユニットテスト

テストプロジェクトを作成します。

参照を追加します。

テストを実行:結果と一致

生産性ツール

主な機能を変更します。

public static void Main(string[] args)
{
    for (int i = 0; i < 10000000; i++) {
        Result result = formulaMaker();
        Queue<string> postfixQueue = changeToPostfix(result.FormulaQueue);
        int res = calculate(postfixQueue);
        Console.WriteLine("formula:" + result.infixFormula);
        Console.WriteLine("result:" + res);
    }
}

効率分析レポート:

詳細なレポートを作成します。

コードを送信

40184@Rongz MINGW32 /e/RongGit (master)
$ git add AchaoCalculator
warning: adding embedded git repository: AchaoCalculator
hint: You've added another git repository inside your current repository.
hint: Clones of the outer repository will not contain the contents of
hint: the embedded repository and will not know how to obtain it.
hint: If you meant to add a submodule, use:
hint:
hint:   git submodule add <url> AchaoCalculator
hint:
hint: If you added this path by mistake, you can remove it from the
hint: index with:
hint:
hint:   git rm --cached AchaoCalculator
hint:
hint: See "git help submodule" for more information.

40184@Rongz MINGW32 /e/RongGit (master)
$ git commit -m "release version"
[master (root-commit) 31ec8d9] release version
 1 file changed, 1 insertion(+)
 create mode 160000 AchaoCalculator

40184@Rongz MINGW32 /e/RongGit (master)
$ cd AchaoCalculator/

独自のリポジトリを作成します(少し)

何のアクセスはありません上記の二つの文の実装は、Gitは403を要求されます。この時、

Gitはあなたが誰であるかを、そのgitのに公開鍵を与えていないので、具体的な構成は、次のブログを参照してください。

https://www.cnblogs.com/wmr95/p/7852832.html

何の問題を提出しなかった後:

40184@Rongz MINGW32 /e/RongGit/AchaoCalculator (master)
$ git push -u origin master
Counting objects: 17, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (14/14), done.
Writing objects: 100% (17/17), 6.70 KiB | 190.00 KiB/s, done.
Total 17 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To github.com:Zrz458/ZrzCalculator.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

おすすめ

転載: www.cnblogs.com/Rongze-blog/p/11527756.html
おすすめ