Home   Cover Cover Cover Cover
 

Attributes

Trace.cs
#define debug

using System;
using System.Diagnostics;

class Stack {
  int[] val = new int[10];
  int top = -1;
  
  [Conditional("debug")]
  public void PrintStack(string msg) {
    Console.Write(msg + ": top = " + top + ", val = ");
    foreach (int x in val) Console.Write(x + " ");
    Console.WriteLine();
  }
  
  public void Push(int x) {
    PrintStack("before");
    if (top < val.Length) val[++top] = x;
    PrintStack("after");
  }
  
  public int Pop() {
    int res;
    PrintStack("before");
    if (top >= 0) { res = val[top]; top--; } else res = -1;
    PrintStack("after");
    return res;
  }
}

class Test {
  
  public static void Main() {
    Stack s = new Stack();
    s.Push(3);
    s.Push(7);
    Console.WriteLine("result = " + (s.Pop() + s.Pop()));
  }
}