本文整理汇总了C#中Stack.OnTop方法的典型用法代码示例。如果您正苦于以下问题:C# Stack.OnTop方法的具体用法?C# Stack.OnTop怎么用?C# Stack.OnTop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Stack
的用法示例。
在下文中一共展示了Stack.OnTop方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ProcessInteger
/// <summary>
/// This encapsulates the behavior to expect when
/// the intValue being processed is an integer.
/// If * or / is at the top of the operator stack,
/// pop the value stack, pop the operator stack,
/// and apply the popped operator to t and the
/// popped number. Push the result onto the value stack.
/// Otherwise, push t onto the value stack.
/// </summary>
/// <param name="valueStack"></param>
/// <param name="intValue">The integer value we're
/// passing in to be calculated</param>
/// <param name="opStack">Passed by reference so we make sure
/// it's updated as needed</param>
public static void ProcessInteger(ref Stack<int> valueStack, int intValue, ref Stack<string> opStack)
{
//If the top of the operations stack is * or /, and assuming there's something in the value stack already
if(opStack.OnTop("*", "/"))
{
if (valueStack.Count == 0)
{
throw new ArgumentException("Value stack empty, need at least one number to multiply");
}
int value = valueStack.Pop();
switch (opStack.Pop())
{
case "*":
valueStack.Push(intValue * value);
break;
case "/":
valueStack.Push(value / intValue);
break;
}
}
else
{
valueStack.Push(intValue);
}
}
示例2: processRightParentheses
/// <summary>
/// Processes the right parentheses token.
/// If + or - is at the top of the operator stack,
/// pop the value stack twice and the operator
/// stack once. Apply the popped operator to the
/// popped numbers. Push the result onto the value
/// stack. Next, the top of the operator stack
/// should be a(. Pop it. Finally, if * or / is
/// at the top of the operator stack, pop the
/// value stack twice and the operator stack once.
/// Apply the popped operator to the popped numbers.
/// Push the result onto the value stack.
/// </summary>
/// <param name="valStack"></param>
/// <param name="opStack"></param>
public static void processRightParentheses(ref Stack<int> valStack, ref Stack<string> opStack)
{
int result, first, second;
switch (opStack.Pop())
{
case "+":
result = valStack.Pop() + valStack.Pop();
valStack.Push(result);
if (!opStack.OnTop("(", "(")) throw new ArgumentException("Missing open parentheses");
else opStack.Pop();
break;
case "-":
//Make sure this is done in reverse, since stacks pop things in reverse of how they were inserted
first = valStack.Pop();
second = valStack.Pop();
result = second - first;
valStack.Push(result);
if (!opStack.OnTop("(", "(")) throw new ArgumentException("Missing open parentheses");
else opStack.Pop();
break;
case "*":
result = valStack.Pop() * valStack.Pop();
valStack.Push(result);
break;
case "/":
//Make sure this is done in reverse, since stacks pop things in reverse of how they were inserted
first = valStack.Pop();
second = valStack.Pop();
result = second / first;
valStack.Push(result);
break;
case ")":
case "(":
throw new ArgumentException("Need an operator and not a parentheses here.");
default:
//Do nothing, the operator that was popped should've been a left paren
break;
}
}
示例3: ProcessDouble
/// <summary>
/// This encapsulates the behavior to expect when
/// the doubleValue being processed is an integer.
/// If * or / is at the top of the operator stack,
/// pop the value stack, pop the operator stack,
/// and apply the popped operator to t and the
/// popped number. Push the result onto the value stack.
/// Otherwise, push t onto the value stack.
/// </summary>
/// <param name="valueStack"></param>
/// <param name="intValue">The integer value we're
/// passing in to be calculated</param>
/// <param name="opStack">Passed by reference so we make sure
/// it's updated as needed</param>
private void ProcessDouble(ref Stack<double> valueStack, double doubleValue, ref Stack<string> opStack)
{
//If the top of the operations stack is * or /, and assuming there's something in the value stack already
if (opStack.OnTop("*", "/"))
{
if (valueStack.Count == 0)
{
throw new ArgumentException("Value stack empty, need at least one number to multiply");
}
double value = valueStack.Pop();
switch (opStack.Pop())
{
case "*":
valueStack.Push(doubleValue * value);
break;
case "/":
if (doubleValue == 0) throw new DivideByZeroException();
valueStack.Push(value / doubleValue);
break;
}
}
else
{
valueStack.Push(doubleValue);
}
}