本文整理汇总了C#中Stack.Pop方法的典型用法代码示例。如果您正苦于以下问题:C# Stack.Pop方法的具体用法?C# Stack.Pop怎么用?C# Stack.Pop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Stack
的用法示例。
在下文中一共展示了Stack.Pop方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Stack
private static int Stack(string rpn)
{
string operators = "+-*/";
Func<int, int, int>[] functions = new Func<int, int, int>[]
{
(x, y) => x + y,
(x, y) => x - y,
(x, y) => x * y,
(x, y) => x / y,
};
string[] tokens = rpn.Split(',');
Stack<int> values = new Stack<int>();
foreach(string token in tokens)
{
int index = operators.IndexOf(token);
if (index == -1)
{
values.Push(int.Parse(token));
continue;
}
int y = values.Pop();
int x = values.Pop();
values.Push(functions[index](x, y));
}
return values.Pop();
}
示例2: LargestRectangleArea
public int LargestRectangleArea(int[] height)
{
var stack = new Stack<int>();
int i = 0, maxArea = 0, topIndex = 0;
while (i < height.Length)
{
if (stack.Count == 0 || height[i] > height[stack.Peek()])
{
stack.Push(i++);
continue;
}
topIndex = stack.Pop();
maxArea = Math.Max(
maxArea,
height[topIndex] * (stack.Count == 0 ? i : i - stack.Peek() - 1));
}
while (stack.Count != 0)
{
topIndex = stack.Pop();
maxArea = Math.Max(
maxArea,
height[topIndex] * (stack.Count == 0 ? i : i - stack.Peek() - 1));
}
return maxArea;
}
示例3: Print
private static void Print(Stack<int> stack)
{
while (stack.Count > 0)
{
if (stack.Count == 1)
{
Console.Write(stack.Pop());
}
else
{
Console.Write(stack.Pop() + ",");
}
}
Console.WriteLine();
for (int i = 0; i < labyrinth.GetLength(0); i++)
{
for (int j = 0; j < labyrinth.GetLength(1); j++)
{
Console.Write("{0, 2} ", labyrinth[i, j]);
}
Console.WriteLine();
}
Console.WriteLine();
}
示例4: StackThrowsException_WhenPopOnEmpty
public void StackThrowsException_WhenPopOnEmpty()
{
var st = new Stack<int>();
st.Push(1);
var item = st.Pop();
st.Pop();
}
示例5: Format
public string Format(string format, object arg, IFormatProvider provider)
{
if (arg == null || format != "Hx")
return String.Format(parent,"{0:" + format + "}", arg);
StringBuilder hexString = new StringBuilder();
Stack<int> stack = new Stack<int>();
int popped,delta, dec, providerBase = 16;
try
{
dec = (int)arg;
}
catch (InvalidCastException ex)
{
throw new InvalidCastException("DecimalToHexProvider must recieve numerical type",ex);
}
while (dec != 0)
{
stack.Push(dec);
dec /= providerBase;
}
delta = popped = stack.Pop();
hexString.Append(hexCharacters[delta]);
while (stack.Count > 0)
{
delta = stack.Peek() - popped * providerBase;
hexString.Append(hexCharacters[delta]);
popped = stack.Pop();
}
return hexString.ToString();
}
示例6: Main
static void Main()
{
var test = new Stack<int>();
test.Push(1);
Console.WriteLine(string.Join(" ", test.ToArray()));
test.Push(2);
Console.WriteLine(string.Join(" ", test.ToArray()));
test.Push(3);
Console.WriteLine(string.Join(" ", test.ToArray()));
test.Push(4);
Console.WriteLine(string.Join(" ", test.ToArray()));
test.Push(5);
Console.WriteLine(string.Join(" ", test.ToArray()));
test.Push(6);
Console.WriteLine(string.Join(" ", test.ToArray()));
test.Push(7);
Console.WriteLine(string.Join(" ", test.ToArray()));
Console.WriteLine("The stack count: {0}", test.Count);
test.Pop();
test.Pop();
Console.WriteLine(string.Join(" ", test.ToArray()));
Console.WriteLine("The stack count: {0}", test.Count);
Console.WriteLine(test.Peek());
Console.WriteLine(string.Join(" ", test.ToArray()));
Console.WriteLine("The stack count: {0}", test.Count);
test.Clear();
Console.WriteLine(string.Join(" ", test.ToArray()));
Console.WriteLine("The stack count: {0}", test.Count);
//test.Pop(); //if uncommented this line should throw exception
}
示例7: ReadPassword
/// <summary>
/// Like System.Console.ReadLine(), only with a mask.
/// </summary>
/// <param name="mask">a <c>char</c> representing your choice of console mask</param>
/// <returns>the string the user typed in </returns>
public static string ReadPassword(char mask)
{
const int ENTER = 13, BACKSP = 8, CTRLBACKSP = 127;
int[] filtered = { 0, 27, 9, 10 /*, 32 space, if you care */ }; // const
var pass = new Stack<char>();
char chr;
while ((chr = Console.ReadKey(true).KeyChar) != ENTER) {
if (chr == BACKSP) {
if (pass.Count > 0) {
Console.Write("\b \b");
pass.Pop();
}
}
else if (chr == CTRLBACKSP) {
while (pass.Count > 0) {
Console.Write("\b \b");
pass.Pop();
}
}
else if (filtered.Count(x => chr == x) > 0) { }
else {
pass.Push(chr);
Console.Write(mask);
}
}
Console.WriteLine();
return new string(pass.Reverse().ToArray());
}
示例8: Print
private static void Print(Node<int> nodeOne)
{
var result = new Stack<int>();
var currentNode = nodeOne;
while (currentNode.Previous != null)
{
result.Push(currentNode.Value);
currentNode = currentNode.Previous;
}
result.Push(currentNode.Value);
var sb = new StringBuilder();
while (result.Count > 0)
{
if (sb.Length == 0)
{
sb.Append(result.Pop());
}
else
{
sb.Append(" - >" + result.Pop());
}
}
System.Console.WriteLine(sb.ToString());
}
示例9: Main
static void Main()
{
Console.WriteLine("Insert expression:");
string input = Console.ReadLine();
Stack<char> brackets = new Stack<char>();
brackets.Push('(');
while (brackets.Count != 0)
{
brackets.Pop();
foreach (var element in input)
{
if (element == '(')
{
brackets.Push(element);
}
else if (element == ')')
{
if (brackets.Count == 0)
{
Console.WriteLine("Incorrect expression!");
return;
}
brackets.Pop();
}
}
}
if (brackets.Count == 0)
{
Console.WriteLine("Correct expression!");
}
}
示例10: Execute
public override void Execute(IContext context, Stack<object> stack)
{
Arg1.Execute(context, stack);
var targets = (IEnumerable<IAssociations>)stack.Pop();
Arg2.Execute(context, stack);
var sourceAttributeId = Convert.ToInt32(stack.Pop());
IAttribute sourceAttribute;
if (!context.Self.TryGetAttributeById(sourceAttributeId, out sourceAttribute))
{
return;
//TODO hier sollte besser ein fehler entstehen, weil sonst fehlende dinge uebersehen werden
//oder irgednwie loggen zumindest
//TODO exception type -- code duplication
throw new Exception(string.Format("Missing attribute {0} for item {1}/{2}", sourceAttributeId, context.Self.TypeName, context.Self.TypeId));
}
foreach (var curTarget in targets)
{
curTarget.Remove(sourceAttribute);
}
}
示例11: Execute
/// <summary>
/// Execute divison operator and put result into operands stack
/// </summary>
/// <param name="operands">stack with operands</param>
/// <param name="mode">mode to operate in</param>
/// <returns>true/false if execution went well</returns>
public override bool Execute(Stack<Operand> operands, CalculatorMode mode)
{
try
{
// b / a
Operand a = operands.Pop();
Operand b = operands.Pop();
Operand c; // result
// if we are in programming mode and both operands are integers
// do a integer division
if (mode == CalculatorMode.Programming &&
a.Value == (Int32)a.Value &&
b.Value == (Int32)b.Value)
{
c = new Operand((Int32)b.Value / (Int32)a.Value);
}
else
{
c = new Operand(b.Value / a.Value);
}
operands.Push(c);
return true;
}
catch (Exception)
{
return false;
}
}
示例12: Perform
public void Perform(
CodeBlock block,
Stack<DmlObject> stack,
Dictionary<string, DmlObject> locals,
DmlObject bullet, DmlSystem system)
{
switch (property)
{
case DmlTokens.INTRINSIC_DIRECTION:
((DmlBullet)bullet.Value).Direction = (Vector2)(stack.Pop().Value);
break;
case DmlTokens.INTRINSIC_SPEED:
((DmlBullet)bullet.Value).Speed = (double)(stack.Pop().Value);
break;
case DmlTokens.INTRINSIC_COLOUR:
((DmlBullet)bullet.Value).Colour = (Color)(stack.Pop().Value);
break;
case DmlTokens.INTRINSIC_SPRITE:
string name = (string)(stack.Pop().Value);
((DmlBullet)bullet.Value).SetSprite(name.Substring(1, name.Length - 2));
break;
default:
throw new DmlSyntaxError(String.Format("Unknown intrinsic property \"{0}\"", property));
}
}
示例13: TestStack
public void TestStack()
{
var st = new Stack<int>();
for (int i = 0; i < 10; i++)
{
st.Push(i);
}
Assert.AreEqual(st.Top(), 9);
Assert.AreEqual(st.Pop(), 9);
Assert.AreEqual(st.Top(), 8);
while (!st.IsEmpty())
st.Pop();
try
{
var t = st.Top(); // should raise Exception here
Assert.IsTrue(false);
}
catch (Exception)
{
// should be here
}
}
示例14: Main
public static void Main()
{
Console.WriteLine("Write a sequence of integer numbers separated by space or comma");
Console.Write("Integers: ");
string[] userInput = Console.ReadLine().Split(' ', ',');
Stack<int> sequence = new Stack<int>();
for (int i = 0; i < userInput.Length; i++)
{
sequence.Push(int.Parse(userInput[i]));
}
Console.Write("Reversed sequence -> ");
while (sequence.Count > 0)
{
if (sequence.Count == 1)
{
Console.Write("{0}", sequence.Pop());
break;
}
Console.Write("{0}, ", sequence.Pop());
}
Console.WriteLine();
}
示例15: GetMaxHappiness
private static int GetMaxHappiness(Stack<string> atTable, List<string> free, Conditions conditions)
{
if (free.Count == 1)
{
atTable.Push(free.Single());
var table = new Table(atTable.ToList());
var totalHappiness = table.CalcTotalHappiness(conditions);
atTable.Pop();
return totalHappiness;
}
var maxHappiness = 0;
for (int i = 0; i < free.Count; i++)
{
atTable.Push(free[i]);
free.RemoveAt(i);
var happiness = GetMaxHappiness(atTable, free, conditions);
if (happiness > maxHappiness)
{
maxHappiness = happiness;
}
var person = atTable.Pop();
free.Insert(i, person);
}
return maxHappiness;
}