Created
December 17, 2013 22:12
-
-
Save einarwh/8013516 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EvalStack | |
{ | |
private const char YesThis = '1'; | |
private const char NotThis = '0'; | |
private readonly string _; | |
private EvalStack(string s) { _ = s; } | |
public EvalStack() : this("") {} | |
public bool Peek() | |
{ | |
if (IsEmpty) | |
{ | |
throw new Exception("Cannot Peek an empty stack."); | |
} | |
return _[0] == YesThis; | |
} | |
public EvalStack Pop() | |
{ | |
if (IsEmpty) | |
{ | |
throw new Exception("Cannot Pop an empty stack."); | |
} | |
return new EvalStack(_.Substring(1)); | |
} | |
public EvalStack Push(bool b) | |
{ | |
char c = b ? YesThis : NotThis; | |
return new EvalStack(c + _); | |
} | |
public bool IsEmpty | |
{ | |
get { return _.Length == 0; } | |
} | |
public override bool Equals(object that) | |
{ | |
return Equals(that as EvalStack); | |
} | |
public bool Equals(EvalStack that) | |
{ | |
return _.Equals(that._); | |
} | |
public override int GetHashCode() | |
{ | |
return (_ != null ? _.GetHashCode() : 0); | |
} | |
public override string ToString() | |
{ | |
return "[" + _ + "]"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment