// 将逆波兰表达式转换为一棵树 // parameters: // return: // NULL // 其他 BerNode ChangeRPNToTree() { BerNode param = null; int nRet; Token token = null; for (int i = 0; i < this.m_PolandArray.Count; i++) { token = m_PolandArray[i]; Debug.Assert(token != null, ""); if (token.m_nType == TYPE_OPERAND) { nRet = HandleOperand(token.m_strToken); if (nRet == -1) { throw new Exception("HandleOperand Fail!"); // return null; } } else { nRet = HandleOperator(token.m_nType, token.m_strToken); if (nRet == -1) { return(null); } } } PopFromArray(out param); /* * DeleteFile("polandtree.txt"); * pParam->DumpToFile("polandtree.txt"); */ // 如果pParam->m_ChildArray.GetSize() > 1 // 表示不能为21,必须在21下面有一个1 m_Subroot.AddSubtree(param); return(param); }
// 处理operator // parameters: // return: // NULL // 其他 int HandleOperator(int nType, string strToken) { BerNode param = null; BerNode subparam = null; int nRet; int i; param = new BerNode(); param.m_cClass = ASN1_CONTEXT; param.m_cForm = ASN1_CONSTRUCTED; param.m_uTag = 1; param.m_strDebugInfo = "operator [" + strToken + "]"; // 从堆栈中弹出两个操作数对象 for (i = 0; i < 2; i++) { nRet = PopFromArray(out subparam); if (nRet == -1) { return(-1); } param.AddSubtree(subparam); } // 创建op BldOperator(param, nType, strToken); // 结果又入栈 PushToArray(param); return(0); }