Board logo

标题: 如何实现多个文件的同一行同一列相加? [打印本页]

作者: 24634080     时间: 2007-4-27 03:11    标题: 如何实现多个文件的同一行同一列相加?

如何实现多个文件的同一行同一列相加?谢谢!
作者: lxmxn     时间: 2007-4-27 05:39
To 24634080:

可以试试下面这个,针对两个文件的,如果1.txt和2.txt的同一行和同一列都有数字的话,将被计算其两者之和,否则将不参加计算,结果也不会显示。需要注意的是,如果结果过大,会产生溢出,导致结果不正确。
@echo off&Setlocal EnableDelayedExpansion
set maxcols=1
set maxline=1
copy nul 3.txt >nul
call :context 1.txt
call :context 2.txt
call :result 1.txt 2.txt
start notepad.exe 3.txt
pause&goto :eof

:context
set flag=%1
set line=1
for /f "delims=" %%a in (%flag%) do (
        set cols=1
        for %%b in (%%a) do (
                set %flag%!line!!cols!=%%b
                set /a cols+=1
                if !maxcols! leq !cols! set /a maxcols=!cols!
        )
        set /a line+=1
        if !maxline! leq !line! set /a maxline=!line!
)

for /l %%z in (1,1,%maxline%) do (
        for /l %%y in (1,1,%maxcols%) do (
        set/p=!%flag%%%z%%y! <nul
        )
echo\
)
goto :eof

:result
for /l %%z in (1,1,%maxline%) do (
        for /l %%y in (1,1,%maxcols%) do (
        if defined %1%%z%%y if defined %2%%z%%y (set /a _%%z%%y=!%1%%z%%y!+!%2%%z%%y!)
        set/p=!_%%z%%y! <nul
        >>3.txt set/p=!_%%z%%y! <nul
        )
        echo\>>3.txt
        echo\
)
1.txt内容如下:

  Quote:
1 2 3 4
456 153 6548 315
5648 2489 321 43534
54668 3289 328 343

2.txt的内容如下:

  Quote:
115 316 358 345
2315 54  654 6548
1981898 231984
212 654 6548 315 6483
212 564 5684 984 32

输出结果3.txt的内容如下:

  Quote:
116  318  361  349      
2771  207  7202  6863      
1987546  234473         
54880  3943  6876  658