Using system;using system.collections.generic;using system.linq;using system.text;using System.IO; Namespace calc24points{public class Cell {public enum Type {number, Signal } public int number; public Char Signal; Public Type Typ; public Cell right; Public Cell left; ////// Sign Priority ///public int Priority {get {if (Typ = = type.signal) { Switch (Signal) {case ' + ': return 0; Case '-': return 0; Case ' * ': return 1; Case '/': return 1; default:return-1; }} return-1; } } ////// basic unit constructor //////Cell type, value, or symbol///Numerical///Symbol Public Cell (Type T, int. num, char sig) {right = NULL; left = null; TYP = t; Number = num; Signal = sig; Public Cell () {right = NULL; left = null; Number = 0; TYP = Type.number; The public cell (cell c) {right = NULL; left = null; Number = C.number; Signal = c.signal; TYP = C.typ; }} public class Calc24points {string m_exp; BOOL M_stop; Cell[] M_cell; Int[] m_express; StringWriter m_string; Public calc24points (int n1, int n2, int n3, int n4) {M_cell = new cell[8]; M_CELL[0] = new cell (Cell.Type.Number, N1, '? '); M_CELL[1] = new cell (Cell.Type.Number, N2, '? '); M_CELL[2] = new cell (Cell.Type.Number, N3, '? '); M_CELL[3] = new cell (Cell.Type.Number, N4, '? '); M_CELL[4] = new cell (Cell.Type.Signal, 0, ' + '); M_CELL[5] = new cell (Cell.Type.Signal, 0, '-'); M_CELL[6] = new cell (Cell.Type.Signal, 0, ' * '); M_CELL[7] = new cell (Cell.Type.Signal, 0, '/'); M_stop = false; m_express = new Int[7]; m_string = new StringWriter (); M_exp = null; } public override string ToString () {if (m_exp = = null) {Putcell (0) ; M_exp = m_string. ToString (); } if (M_exp! = "") return m_exp; return null; } ////// place a unit in nth position //////void Putcell (int n) {if (n >= 7) {if (Calculate ()) { M_stop = true; Formate (); } return; } int end = 8; if (n < 2) end = 4; for (int i = 0; i < end; ++i) {m_express[n] = i; if (Checkcell (n)) Putcell (n + 1); if (m_stop) break; } } ////// Check whether the current placement is reasonable ////////
BOOL Checkcell (int n) {int nums = 0, sigs = 0; for (int i = 0; I <= N; ++i) {if (M_cell[m_express[i]]. Typ = = Cell.Type.Number) ++nums; else ++sigs; } if (Nums-sigs < 1) return false; if (M_cell[m_express[n]]. Typ = = Cell.Type.Number)//values cannot be duplicated, but symbols can be repeated {for (int i = 0; i < n; ++i) if (m_express[i] = = M_express[n]) return false; } if (n = = 6) {if (nums! = 4 | | sigs! = 3) return false; if (M_cell[m_express[6]]. Typ! = Cell.Type.Signal) return false; return true; } return true; } ////// Calculate whether the expression is///
The
return value of True is 24, otherwise it is not
BOOL Calculate () {double[] Dblstack = new Double[4]; int indexstack =-1; for (int i = 0; i < 7; ++i) {if (M_cell[m_express[i]]. Typ = = Cell.Type.Number) {++indexstack; Dblstack[indexstack] = M_cell[m_express[i]]. number; } else {switch (m_cell[m_express[i]). Signal) {case ' + ': dblstack[indexstack-1] = Dblsta CK[INDEXSTACK-1] + dblstack[indexstack]; Break Case '-': dblstack[indexstack-1] = dblstack[indexstack-1]-+ Dblstack[indexstack]; Break Case ' * ': dblstack[indexstack-1] = dblstack[indexstack-1] * Dblstack[indexstack]; Break Case '/': dblstack[indexstack-1] = dblstack[indexstack-1]/dblstack[index Stack]; Break }--indexstack; }} if (Math.Abs (Dblstack[indexstack) < 0.1) return true; return false; } /////suffix expression to infix expression/// void Formate () {cell[] c = new Cell[7]; for (int i = 0; i < 7; ++i) C[i] = new Cell (m_cell[m_express[i]); int[] Cstack = new Int[4]; int indexstack =-1; for (int i = 0; i < 7; ++i) {if (C[i]. Typ = = Cell.Type.Number) {++indexstack; Cstack[indexstack] = i; } else {C[i]. right = C[cstack[indexstack]]; --indexstack; C[i]. left = C[cstack[indexstack]]; Cstack[indexstack] = i; }} tostringformate (C[cstack[indexstack]); } void Tostringformate (Cell root) {if (root. Left.typ = = Cell.Type.Number) {m_string. Write (Root. Left.number); M_string. Write (Root. Signal); } else { if (root. Priority > root. left.priority) {m_string. Write ("("); Tostringformate (Root. left); M_string. Write (")"); } else Tostringformate (root. left); M_string. Write (Root. Signal); } if (root. Right.typ = = Cell.Type.Number) m_string. Write (Root. Right.number); else {if (root. Priority >= Root. right.priority) {m_string. Write ("("); Tostringformate (Root. right); M_string. Write (")"); } else Tostringformate (root. right); } } }}