复制代码
- 输入1+2+3
- 得6
大家看看这是主代码复制代码
- @echo off
- setlocal enabledelayedexpansion
- title
- color b0
- :beginn
- cls
- echo 说明
- echo =============================================================
- echo a.函数
- echo.
- echo b.定义新运算
- echo.
- echo c.递归函数
- echo.
- echo d.等差/等比数列
- echo.
- echo e.最大公约数
- echo.
- echo =============================================================
- set /p a=输入想被帮助的项目(q.退出)(f.进入计算系统)
- if %a% equ a (
- goto a
- )
- if %a% equ b (
- goto b
- )
- if %a% equ c (
- goto c
- )
- if %a% equ d (
- goto d
- )
- if %a% equ e (
- goto e
- )
- if %a% equ f (
- goto f
- )
- if %a% equ q (
- goto q
- )
- if %a% equ g (
- goto g
- )
- :a
- echo negative函数:取反
- echo sum函数:求和
- echo max,min:取最大最小值
- echo mod:取余
- echo cube:3次方
- echo square:平方和
- pause>nul
- goto beginn
- :b
- start 1.jpg
- echo 如图所示,最多可支持二元函数的自定义
- pause>nul
- goto beginn
- :c
- start 2.jpg
- echo 如图所示
- pause>nul
- goto beggin
- :d
- echo 定义等比/等差数列
- echo 你们可以自己试一试,我就不发图了
- echo a(1)=1
- echo a(x)=a(x-1)*2
- echo a(1)=1
- echo a(x)=a(x-1)+2
- echo 还可以定义阶乘函数:
- echo factor(0)=1
- echo factor(1)=1
- echo factor(x)=factor(x-1)*x
- echo 自己试试
- pause>nul
- goto beginn
- :e
- echo 辗转相除法:
- echo **(x,0)=x
- echo **(x,y)=**(y,mod(x,y))
- echo 就这么简单
- pause>nul
- goto beggin
- :q
- exit
- :f
- cls
- rem initalize
- set stack=1
- set heap=1
- set number=1 2 3 4 5 6 7 8 9 0
- set addsub=+ -
- set digit=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 env_pi=3
- set env_e=3
- set mem_test_1=100
- :entry
- set /p input=^>
- call :clear_heap
- call :clear_state
- set state=0
- :convert_vector
- if not defined input goto next
- set this=%input:~0,1%
- set input=%input:~1%
- for %%i in (%number%) do (
- if !this!==%%i (if !input_%state%.type!==number (if not defined input_%state%.next set input_%state%.value=!input_%state%.value!%%i&goto convert_vector))
- if !this!==%%i (
- set /a state+=1
- set input_!state!.value=%%i
- set input_!state!.type=number
- goto convert_vector
- ))
- for %%i in (%digit%) do (
- if !this!==%%i (if !input_%state%.type!==digit (if not defined input_%state%.next set input_%state%.value=!input_%state%.value!%%i&goto convert_vector))
- if !this!==%%i (
- set /a state+=1
- set input_!state!.value=%%i
- set input_!state!.type=digit
- goto convert_vector
- ))
- if !this!==+ set /a state+=1&set input_!state!.type=+&goto convert_vector
- if !this!==- set /a state+=1&set input_!state!.type=-&goto convert_vector
- if !this!==* set /a state+=1&set input_!state!.type=*&goto convert_vector
- if !this!==/ set /a state+=1&set input_!state!.type=/&goto convert_vector
- if "%this%"=="(" set /a state+=1&set input_!state!.type=(&goto convert_vector
- if "%this%"==")" set /a state+=1&set input_!state!.type=)&goto convert_vector
- if "%this%"=="=" set /a state+=1&set input_!state!.type==&goto convert_vector
- if "%this%"=="," set /a state+=1&set input_!state!.type=,&goto convert_vector
- if "%this%"==" " set input_%state%.next=1&goto convert_vector
- echo 词法分析错误:有无法分析的单词 :!this!
- :next
- if "%input_7.type%"=="=" (call :new2yuan
- goto entry)
- if "%input_5.type%"=="=" (call :new1yuan
- goto entry)
- if "%input_2.type%"=="=" (call :expr 3 ret heap.1 input
- if defined ret.ret (set env_%input_1.value%=!ret.ret!&goto entry) else (
- echo 定义变量错误
- goto entry))
- call :expr 1 ret heap.1 input
- set /a ret.new-=1
- if %ret.new% equ %state% ( echo !ret.ret!&goto entry ) else (
- echo 表达式计算错误
- goto entry)
- pause
- goto entry
- goto :eof
- :expr %1 %2 %3 %4
- set /a heap+=1
- call :factor %1 %3.a heap.%heap% %4
- if not defined %3.a.ret goto :eof
- set newlocal=!%3.a.new!
- set op=!%4_%newlocal%.type!
- if not "%op%"=="+" (if not "%op%"=="-" set %2.ret=!%3.a.ret!&set %2.new=!%3.a.new!&goto :eof)
- set /a newlocal=!%3.a.new!+1
- set /a heap+=1
- call :expr %newlocal% %3.b heap.%heap% %4
- if not defined %3.b.ret set %2.ret=!%3.a.ret!&set %2.new=!%3.a.new!&goto :eof
- set newlocal=!%3.a.new!
- set op=!%4_%newlocal%.type!
- if %op%==+ set /a %2.ret=%3.a.ret+%3.b.ret&set %2.new=!%3.b.new!&goto :eof
- if %op%==- set /a %2.ret=%3.a.ret-%3.b.ret&set %2.new=!%3.b.new!&goto :eof
- echo error :can't find '+' or '-'
- goto :eof
- :factor %1 %2 %3 %4
- set /a heap+=1
- call :term %1 %3.a heap.%heap% %4
- if not defined %3.a.ret goto :eof
- set newlocal=!%3.a.new!
- set op=!%4_%newlocal%.type!
- if not "%op%"=="*" (if not "%op%"=="/" set %2.ret=!%3.a.ret!&set %2.new=!%3.a.new!&goto :eof)
- set /a newlocal=!%3.a.new!+1
- set /a heap+=1
- call :factor %newlocal% %3.b heap.%heap% %4
- if not defined %3.b.ret set %2.ret=!%3.a.ret!&set %2.new=!%3.a.new!&goto :eof
- set newlocal=!%3.a.new!
- set op=!%4_%newlocal%.type!
- if %op%==* set /a %2.ret=%3.a.ret*%3.b.ret&set %2.new=!%3.b.new!&goto :eof
- if %op%==/ set /a %2.ret=%3.a.ret/%3.b.ret&set %2.new=!%3.b.new!&goto :eof
- echo error :can't find '*' or '/'
- goto :eof
- :term %1 %2 %3 %4
- if !%4_%1.type!==number set %2.ret=!%4_%1.value!&set /a %2.new=%1+1
- if !%4_%1.type!==digit call :var %1 %2 %3 %4&goto :eof
- if not !%4_%1.type!==( goto :eof
- set /a heap+=1
- set /a newlocal=%1+1
- call :expr %newlocal% %3.a heap.%heap% %4
- if not defined %3.a.ret goto :eof
- set /a newlocal=!%3.a.new!
- if not !%4_%newlocal%.type!==) goto :eof
- set %2.ret=!%3.a.ret!
- set /a newlocal+=1
- set %2.new=%newlocal%
- goto :eof
- :var %1 %2 %3 %4
- set temp=!%4_%1.value!
- set /a temp2=%1+1
- if not !%4_%temp2%.type!==( set %2.ret=!env_%temp%!&set /a %2.new=%1+1&goto :eof
- set /a temp2+=1
- set analyze_t=%temp%
- if !%4_%temp2%.type!==) set /a %2.new=temp+1&goto var_end
- set /a heap+=1
- call :push analyze_t
- call :expr %temp2% %3.a heap.%heap% %4
- call :pop analyze_t
- set analyze_t=%analyze_t% !%3.a.ret!
- set /a temp2=!%3.a.new!
- if !%4_%temp2%.type!==) set /a %2.new=temp2+1&goto var_end
- set /a heap+=1
- set /a temp2+=1
- call :push analyze_t
- call :expr %temp2% %3.b heap.%heap% %4
- call :pop analyze_t
- set analyze_t=%analyze_t% !%3.b.ret!
- set /a temp2=!%3.b.new!+1
- set %2.new=%temp2%
- :var_end
- set ret=
- call :%analyze_t% 2>nul
- if defined ret set %2.ret=%ret%&goto :eof
- set temp=mem_!%4_%1.value!_!%3.a.ret!
- if defined %temp% set %2.ret=!%temp%!&goto :eof
- set temp=mem_!%4_%1.value!_!%3.a.ret!_!%3.b.ret!
- if defined %temp% set %2.ret=!%temp%!&goto :eof
- set temp=save_!%4_%1.value!
- set temp2=!%4_%1.value!
- if !yuan_%temp2%! equ 2 goto yuan2
- call :push env_!%temp%!
- set env_!%temp%!=!%3.a.ret!
- set /a heap+=1
- call :expr 1 %3.a heap.%heap% envf_%temp2%
- set temp=save_!%4_%1.value!
- set temp2=env_!%temp%!
- set mem_!%4_%1.value!_!%temp2%!=!%3.a.ret!
- call :pop env_!%temp%!
- set %2.ret=!%3.a.ret!
- goto :eof
- :yuan2
- if defined envf_%temp2%_var1_!%3.a.ret!_1.type goto yuan2_1
- if defined envf_%temp2%_var2_!%3.b.ret!_1.type goto yuan2_2
- set temp3=save2_!%4_%1.value!
- call :push env_!%temp%!
- call :push env_!%temp3%!
- set env_!%temp%!=!%3.a.ret!
- set env_!%temp3%!=!%3.b.ret!
- set /a heap+=1
- call :expr 1 %3.a heap.%heap% envf_%temp2%
- set temp=save_!%4_%1.value!
- set tempx=save2_!%4_%1.value!
- set tempvar=env_!%temp%!
- set tempvar2=env_!%tempx%!
- set mem_!%4_%1.value!_!%tempvar%!_!%tempvar2%!=!%3.a.ret!
- call :pop env_!%tempx%!
- call :pop env_!%temp%!
- set %2.ret=!%3.a.ret!
- goto :eof
- :yuan2_1
- set temp=save_!%4_%1.value!_var1_!%3.a.ret!
- call :push env_!%temp%!
- set env_!%temp%!=!%3.b.ret!
- set /a heap+=1
- call :expr 1 %3.b heap.%heap% envf_%temp2%_var1_!%3.a.ret!
- set temp=save_!%4_%1.value!_var1_!%3.a.ret!
- set temp2=env_!%temp%!
- set mem_!%4_%1.value!_!%3.a.ret!_!%temp2%!=!%3.b.ret!
- call :pop env_!%temp%!
- set %2.ret=!%3.b.ret!
- goto :eof
- :yuan2_2
- set temp=save_!%4_%1.value!_var2_!%3.b.ret!
- call :push env_!%temp%!
- set env_!%temp%!=!%3.a.ret!
- set /a heap+=1
- call :expr 1 %3.a heap.%heap% envf_%temp2%_!%3.b.ret!
- set temp=save_!%4_%1.value!_var2_!%3.b.ret!
- set temp2=env_!%temp%!
- set mem_!%4_%1.value!_!%temp2%!_!%3.b.ret!=!%3.a.ret!
- call :pop env_!%temp%!
- set %2.ret=!%3.a.ret!
- goto :eof
- :square %1 %2
- set /a ret=%1*%1+%2*%2
- goto :eof
- :negative
- set /a ret=0-%1
- goto :eof
- :max %1 %2
- if %1 geq %2 set ret=%1&goto :eof
- set ret=%2
- goto :eof
- :min %1 %2
- if %1 leq %2 set ret=%1&goto :eof
- set ret=%2
- goto :eof
- :cube
- set /a ret=%1*%1*%1
- goto :eof
- :mod
- set /a ret=%1%%%2
- goto :eof
- :sum %1 %2
- set ret=0
- for /l %%i in (%1 1 %2) do (
- set /a ret+=%%i
- )
- goto :eof
- :clear_heap
- for /l %%i in (1 1 %heap%) do (
- set heap.%%i.a.ret=
- set heap.%%i.a.new=
- set heap.%%i.b.ret=
- set heap.%%i.b.new=
- )
- set heap=1
- goto :eof
- :clear_state
- for /l %%i in (1 1 %state%) do (
- set input_%%i.type=
- set input_%%i.value=
- )
- goto :eof
- :push
- set stack.%stack%=!%1!
- set /a stack+=1
- goto :eof
- :pop
- set /a stack-=1
- set %1=!stack.%stack%!
- goto :eof
- :new1yuan
- if %input_3.type%==number (
- call :expr 6 get heap.1 input
- set mem_%input_1.value%_%input_3.value%=!get.ret!
- goto :eof
- )
- set save_%input_1.value%=%input_3.value%
- set acc=1
- for /l %%i in (6 1 %state%) do (
- set envf_%input_1.value%_!acc!.type=!input_%%i.type!
- set envf_%input_1.value%_!acc!.value=!input_%%i.value!
- set /a acc+=1
- )
- set yuan_%input_1.value%=1
- goto :eof
- :new2yuan
- if %input_3.type%==number ( if %input_5.type%==number (
- call :expr 8 get heap.1 input
- set mem_%input_1.value%_%input_3.value%_%input_5.value%=!get.ret!
- goto :eof
- ))
- if %input_3.type%==digit ( if %input_5.type%==number (
- set acc=1
- for /l %%i in (8 1 %state%) do (
- set envf_%input_1.value%_var2_%input_5.value%_!acc!.type=!input_%%i.type!
- set envf_%input_1.value%_var2_%input_5.value%_!acc!.value=!input_%%i.value!
- set /a acc+=1
- )
- set yuan_%input_1.value%=2
- set save_%input_1.value%_var2_%input_5.value%=%input_3.value%
- goto :eof
- ))
- if %input_3.type%==number ( if %input_5.type%==digit (
- set acc=1
- for /l %%i in (8 1 %state%) do (
- set envf_%input_1.value%_var1_%input_3.value%_!acc!.type=!input_%%i.type!
- set envf_%input_1.value%_var1_%input_3.value%_!acc!.value=!input_%%i.value!
- set /a acc+=1
- )
- set yuan_%input_1.value%=2
- set save_%input_1.value%_var1_%input_3.value%=%input_5.value%
- goto :eof
- ))
- set acc=1
- for /l %%i in (8 1 %state%) do (
- set envf_%input_1.value%_!acc!.type=!input_%%i.type!
- set envf_%input_1.value%_!acc!.value=!input_%%i.value!
- set /a acc+=1
- )
- set save_%input_1.value%=%input_3.value%
- set save2_%input_1.value%=%input_5.value%
- set yuan_%input_1.value%=2
- goto :eof
欢迎光临 批处理之家 (http://www.bathome.net/) | Powered by Discuz! 7.2 |