本文整理汇总了C#中System.Collections.Stack.Pop方法的典型用法代码示例。如果您正苦于以下问题:C# Stack.Pop方法的具体用法?C# Stack.Pop怎么用?C# Stack.Pop使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Collections.Stack
的用法示例。
在下文中一共展示了Stack.Pop方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
private static void Main()
{
string inputSentence = SampleSentence;
string splitPattern = @"(\s+|\s*\,\s*|\s*\-\s*|\s*\!|\s*\.)";
string[] elements = Regex.Split(inputSentence, splitPattern);
Stack words = new Stack();
Queue separators = new Queue();
StringBuilder result = new StringBuilder();
foreach (var element in elements)
{
if (Regex.IsMatch(element, splitPattern))
{
separators.Enqueue(element);
}
else if (Regex.IsMatch(element, @"\S"))
{
words.Push(element);
}
}
while (words.Count > 0)
{
result.Append(words.Pop());
result.Append(separators.Dequeue());
}
Console.WriteLine(result);
}
示例2: IntAdd
//[int, int] ==> [int]
public Stack IntAdd(Stack s)
{
var x = (int) s.Pop();
var y = (int) s.Pop();
s.Push(x + y);
return s;
}
示例3: getLine
/// <summary>
/// Returns a line composed of the words in the stack that would
/// fit into the label.
/// The line is returned when the next word wouldn't fit or the last poped
/// word was a newline.
///
/// The stack is requiered to have all characters and all whitespaces in
/// separate elements. The labels text must be blank.
/// </summary>
public static string getLine( Stack<string> words, UILabel targetLabel, bool doLinePadding ){
string line = "";
string currWord = "";
Vector2 labelSize = new Vector2( targetLabel.width, targetLabel.height );
Vector2 textSize = new Vector2();
targetLabel.UpdateNGUIText();
//Add next word to the current line as long as the line would fit in the label
//and not cause a newline.
while( words.Count > 0 ){
currWord = words.Peek();
textSize = NGUIText.CalculatePrintedSize(line + currWord);
if( textSize.y > labelSize.y ){
//Check if the current word is a whitespace. If it is, remove it
if( currWord.Trim() == string.Empty ){
words.Pop();
line.Trim();
}
textSize = NGUIText.CalculatePrintedSize(line + " ");
while( textSize.y < labelSize.y && doLinePadding ){
line += " ";
textSize = NGUIText.CalculatePrintedSize(line + " ");
}
return line;
}
line += words.Pop();
}
return line;
}
示例4: ZnajdzOtoczke
public void ZnajdzOtoczke(List<Punkt> lista)
{
List<Punkt> listaTmp = new List<Punkt>();
Stack stos = new Stack();
stos.Push(lista[0]);
stos.Push(lista[1]);
stos.Push(lista[2]);
listaTmp.Add(lista[0]);
listaTmp.Add(lista[1]);
listaTmp.Add(lista[2]);
for (int i = 3; i < lista.Count; i++)
{
int j = i;
while (ObliczDet(listaTmp[stos.Count - 2], listaTmp[stos.Count - 1], lista[i]) < 0)
{
//Console.WriteLine(ObliczDet(lista[j - 2], lista[j - 1], lista[i]));
stos.Pop();
listaTmp.RemoveRange(stos.Count, 1);
}
stos.Push(lista[i]);
listaTmp.Add(lista[i]);
}
int ileWierz = stos.Count;
for (int i = 0; i < ileWierz; i++)
{
wierzcholki.Add((Punkt)stos.Pop());
}
wierzcholki.Reverse();
}
示例5: QuickSortStackedInsertion
/// <summary>
/// Быстрая сортировка - нерекурсивная реализация со вспомогательной сортировкой вставками
/// </summary>
public static void QuickSortStackedInsertion(List<Item> data, int l, int r)
{
Stack<int> s = new Stack<int>();
s.Push(r);
s.Push(l);
int M = 9;
while (s.Count != 0)
{
l = s.Pop();
r = s.Pop();
if (r - 1 < M)
{
continue;
}
int i = Partition(data, l, r);
if (i - 1 < r - i && r > l)
{
s.Push(i - 1);
s.Push(l);
s.Push(r);
s.Push(i + 1);
}
else if (r > l)
{
s.Push(r);
s.Push(i + 1);
s.Push(i - 1);
s.Push(l);
}
}
Insertion.InsertionSortLv3(data, l, r);
}
示例6: Calculate
public int Calculate(string rpn)
{
string[] parts = rpn.Split(' ');
var stack = new Stack<int>();
foreach (string part in parts)
{
int digit;
bool isDigit = int.TryParse(part, out digit);
if (isDigit)
{
stack.Push(digit);
continue;
}
bool isOperator = Array.IndexOf(operators, part) >= 0;
if (isOperator)
{
int digit2 = stack.Pop();
int digit1 = stack.Pop();
int value = InternalCalcul(digit1, digit2, part);
stack.Push(value);
}
}
return stack.Pop();
}
示例7: IntMult
//[int, int] ==> [int]
public Stack IntMult(Stack s)
{
var x = (int) s.Pop();
var y = (int) s.Pop();
s.Push(x*y);
return s;
}
示例8: Main
static void Main(string[] args)
{
Stack ast = new Stack();
ast.Push("Item 1");
ast.Push("Item 2");
ast.Push("Item 3");
ast.Push("Item 4");
Console.WriteLine("Count: {0}", ast.Count);
PrintValues(ast);
// Peek item but do not remove
object item = ast.Peek();
Console.WriteLine("Peek: {0}", item);
PrintValues(ast);
// Peek and cast but do not remove
string itemString = ast.Peek() as string; // fast cast
Console.WriteLine("Peek: {0}", item);
PrintValues(ast);
// Contains
Boolean contains = ast.Contains("Item 3");
Console.WriteLine("Contains: {0}", contains);
// Remove items
object item4 = ast.Pop();
object item3 = ast.Pop();
Console.WriteLine("Pop: {0} {1}", item4, item3);
PrintValues(ast);
Console.WriteLine("Count: {0}", ast.Count);
// no TrimToSize method
}
示例9: Expand
internal static TreeViewItemViewModel Expand(TreeViewItemModelItemViewModel rootTreeViewItem, ModelItem modelItemToExpandTo)
{
Fx.Assert(modelItemToExpandTo != null && rootTreeViewItem != null, "rootTreeViewItem and modelItemToExpand should not have null value");
// ModelItems with HidePropertyInOutlineViewAttribute are invisible in the designerTree.
if (ExtensibilityAccessor.GetAttribute<HidePropertyInOutlineViewAttribute>(modelItemToExpandTo) != null)
{
return null;
}
Stack pathStack = new Stack();
TreeViewItemViewModel itemToBeSelected = null;
if (GetExpandingPath(modelItemToExpandTo, pathStack, new HashSet<ModelItem>()))
{
// If the root of modelItemToExpandTo differs from the root of the designerTree, it means modelItemToExpandTo doesn't belong to the designerTree.
if (pathStack.Pop() != rootTreeViewItem.VisualValue)
{
return null;
}
object item = null;
TreeViewItemViewModel treeViewItem = rootTreeViewItem;
TreeViewItemViewModel tempTreeViewItem = rootTreeViewItem;
// Using the path to the root, expand the corresponding tree node. Ignore the items which is not visible on the designerTree.
while (pathStack.Count > 0)
{
if (tempTreeViewItem != null)
{
treeViewItem = tempTreeViewItem;
treeViewItem.IsExpanded = true;
}
item = pathStack.Pop();
tempTreeViewItem = (from child in treeViewItem.Children
where (child is TreeViewItemModelItemViewModel && ((TreeViewItemModelItemViewModel)child).VisualValue == item as ModelItem)
|| (child is TreeViewItemModelPropertyViewModel && ((TreeViewItemModelPropertyViewModel)child).VisualValue == item as ModelProperty)
|| (child is TreeViewItemKeyValuePairModelItemViewModel && ((TreeViewItemKeyValuePairModelItemViewModel)child).VisualValue.Value == item as ModelItem)
select child).FirstOrDefault();
// For TreeViewItemKeyValuePairModelItemViewModel, its path to the children is very complicated.
// Take Switch as example: Switch(ModelItem) -> Cases(ModelProperty) -> KeyDictionaryCollection(ModelItem) -> ItemsCollection(ModelProperty) -> ModelItemKeyValuePair<T, Activity>(ModelItem) -> Value(ModelProperty) -> Children
// All the path nodes except Switch and Children are invisible and can be ignored, the child node in the path is used twice to search for TreeViewItemKeyValuePairModelItemViewModel and its children in designerTree.
if (tempTreeViewItem is TreeViewItemKeyValuePairModelItemViewModel)
{
// For further searching
pathStack.Push(item);
}
if (pathStack.Count == 0)
{
itemToBeSelected = tempTreeViewItem;
}
}
}
return itemToBeSelected;
}
示例10: GetLocalFairCounterExample
/// <summary>
/// Get local fair counter example
/// </summary>
/// <param name="newSCC"></param>
/// <param name="localCallStack"></param>
/// <param name="isDeadLock"></param>
public void GetLocalFairCounterExample(Stack<LocalPair> localCallStack, List<ConfigurationBase> cycle, bool isDeadLock)
{
localCallStack.Pop();
int traceLen = localCallStack.Count;
List<ConfigurationBase> trace = new List<ConfigurationBase>(traceLen);
while (localCallStack.Count > 0)
{
LocalPair tmp = localCallStack.Pop();
trace.Insert(0, tmp.configuration);
if (tmp.configuration.Event == Constants.INITIAL_EVENT)
{
break;
}
}
lock (globalCounterExampleLocker)
{
if (isGlobalStop)
{
return;
}
if (!isDeadLock)
{
finalLoopIndex = trace.Count;
}
trace.AddRange(cycle);
finalTrace = trace;
isGlobalStop = true;
}
}
示例11: CreateSubTree
///
/// Tạo một cây nhị phân 3 node với node gốc là toán tử, 2 node lá là toán hạng
///
/// <param name="node" />
/// <param name="opStack" />
/// <param name="nodeStack" />
private void CreateSubTree(Stack<BinaryTreeNode> opStack, Stack<BinaryTreeNode> nodeStack)
{
BinaryTreeNode node = opStack.Pop();
node.RightChild = nodeStack.Pop();
node.LeftChild = nodeStack.Pop();
nodeStack.Push(node);
}
示例12: Main
static void Main(string[] args)
{
int T = int.Parse(Console.ReadLine());
for (int i = 0; i < T; i++)
{
char[] line = Console.ReadLine().ToCharArray();
Stack<char> ops = new Stack<char>();
char opFound;
StringBuilder postFix = new StringBuilder();
for (int j = 0; j < line.Length; j++)
{
char c =line[j];
if (c != '(' && c != ')' && c != '*' && c != '+' && c != '/' && c != '-' && c != '^')
{
postFix.Append(c);
}
else if(c=='(')
{
ops.Push(c);
}
else if(c==')')
{
opFound = ops.Pop();
while (opFound != '(')
{
postFix.Append(opFound);
opFound = ops.Pop();
}
}
else
{
if((ops.Count!=0)&& Predecessor(ops.Peek(),c)){
opFound = ops.Pop();
while(Predecessor(opFound,c)){
postFix.Append(opFound);
if (ops.Count == 0)
break;
opFound = ops.Pop();
}
ops.Push(c);
}
else
{
ops.Push(c);
}
}
}
while (ops.Count > 0)
{
opFound = ops.Pop();
postFix.Append(opFound);
}
Console.WriteLine(postFix.ToString());
}
Console.ReadLine();
}
示例13: function
private bool function(string token, Stack<CValue> values, int x, int y, List<CellReference> refs)
{
switch (token.ToLowerInvariant()) {
case "$": {// value at coords
var ry = (int)values.Pop().NumericValue;
var rx = (int)values.Pop().NumericValue;
var g = sourceArray[rx, ry] as GridCell;
if (g == null) values.Push(new CValue());
else values.Push(new CValue(g.Value, true));
refs.Add(new CellReference(rx, ry));
return true;
}
case "$f": {// function string at coords
var ry = (int)values.Pop().NumericValue;
var rx = (int)values.Pop().NumericValue;
var g = sourceArray[rx, ry] as GridCell;
if (g == null) values.Push(new CValue());
else values.Push(new CValue(g.Formula));
refs.Add(new CellReference(rx, ry));
return true;
}
case "$n": {// name at coords
var ry = (int)values.Pop().NumericValue;
var rx = (int)values.Pop().NumericValue;
var g = sourceArray[rx, ry] as GridCell;
if (g == null) values.Push(new CValue());
else values.Push(new CValue(g.Name));
refs.Add(new CellReference(rx, ry));
return true;
}
case "floor":
values.Push(CValue.Floor(values.Pop()));
return true;
case "ceil":
values.Push(CValue.Ceil(values.Pop()));
return true;
case "sqrt":
values.Push(new CValue((decimal)Math.Sqrt((double)values.Pop().NumericValue)));
return true;
case "cos":
values.Push(new CValue((decimal)Math.Cos((double)values.Pop().NumericValue)));
return true;
case "sin":
values.Push(new CValue((decimal)Math.Sin((double)values.Pop().NumericValue)));
return true;
case "sign":
values.Push(new CValue(Math.Sign((double)values.Pop().NumericValue)));
return true;
}
return false;
}
示例14: XmlAttrVal
//[XmlBuilder, AttrBuilder] ==> [XmlBuilder] // add a finished XmlAttribute to the XmlBuilder
public Stack XmlAttrVal(Stack stack)
{
var val = (string) stack.Pop();
var ab = (AttrBuilder)stack.Pop();
ab.SetAttrValue(val);
var xeb = (XmlBuilder) stack.Pop();
xeb.AddAttr(ab.Complete());
stack.Push(xeb);
return stack;
}
示例15: EncontrarCoresFundo
/// <summary>
/// Encontra cores de fundo em um bitmap
/// </summary>
/// <param name="origem">Imagem original</param>
/// <returns>Dicionário de cores do fundo</returns>
private Dictionary<Color, Color> EncontrarCoresFundo(Bitmap origem)
{
Dictionary<Color, Color> cores = new Dictionary<Color, Color>(); // Cores do fundo
Stack pilha = new Stack(); // Pilha para flood-fill
bool [,] marcado; // Pontos marcados no flood-fill
marcado = new bool[origem.Width, origem.Height];
/* A cor de fundo é encontrada realizando um flood-fill
* nas bordas da imagem. As cores encontradas cujo brilho
* se difere de um determinado limiar são consideradas como
* cores de fundos.
*/
InserirPilha(pilha, origem.GetPixel(0, 0).GetBrightness(), 0, 0);
InserirPilha(pilha, origem.GetPixel(origem.Width - 1, 0).GetBrightness(), origem.Width - 1, 0);
InserirPilha(pilha, origem.GetPixel(origem.Width - 1, origem.Height - 1).GetBrightness(), origem.Width - 1, 0);
InserirPilha(pilha, origem.GetPixel(0, origem.Height - 1).GetBrightness(), 0, origem.Height - 1);
while (pilha.Count > 0)
{
Point ponto;
float brilhoAnterior;
Color corAtual;
ponto = (Point) pilha.Pop();
brilhoAnterior = (float) pilha.Pop();
if (ponto.X < 0 || ponto.X >= origem.Width
|| ponto.Y < 0 || ponto.Y >= origem.Height
|| marcado[ponto.X, ponto.Y])
continue;
corAtual = origem.GetPixel(ponto.X, ponto.Y);
if (Math.Abs(corAtual.GetBrightness() - brilhoAnterior) < limiarBrilho)
{
float brilhoAtual = corAtual.GetBrightness();
cores[corAtual] = Color.Black;
marcado[ponto.X, ponto.Y] = true;
InserirPilha(pilha, brilhoAtual, ponto.X - 1, ponto.Y - 1);
InserirPilha(pilha, brilhoAtual, ponto.X - 1, ponto.Y - 1);
InserirPilha(pilha, brilhoAtual, ponto.X, ponto.Y - 1);
InserirPilha(pilha, brilhoAtual, ponto.X + 1, ponto.Y - 1);
InserirPilha(pilha, brilhoAtual, ponto.X + 1, ponto.Y);
InserirPilha(pilha, brilhoAtual, ponto.X + 1, ponto.Y + 1);
InserirPilha(pilha, brilhoAtual, ponto.X, ponto.Y + 1);
InserirPilha(pilha, brilhoAtual, ponto.X - 1, ponto.Y + 1);
InserirPilha(pilha, brilhoAtual, ponto.X - 1, ponto.Y);
}
}
return cores;
}