PNode FindNode(PNode p, int x, int y) { PNode result = null; if (p == null) { return(result); } if (((p.x - x) * (p.x - x) + (p.y - y) * (p.y - y)) <= 10 * R) { result = p; } else { if ((p is NodeOperation) && ((p as NodeOperation).left != null)) { result = FindNode((p as NodeOperation).left, x, y); } if ((result == null) && (p is NodeOperation) && ((p as NodeOperation).right != null)) { result = FindNode((p as NodeOperation).right, x, y); } } return(result); }
public Parser(int width, int height, ref string s) { top = null; bitmap = new Bitmap(width, height); Expression(ref s, out top); SetCoordinates(); }
public bool FindCoordinates(int x, int y) { if (top != null) { selected = FindNode(top, x, y); } return(selected != null); }
void Digit(ref string s, out PNode P) { string st = ""; while (s.Length != 0 && Test(s[0], digits)) { st += Pop(ref s, 1); } P = new NodeConst(Convert.ToInt32(st)); }
void DrawNode(PNode p) { if ((p is NodeOperation) && ((p as NodeOperation).left != null)) { g.DrawLine(Pens.Black, p.x, p.y, (p as NodeOperation).left.x, (p as NodeOperation).left.y); } if ((p is NodeOperation) && ((p as NodeOperation).right != null)) { g.DrawLine(Pens.Black, p.x, p.y, (p as NodeOperation).right.x, (p as NodeOperation).right.y); } if (p.visit == false) { g.FillEllipse(Brushes.LightBlue, p.x - R, p.y - R, 2 * R, 2 * R); } else { g.FillEllipse(Brushes.CornflowerBlue, p.x - R, p.y - R, 2 * R, 2 * R); } g.DrawEllipse(Pens.Black, p.x - R, p.y - R, 2 * R, 2 * R); string s = ""; if (p is NodeOperation) { switch ((p as NodeOperation).type) { case Operation.multiply: s = "*"; break; case Operation.divide: s = "/"; break; case Operation.plus: s = "+"; break; case Operation.minus: s = "-"; break; } } else { s = Convert.ToString(p.Value); } SizeF size = g.MeasureString(s, myFont); g.DrawString(s, myFont, Brushes.Black, p.x - size.Width / 2, p.y - size.Height / 2); if ((p is NodeOperation) && ((p as NodeOperation).left != null)) { DrawNode((p as NodeOperation).left); } if ((p is NodeOperation) && ((p as NodeOperation).right != null)) { DrawNode((p as NodeOperation).right); } }
void Set(PNode p, int x, int y, int i) { p.x = x; p.y = y; if ((p is NodeOperation) && ((p as NodeOperation).left != null)) { Set((p as NodeOperation).left, x - (int)Math.Pow(2, i) * step, y + dh * step, i - 1); } if ((p is NodeOperation) && ((p as NodeOperation).right != null)) { Set((p as NodeOperation).right, x + (int)Math.Pow(2, i) * step, y + dh * step, i - 1); } }
void ShiftNode(PNode p, int dx, int dy) { p.x -= dx; p.y -= dy; if ((p is NodeOperation) && ((p as NodeOperation).left != null)) { ShiftNode((p as NodeOperation).left, dx, dy); } if ((p is NodeOperation) && ((p as NodeOperation).right != null)) { ShiftNode((p as NodeOperation).right, dx, dy); } }
void DeSelectNode(PNode p) { if (p != null) { if (p is NodeOperation) { DeSelectNode((p as NodeOperation).left); } p.visit = false; if (p is NodeOperation) { DeSelectNode((p as NodeOperation).right); } } }
public Parser(int width, int height) { top = null; bitmap = new Bitmap(width, height); }
public NodeOperation(Operation op, PNode left, PNode right) { this.left = left; this.right = right; this.type = op; }
public void Clear() { n = 0; top = null; selected = null; }
void Expression(ref string s, out PNode P) { if (s.Length != 0) { if (Test(s[0], digits)) { Digit(ref s, out P); } else if (s[0] == '(') { Pop(ref s, 1); Expression(ref s, out P); if (s.Length == 0 || s.Length > 0 && s[0] != ')') { throw new ParserException(2); } Pop(ref s, 1); } else { switch (s[0]) { case '*': throw new ParserException(3); case '/': throw new ParserException(4); default: throw new ParserException(8); } } if (s.Length > 0 && !(Test(s[0], symbols) || s[0] == ')')) { throw new ParserException(8); } while (s.Length > 0 && Test(s[0], symbols)) { string symbol = Pop(ref s, 1); if (s.Length == 0) { switch (symbol) { case "*": throw new ParserException(3); case "/": throw new ParserException(5); case "+": throw new ParserException(6); case "-": throw new ParserException(7); } } PNode P2; Expression(ref s, out P2); PNode P1 = P; Operation op = Operation.none; switch (symbol) { case "*": op = Operation.multiply; break; case "/": op = Operation.divide; break; case "+": op = Operation.plus; break; case "-": op = Operation.minus; break; } P = new NodeOperation(op, P1, P2); n++; } } else { throw new ParserException(1); } }