标题: [转]纯批处理解一次方程 [打印本页]
作者: 路过 时间: 2021-1-1 16:37 标题: [转]纯批处理解一次方程
转自百度bat吧。https://tieba.baidu.com/p/7173843754
v1.1的代码- @echo off
- for /f "tokens=*" %%i in ('more +2 "%~s0"^|find "\"') do (echo%%i)
- for /f "delims==" %%w in ('set') do (set "%%w=")
-
- ::初始化常量
- ::_MAX为最大未知数个数
- set/a _MAX=100
- setlocal enabledelayedexpansion
- set/a num=0,mod=1
- set "#=echo;$&pause&goto put"
- set .=a b c d e f g h i j k l m n o p q r s t u v w x y z
- set _=0 1 2 3 4 5 6 7 8 9
- set ..+=2
- set ..-=2
- set ..`=1
- set/a .10=1,.20=1,.1`=1,.2`=1
- for %%a in (%.%) do (set ..%%a=3)
- for %%a in (%_%) do (set ..%%a=4)
- pause
- setlocal
-
- :put 用户输入计算式
- cls
- title 一次方程计算器V1.1
- for /l %%a in (1,1,%num%) do (echo.%%a:!equ%%a!)
- set/a e=num+1,att=1,n=1,un=0
- set "d="
- set /p d=%e%:
- if "!d!"=="" (%#:$=表达式不能为空%)
- set d=!d: =!
- set f=!d!
- for %%a in (%.% %_% + -) do (set f=!f:%%a=0!)
- if "!f:0=!" neq "=" (%#:$=方程式有误%)
- for /f "delims== tokens=1,2" %%a in ("!d!#") do (
- if "%%b"=="" (set d=`%%a) else (set d=%%a`%%b))
- set d=!d:#=!
-
- ::检验命令表达式
- if %e% neq 1 (
- if /i %d%==`run (set mod=1&goto solve)
- if /i %d%==`ret (set mod=2&goto solve)
- if /i %d:~0,4%==`del (set h=%d:~4%
- if defined equ!h! (set equ!h!=!equ%num%!)
- set/a num-=1
- set "equ%num%="
- goto put)
- if /i %d%==`cls (%#:echo;$&pause=endlocal&setlocal%)
- )
-
- ::检验方程式表达式
- set/a y=2048,g=0
- set "nd="
- for /l %%y in (1,1,12) do (set/a "len=(y+g)/2"
- for /f "delims=" %%z in ("!len!") do (if "!d:~%%z!" equ "" (set y=!len!) else (set g=!len!)))
- for /l %%a in (0,1,%len%) do (set m=!d:~%%a,1!
- set ck=0
- if defined .!att!!m! (%#:$=格式有误%)
- for %%b in (!m!) do (set att=!..%%b!)
- if !un!==1 (if !att!==3 (%#:$=未知数不能乘未知数%)
- if !att! leq 2 (set/a un=0,ck=1
- set nd=!nd!]!m!))
- if !att!==3 (set nd=!nd![!m!
- set/a un=1,ck=1)
- if !ck!==0 (set nd=!nd!!m!)
- set n=!n!!att!)
- if %n:3=0%==%n% (%#:$=无未知数%)
- if %att%==1 (set nd=%nd%0)
- if %att%==2 (%#:$=加减号最后%)
- if %un%==1 (set nd=%nd%])
-
- :simple 符号化简
- set ck=%nd%
- set nd=%nd:--=+%
- set nd=%nd:++=+%
- set nd=%nd:+-=-%
- set nd=%nd:-+=-%
- if %nd% neq %ck% (goto simple)
- for /f "delims=` tokens=1,2" %%a in ("!nd!") do (set "nd=%%a=%%b")
-
- ::开始下一个方程式的输入
- set/a num+=1
- set equ%e%=%nd%
- goto put
-
- :solve 解方程初始化
- setlocal enabledelayedexpansion
- set/a ukc=0,err=0
- set "unk="
- set "ukn="
- set pl=+
- set pr=-
- set ql=-
- set qr=+
-
- ::获取未知数
- for /l %%a in (1,1,%num%) do (set x=!equ%%a:[= !
- for %%b in (!x!) do (set y=%%b
- if "!y:]=!" neq "!y!" (for /f "delims=] tokens=1,2" %%c in ("%%b") do (set unk=%%c
- if not defined @!unk! (set/a ukc+=1
- set @!unk!=!ukc!
- set ukn=!ukn!!unk!,
- )
- )
- )
- )
- )
- echo;未知数个数%ukc% 方程式个数%num%
- if %ukc% neq %num% (endlocal&%#:$=未知数个数不等于方程式个数%)
-
- ::格式转化
- for /f "delims==" %%a in ('set @') do (set x=%%a
- set x=!x:@=[!]
- set y=!%%a!
- for /f "delims== tokens=1,2" %%b in ("!x!=!y!") do (
- for /l %%d in (1,1,%num%) do (set equ%%d=!equ%%d:%%b=_%%c!)))
- for /l %%d in (1,1,%num%) do (set equ%%d=!equ%%d:-=+-!)
-
- ::获取系数
- for /l %%a in (1,1,%num%) do (%Made%
- for /f "delims== tokens=1,2" %%b in ("!equ%%a!") do (%By%
- for /l %%d in (0,1,%ukc%) do (set @%%a_%%d=0)
- set sl=%%b
- set sr=%%c
- for %%d in (l r) do (if "!s%%d:~0,1!"=="+" (set s%%d=0!s%%d!)
- set s%%d=!s%%d:+= !
- for %%e in (!s%%d!) do (set cy=%%e
- if "!cy:_=!" neq "!cy!" (%绽放ad%
- if "!cy:~0,1!"=="_" (set cy=1!cy!)
- set cy=!cy:-_=-1_!
- for /f "delims=_ tokens=1,2" %%f in ("!cy!") do (set @%%a_%%g=!@%%a_%%g!!p%%d!%%f)
- ) else (
- set @%%a_0=!@%%a_0!!q%%d!!cy!
- )
- )
- )
- )
- )
-
- ::计算系数
- for /f "delims== tokens=1,2" %%a in ('set @') do (set/a %%a=%%b||set err=1)
- if %err%==1 (endlocal&%#:$=溢出%)
- if %mod%==2 (set file=%~sdp0rect.txt
- (echo;!ukn!常数项
- for /l %%a in (1,1,%num%) do (set "x="
- for /l %%b in (1,1,%num%) do (set x=!x!!@%%a_%%b!,)
- echo;!x!!@%%a_0!))>!file!
- endlocal&%#:$=结果已保存到rect.txt%
- )
- if %ukc% gtr %_MAX% (endlocal&%#:$=超出程序允许的最多未知数!_MAX!%)
-
- ::清除无关紧要的变量
- set @u_m=,
- set @n_m=%num%
- for /l %%a in (1,1,%num%) do (set @u_m=!@u_m!%%a,)
- for /f "delims== eol=@" %%a in ('set ') do (set "%%a=")
- for /f "delims== tokens=1,2" %%a in ('set @') do (set q=%%a
- if "!q:_=!"=="!q!" (set @%%b=!q:@=!
- set "%%a="))
- set/a "all=2*%@n_m%*(%@n_m%+1)*(%@n_m%-1)/3,now=0,fx=%@n_m%"
-
- ::高斯列主消元法,省去了排序,但多了约分操作。
- ::第一步化为三角矩阵
- echo;正在解方程组...复杂度o(%@n_m%)=!all!
- set ux=%@u_m%
- set uy=%@u_m%
- for %%a in (his sih ls) do (set "%%a=")
- for /l %%z in (1,1,%@n_m%) do (set q=0
- for %%a in (!uy!) do (for %%b in (!ux!) do (if !q!==0 (
- if "!@%%a_%%b!" neq "0" (set his=!his!%%a_%%b
- if %%z neq %@n_m% (set/a m=0,n=0,a0=0
- for /l %%x in (0,1,%@n_m%) do (if "!@%%a_%%x!" neq "0" (set/a a!m!=!@%%a_%%x!,m+=1))
- set/a n=m-1
- for /l %%x in (1,1,!n!) do (set c=0
- for /l %%y in (0,1,32) do (if !c!==0 (set/a d=!a%%x!,z=!a%%x! %% !a0!
- if !z! neq 0 (set/a a%%x=!a0!,a0=!z!) else (set c=1))))
- if !a0!==0 (set a0=1)
- for /l %%x in (0,1,%@n_m%) do (set/a @%%a_%%x/=a0)
- set ux=!ux:,%%b,=,!
- set uy=!uy:,%%a,=,!
- for %%c in (!uy!) do (
- for %%d in (!ux!0) do (set/a @%%c_%%d=!@%%c_%%d!*!@%%a_%%b!-!@%%a_%%d!*!@%%c_%%b!)
- set @%%c_%%b=0
- set/a m=0,n=0,a0=0
- for /l %%x in (0,1,%@n_m%) do (if "!@%%c_%%x!" neq "0" (set/a a!m!=!@%%c_%%x!,m+=1))
- set/a n=m-1
- for /l %%x in (1,1,!n!) do (set c=0
- for /l %%y in (0,1,32) do (if !c!==0 (set/a d=!a%%x!,z=!a%%x! %% !a0!
- if !z! neq 0 (set/a a%%x=!a0!,a0=!z!) else (set c=1))))
- if !a0!==0 (set a0=1)
- for /l %%x in (0,1,%@n_m%) do (set/a @%%c_%%x/=a0)
- )
- set q=1
- )
- )
- )
- )
- )
- set/a m=fx*fx-fx
- set/a now+=m,fx-=1,n=1000*now/all
- title 进度 !n:~0,-1!.!n:~-1!%%
- )
-
- ::第二步转标准矩阵
- echo;===============
- set/a ck=0,fx=%@n_m%
- for %%a in (%his%) do (if !ck!==0 (set ls=%%a)
- set sih=%%a !sih!
- set/a ck+=1)
- set his=!sih!
- set ux=%@u_m%
- set uy=%@u_m%
- for %%z in (%his%) do (
- for /f "delims=_ tokens=1,2" %%a in ("%%z") do (set/a m=0,n=0,a0=0
- for /l %%x in (0,1,%@n_m%) do (if "!@%%a_%%x!" neq "0" (set/a a!m!=!@%%a_%%x!,m+=1))
- set/a n=m-1
- for /l %%x in (1,1,!n!) do (set c=0
- for /l %%y in (0,1,32) do (if !c!==0 (set/a d=!a%%x!,z=!a%%x! %% !a0!
- if !z! neq 0 (set/a a%%x=!a0!,a0=!z!) else (set c=1))))
- if !a0!==0 (set a0=1)
- for /l %%x in (0,1,%@n_m%) do (set/a @%%a_%%x/=a0)
- if "%%z" neq "%ls%" (
- set ux=!ux:,%%b,=,!
- set uy=!uy:,%%a,=,!
- for %%c in (!uy!) do (
- for %%d in (!ux!0) do (set/a @%%c_%%d=!@%%c_%%d!*!@%%a_%%b!-!@%%a_%%d!*!@%%c_%%b!)
- set @%%c_%%b=0
- set/a m=0,n=0,a0=0
- for /l %%x in (0,1,%@n_m%) do (if "!@%%c_%%x!" neq "0" (set/a a!m!=!@%%c_%%x!,m+=1))
- set/a n=m-1
- for /l %%x in (1,1,!n!) do (set c=0
- for /l %%y in (0,1,32) do (if !c!==0 (set/a d=!a%%x!,z=!a%%x! %% !a0!
- if !z! neq 0 (set/a a%%x=!a0!,a0=!z!) else (set c=1))))
- if !a0!==0 (set a0=1)
- for /l %%x in (0,1,%@n_m%) do (set/a @%%c_%%x/=a0)
- )
- )
- )
- set/a m=fx*fx-fx
- set/a now+=m,fx-=1,n=1000*now/all
- title 进度 !n:~0,-1!.!n:~-1!%%
- )
-
- ::输出方程组的各个解
- set e=0
- for /l %%a in (1,1,%@n_m%) do (set/a ck=0
- set "his="
- for /l %%b in (1,1,%@n_m%) do (set q=!@%%a_%%b!
- if "!q!" neq "0" (set/a ck+=1
- if "!q!"=="1" (set his=!his!!@%%b!+) else (if "!q!"=="-1" (set his=!his!-!@%%b!+) else (set his=!his!!q!!@%%b!+))
- )
- )
- if !ck!==0 (
- if "!@%%a_0!"=="0" (
- if !e! neq 1 (set e=2)
- ) else (
- echo; %%a:0=!@%%a_0! ×
- set e=1
- )
- ) else (
- set "his=!his:~0,-1!=!@%%a_0!"
- echo; %%a:!his:+-=-!
- )
- )
-
- ::输出方程组结果
- echo;===============
- set ck=0
- for %%a in (有唯一解 无解 无穷多解) do (if %e%==!ck! (echo;方程组%%a)
- set/a ck+=1)
- pause
- endlocal
- goto put
-
- \欢迎使用一次方程计算器,本程序能解决大部分线性方程组并显示解答结果。
- \用户需按要求输入方程或命令,回车输入下一个,只有正确方程才保留。
- \命令在回车、执行后便被删除,错误或不合法的方程也会在回车后被删除。
- \
- \支持的字符有26个英文字母(不区分大小写)、数字0-9、运算符号+ - =。
- \只支持整数计算,含小数分数请人工化为整数形式(两边同乘以一个数)再输入。
- \
- \用户在本程序里输入的方程式有以下要求:
- \ ①方程式必须有且只有一个等号,至少含1个未知数。
- \ ②为避免0引起的BUG,规定数字开头不能为0。如果要输入"a=0",请输入"a="。
- \ ③未知数既可以是单个字母也可以是字母加任意下标数字。
- \ ④数字和未知数之间省略乘号,未知数前的数字1可省略,未知数后面必须跟运算符。
- \ ⑤加减号不能出现在方程式末尾,且后面不能是等号。未知数和方程式的个数一致。
- \
- \命令(等号开头,不区分大小写)如下:
- \ ①=run:解方程(组)。
- \ ②=ret:获取方程组的系数矩阵(不解答)。
- \ ③=deln:删除输入的第n个方程。
- \如输入"=del3"会删除第三个方程表达式。n未找到会删除最近的一个方程。
- \ ④=cls:清除所有方程
- \
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |