快来看看,怎样转换逻辑表达式?

将一个只有“与”和“或”的复杂逻辑表达式,转换为没有括号的简单表达式
比如现在有 A & (B ¦ C) ¦ (D ¦ E) & (F ¦ (G & (H ¦ I)))
这样一个逻辑表达式,我希望变成:
A*(B+C) + (D+E)&(F+(G*(H+I)))
=AB+AC+ D*(F+(G*(H+I)))+ E*(F+(G*(H+I)))
=AB+AC+ D*(F+(GH+GI))+ E*(F+(GH+GI))
=AB+AC+ D*(F+GH+GI)+ E*(F+GH+GI)
=AB + AC + DF + DGH + DGI + EF + EGH + EGI
最后一步是希望得到结果。
最后结果要放在一个表里,怎样的表结构最好?
注意:一开始的复杂逻辑式子的表结构也可以任意定。
---------------------------------------------------------------

drop table test
set nocount on
create table test(pid varchar(10),cid varchar(10))
insert test select '0','A'
union all select 'A','B'
union all select 'A','C'
union all select '0','D'
union all select '0','E'
union all select 'D','F'
union all select 'D','G'
union all select 'G','H'
union all select 'G','I'
union all select 'E','F'
union all select 'E','G'

select cid into #t1 from test where pid='0' and cid in (select pid from test)
select identity(int,1,1) as id,cid into #t2 from test where pid='0' and cid not in (select pid from test)
declare @i int,@j int
set @i=1
set @j=1
while @i<>0 and @j<>0
begin
insert #t1 select a.cid+''+b.cid from #t1 a,test b where b.pid=right(a.cid,1) and a.cid+''+b.cid not in (select cid from #t1) and b.cid in (select pid from test)
set @i=@@rowcount
insert #t2 select a.cid+''+b.cid from #t1 a,test b where b.pid=right(a.cid,1) and a.cid+''+b.cid not in (select cid from #t2) and b.cid not in (select pid from test)
set @j=@@rowcount
end

declare @string varchar(1000)
set @string=''
select @string=@string+cid+'+' from #t2
set @string=left(@string,len(@string)-1)
select @string

drop table #t1
select top 100 identity(int,1,1) as N into #t3 from sysobjects
select id,substring(''+cid+'',N+1,charindex('',''+cid+'',N+1)-N-1) as cid from #t2,#t3 where substring(''+cid+'',N,200) like '_%'

drop table #t2
drop table #t3

AB+AC+DF+DGH+EGH+DGI+EGI+EF

id cid
----------- ------------
1 A
1 B
2 A
2 C
3 D
3 F
4 D
4 G
4 H
5 E
5 G
5 H
6 D
6 G
6 I
7 E
7 G
7 I
8 E
8 F

Published At
Categories with 数据库类
Tagged with
comments powered by Disqus