當前位置: 首頁>>代碼示例>>C#>>正文


C# Language.DefaultIfEmpty方法代碼示例

本文整理匯總了C#中System.Language.DefaultIfEmpty方法的典型用法代碼示例。如果您正苦於以下問題:C# Language.DefaultIfEmpty方法的具體用法?C# Language.DefaultIfEmpty怎麽用?C# Language.DefaultIfEmpty使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Language的用法示例。


在下文中一共展示了Language.DefaultIfEmpty方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: ParseExpression

        /// <summary>
        /// Parses a given expression into a graph.
        /// </summary>
        /// <returns>The built graph.</returns>
        /// <param name="expression">The expression to create the graph from.</param>
        /// <param name="initGraph">The graph to append too. Used for recursion.</param>
        public Graph ParseExpression(string expression, Graph initGraph = null)
        {
            var lang = new Language ();
            var ops = new Operations ();
            expression = expression.Trim ();
            var opStack = new Stack<Word>();

            foreach (char letter in expression) {
                //find the language mapping
                var inLanguage = lang
                    .DefaultIfEmpty(null)
                    .FirstOrDefault (w => w.Letter == letter);

                //find the operation mapping
                var inOps = ops
                    .DefaultIfEmpty(null)
                    .FirstOrDefault(w => w.Letter == letter);

                //not in the language or operators
                if(inLanguage == null && inOps == null){
                    throw new InvalidLanguageException("Invalid input: " + letter);
                }

                //it is valid language input, so add it to the list.
                if(inLanguage != null){
                    opStack.Push(inLanguage);
                    continue;
                }
                else{
                    //not a letter, so must be an operation
                    //verify that it has the correct arg count for that type of operation.
                    if (opStack.Count != inOps.MinArgCount && initGraph == null) {
                        throw new InvalidLanguageException (
                            string.Format(
                                "Unbalanced arguement count for operation (expected {0}, but got {1}.",
                                inOps.MinArgCount, opStack.Count
                            ));
                    }
                    //operation, so stop, and process graph
                    initGraph = ProcessOperation(initGraph, inOps, opStack);
                }
            }

            //expression was given with no operation, so error out.
            if (opStack.Count > 0) {
                throw new InvalidLanguageException ("No operation was specified in the given expression.");
            }

            //once done with graph, renumber the states.
            initGraph.RenumberStates ();

            //return the completed graph.
            return initGraph;
        }
開發者ID:tvand7093,項目名稱:Xpressional,代碼行數:60,代碼來源:InputManager.cs


注:本文中的System.Language.DefaultIfEmpty方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。