本文整理汇总了C#中System.Collections.Stack.HasItems方法的典型用法代码示例。如果您正苦于以下问题:C# Stack.HasItems方法的具体用法?C# Stack.HasItems怎么用?C# Stack.HasItems使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Collections.Stack
的用法示例。
在下文中一共展示了Stack.HasItems方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InfixToPostfix
/// <summary>
/// Transform an infix expression string into
/// a postfix expression string
/// </summary>
public Stack<string> InfixToPostfix(string expression)
{
var operands = new Stack<Token>();
var postfix = new Stack<Token>();
foreach (Token token in expression.Tokens()) {
if (token.IsEmpty) continue;
switch (token.Class) {
case TokenClass.Name:
case TokenClass.Operand:
#region Test for uniary operator
if (operands.HasItems()
&& operands.Peek().Class == TokenClass.UniaryPrefix) {
switch (operands.Peek().Value) {
case "-":
// turn a uniary minus and operand into a negative operand
operands.Pop();
postfix.Push(token);
postfix.Push(new Token("-1"));
postfix.Push(new Token("*"));
break;
case "+":
// no change to operand, remove uniary
postfix.Push(token);
operands.Pop();
break;
default:
throw new Exception("Unexpected operator");
}
#endregion
} else {
postfix.Push(token);
}
break;
case TokenClass.UniaryPostfix:
operands.Push(token);
break;
case TokenClass.UniaryPrefix:
case TokenClass.Function:
case TokenClass.OpenBracket:
#region Test for uniary operator
if (operands.HasItems()
&& operands.Peek().Class == TokenClass.UniaryPrefix) {
switch (operands.Peek().Value) {
case "-":
// change value, which will get picked up on bracket close
token.Value = "-";
operands.Pop(); // remove uniary
operands.Push(token); // push bracket
break;
case "+":
operands.Pop(); // remove uniary
operands.Push(token); // push bracket
break;
default:
throw new Exception("Unexpected operator");
}
#endregion
} else {
operands.Push(token);
}
break;
case TokenClass.ArgumentSeperator:
while (operands.HasItems()
&& operands.Peek().Class != TokenClass.OpenBracket) {
if (operands.Count < 1) throw new Exception("Argument seperator outside of argument list");
postfix.Push(operands.Pop());
}
break;
case TokenClass.BinaryOperator:
while (operands.HasItems()
&& token.ShouldDisplace(operands.Peek())) { // compare precedence
postfix.Push(operands.Pop());
}
operands.Push(token);
break;
case TokenClass.CloseBracket:
while (operands.HasItems()
&& operands.Peek().Class != TokenClass.OpenBracket) { // add inner bracket contents
postfix.Push(operands.Pop());
}
#region Check for previously caught uniary minus bracket "-(...)"
if (operands.HasItems()) {
Token ob = operands.Pop(); // check open bracket
if (ob.Value == "-") { // invert value
postfix.Push(new Token("-1"));
postfix.Push(new Token("*"));
}
}
#endregion
if (operands.HasItems()
//.........这里部分代码省略.........