真的 不能 解决吗?(发第4帖 求递归算法了,愿出 500分 感谢)(在线等待)

天限天空 2002-07-23 11:38:52
前三 帖 链接:
http://www.csdn.net/expert/topic/888/888244.xml?temp=.6981623
http://www.csdn.net/Expert/TopicView1.asp?id=888244
详细内容 在 http://www.csdn.net/expert/topic/882/882283.xml?temp=.7165338


--并且
|__ 姓名 包涵 刘
|__ 所属单位 包涵 51robot
|__ 或者
| |__ 性别 不等于 男
| |__ 并且
| |__ 出生年月 等于 1979-7-28
| |__ 所属单位 不包涵 aa
|
|___ 录入时间 不等于 2002-7-17
|___ 身份证号码 包涵 5101071979

上面是 我构造出来的 查询条件树 ,(前面如果是 并且 表示 子节点 都是并且 关系,前面如果是 或者 表示 子节点 都是或者 关系)

请指教 我怎样 从 这个 树组合 sql 查询语句 (每个查询条件接点中都 隐藏着 所属表名、字段名、类型 ... 等条件)

用vc 的递归实现,
以下是 一段 代码, 但结果 总是有问题, 请高手指点!!!
void CSearchDlg::makeSQL(HTREEITEM hItem)
{
HTREEITEM hSubItem,hSiblingItem;

hSubItem = m_tree.GetChildItem(hItem);
if(hSubItem) {
sqlText = sqlText + "(";
makeSQL(hSubItem);
sqlText = sqlText + ")";
}

hSiblingItem = m_tree.GetNextSiblingItem(hItem);
while(hSiblingItem)
{
if(m_tree.GetItemText(hSiblingItem) != "并且" && m_tree.GetItemText(hSiblingItem) != "或者") sqlText = sqlText + "*";

makeSQL(hSiblingItem);
hSiblingItem = m_tree.GetNextSiblingItem(hSiblingItem);
}

}

(在这里用 * 来表示 查询条件)

详细内容 在 http://www.csdn.net/expert/topic/882/882283.xml?temp=.7165338

先谢了
...全文
82 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
gaochunrong 2002-07-25
  • 打赏
  • 举报
回复
SQL 谨供参考
具体情况和数据类型有关

where
and 姓名like '刘%'
and 所属单位 like '%51robot%'
or (性别 <> '男'
and 出生年月 = '1979-7-28'
and 所属单位 not like '%aa%')
and 录入时间 <> '2002-7-17'
and 身份证号码 like '%5101071979%'


  • 打赏
  • 举报
回复
CString CSearchDlg::makeSQLWhere(HTREEITEM hItem)
{
HTREEITEM hSubItem,hSiblingItem;
CString strLogic = m_tree.GetItemText(hItem);
CString str;
ASSERT(strLogic == _T("并且") || strLogic == _T("或者"));
hSubItem = m_tree.GetChildItem(hItem);
CString strL = m_tree.GetItemText(hItem);
sqlText = sqlText + "(";
if(hSubItem)
{
if (m_tree.GetItemText(hSubItem) != "并且" && m_tree.GetItemText(hSubItem) != "或者")
sqlText = sqlText + m_tree.GetItemText(hSubItem);
else sqlText = sqlText + makeSQLWhere(hSubItem);
hSiblingItem = m_tree.GetNextSiblingItem(hSubItem);
while(hSiblingItem)
{
if(m_tree.GetItemText(hSiblingItem) == "并且" || m_tree.GetItemText(hSiblingItem) == "或者")
sqlText = sqlText + " and " + makeSQLWhere(hSiblingItem);
//str = m_tree.GetItemData(hSiblingItem); // or GetItemText() ?
else
sqlText =sqlText + " and " + m_tree.GetItemText(hSiblingItem);

hSiblingItem = m_tree.GetNextSiblingItem(hSiblingItem);

}
}

sqlText = sqlText + ")";
return sqlText;
}

不过对VC不熟,请把GetNextSiblingItem的问题
改一下就行了,逻辑上我想不出来是什么问题
但实际上只要树中有树的时候,总是多出一遍来,
许野平 2002-07-24
  • 打赏
  • 举报
回复
兄弟,我看很多高手都没来解这个题,主要原因是这个问题难度不算高,但又确实非常麻烦。即使写出来代码也是懒婆娘的裹脚--又臭又长,就像开拖拉机一样,没有驾驶乐趣...其实我们曾经讨论过,应该换个简单方便的思路.
demiurge 2002-07-24
  • 打赏
  • 举报
回复
makesql(HTREEITEM hItem, String logOpt) {
HTREEITEM nextItem, chidItem;
childItem = GetChildItem();
if (childItem)
sqltext += logOpt + "(" + makesql(childItem) + ")";
else
sqltext += logOpt + hitem.value;

nextItem = GetNextItem()
if (nextItem) {
sqltext += logOpt + makesql(nextItem);
else
sqltext += "";

return sqltext;
}
xDraw 2002-07-24
  • 打赏
  • 举报
回复
up

33,027

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧