本帖最后由 aa77dd@163.com 于 2015-9-20 19:48 编辑
概念混淆! 楼主要做的运算是 并集, 而不是交集!
把 批处理文件 和 文本文件放在同一目录- @echo off&setlocal enabledelayedexpansion
- (set LOut=')&(set UOut=Z)& rem 在集合最后面加一个假子集 [UOut, UOut] 作为尾部标志
-
- set "U={}" & (call :initSet U)
-
- for %%a in (*.txt) do for /f "tokens=1-2 delims=~" %%a in (%%~sa) do (
- echo %%a,%%b
- set "X={[%%a,%%b]}"
- (call :initSet X)
-
- call :Union X U U
- )
- call :Simplify U
- call :outputSet U
-
- > result.ini call :getSet U
- start result.ini
- echo 任意键退出&pause>nul
- exit
-
-
-
- :Union A B A∪B
- (call :copySet %1 A&call :copySet %2 B&call :clearSet R)
- set /a "pntA=1,pntB=1,pntR=0"&(set RL0=%LOut%&set RU0=%LOut%)
- :UnionLoop
- if "!RL%pntR%!"=="!UOut!" (call :copySet R %3)&exit /b
- :UnionSch1
- if !AU%pntA%! leq !RU%pntR%! set /a "pntA+=1"&goto :UnionSch1
- if !AL%pntA%! leq !RU%pntR%! set "RU!pntR!=!AU%pntA%!"
- :UnionSch2
- if !BU%pntB%! leq !RU%pntR%! set /a "pntB+=1"&goto :UnionSch2
- if !BL%pntB%! leq !RU%pntR%! set "RU!pntR!=!BU%pntB%!"
-
- set "intersec=Y"
- if !AL%pntA%! gtr !BU%pntB%! (set "intersec=N") else (
- if !BL%pntB%! gtr !AU%pntA%! set "intersec=N")
- set /a "pntR+=1"
- (call :min !AL%pntA%! !BL%pntB%! RL!pntR!)
- if "!intersec!"=="Y" (call :max !AU%pntA%! !BU%pntB%! RU!pntR!) else (
- call :min !AU%pntA%! !BU%pntB%! RU!pntR!)
- goto :UnionLoop
- exit /b
-
-
-
- :copySet src dest
- call :clearSet %2
-
- set "i=0"
- :copySet.loop
- set /a i+=1
- for %%i in (%i%) do ( if "!%1L%%i!"=="" (exit /b) else (set %2L%%i=!%1L%%i!&set %2U%%i=!%1U%%i!) )
- goto :copySet.loop
-
- exit /b
-
- :min a b min (a,b 以串值调用, min 以变量名调用)
- if %1 lss %2 (set %3=%1) else (set %3=%2)
- exit /b
-
- :max a b max (a,b 以串值调用, max 以变量名调用)
- if %1 gtr %2 (set %3=%1) else (set %3=%2)
- exit /b
-
-
- :outputSet
- (set lastout=')
- set /p=%1={<nul
- if "!%1L1!"=="" echo }&exit /b
-
- set "i=0"
- :outputSet.loop
- set /a i+=1
- for %%i in (%i%) do (
-
- if "!%1L%%i!"=="" (if %%i neq 1 (echo }) else echo })&exit /b
- if "!%1L%%i!"=="!UOut!" (
- (if %%i neq 1 (echo }) else echo })&exit /b
- ) else if !%1L%%i! gtr !lastout! (
- if !%1L%%i! neq !%1U%%i! (
- set /p=[!%1L%%i!,!%1U%%i!],<nul
- ) else set /p=!%1L%%i!,<nul
- (set lastout=!%1U%%i!)
- )
-
- )
- goto :outputSet.loop
- exit /b
-
-
- :clearSet
- set "i=0"
- :clearSet.loop
- set /a i+=1
- for %%i in (%i%) do ( if "!%1L%%i!"=="" (exit /b) else (set %1L%%i=&set %1U%%i=) )
- goto :clearSet.loop
- exit /b
-
- :err
- echo 错误, 任意键退出&pause
- exit /b
-
- :getUniverseLU
- set "universeL=1"
- :get_uniLU
- set /a "universeL<<=1"
- if %universeL% lss 0 (set /a "universeU=-(universeL+1), universeL=0"&exit /b) else goto :get_uniLU
- exit /b
-
-
- :Simplify A 仅用于最后输出之前(不再做任何运算), 将相邻子集合并
- (call :copySet %1 A)&(set /a pntA=0, pntN=pntA+1)&set "AU0=!LOut!"
- :SimplifyLoop
- if "!AL%pntN%!"=="!UOut!" (call :copySet A %1)&exit /b
- set /a "tt=AL!pntN!-1"
- if "!AU%pntA%!" equ "!tt!" (
- set "AU!pntA!=!AU%pntN%!"
- set /a "pntN+=1"
- ) else set /a "pntA=pntN,pntN+=1"
- goto :SimplifyLoop
- exit /b
-
- :initSet
- set "%1=!%1: =!" & set "%1=!%1:{=!" & set "%1=!%1:}=!"
- set "pnt%1=0" & set "flag=CN"
- for %%i in (!%1!) do (
- set "tt=%%i"
- if "!tt:~0,1!"=="[" (
- if "!flag!" equ "L" (echo 集合%1语法错误:%%i&pause&goto :err)
- set "flag=L"
- set /a pnt%1+=1&set "%1L!pnt%1!=!tt:~1!"
- ) else if "!tt:~-1!"=="]" (
- if "!flag!" neq "L" (echo 集合%1语法错误:%%i&pause&goto :err)
- set "flag=R"
- set "%1U!pnt%1!=!tt:~0,-1!"
- ) else if "!flag!" equ "L" (echo 集合%1语法错误:%%i&pause&goto :err
- ) else set /a pnt%1+=1&set "%1L!pnt%1!=!tt!"&set "%1U!pnt%1!=!tt!"&set "flag=CN"
- )
- set /a pnt%1+=1&set "%1L!pnt%1!=!UOut!"&set "%1U!pnt%1!=!UOut!"
- exit /b
-
-
- :getSet
- (set lastout=')
- if "!%1L1!"=="" exit /b
-
- set "i=0"
- :getSet.loop
- set /a i+=1
- for %%i in (%i%) do (
- if "!%1L%%i!"=="" exit /b
- if "!%1L%%i!"=="!UOut!" (
- exit /b
- ) else if !%1L%%i! gtr !lastout! (
- echo !%1L%%i!~!%1U%%i!
- (set lastout=!%1U%%i!)
- )
- )
- goto :getSet.loop
-
- exit /b
复制代码
|