Board logo

标题: [数值计算] [分享]批处理计算系统 [打印本页]

作者: salad    时间: 2018-3-10 20:16     标题: [分享]批处理计算系统

这是更新了的四则运算,加减乘除等都行
例:
  1. 输入1+2+3
  2. 得6
复制代码
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. title
  4. color b0
  5. :beginn
  6. cls
  7. echo                           说明
  8. echo =============================================================
  9. echo a.函数
  10. echo.
  11. echo b.定义新运算
  12. echo.
  13. echo c.递归函数
  14. echo.
  15. echo d.等差/等比数列
  16. echo.
  17. echo e.最大公约数
  18. echo.
  19. echo =============================================================
  20. set /p a=输入想被帮助的项目(q.退出)(f.进入计算系统)
  21. if %a% equ a (
  22.    goto a
  23. )
  24. if %a% equ b (
  25.    goto b
  26. )
  27. if %a% equ c (
  28.    goto c
  29. )
  30. if %a% equ d (
  31.    goto d
  32. )
  33. if %a% equ e (
  34.    goto e
  35. )
  36. if %a% equ f (
  37.    goto f
  38. )
  39. if %a% equ q (
  40.    goto q
  41. )
  42. if %a% equ g (
  43.    goto g
  44. )
  45. :a
  46. echo negative函数:取反
  47. echo sum函数:求和
  48. echo max,min:取最大最小值
  49. echo mod:取余
  50. echo cube:3次方
  51. echo square:平方和
  52. pause>nul
  53. goto beginn
  54. :b
  55. start 1.jpg
  56. echo 如图所示,最多可支持二元函数的自定义
  57. pause>nul
  58. goto beginn
  59. :c
  60. start 2.jpg
  61. echo 如图所示
  62. pause>nul
  63. goto beggin
  64. :d
  65. echo 定义等比/等差数列
  66. echo 你们可以自己试一试,我就不发图了
  67. echo a(1)=1
  68. echo a(x)=a(x-1)*2
  69. echo a(1)=1
  70. echo a(x)=a(x-1)+2
  71. echo 还可以定义阶乘函数:
  72. echo factor(0)=1
  73. echo factor(1)=1
  74. echo factor(x)=factor(x-1)*x
  75. echo 自己试试
  76. pause>nul
  77. goto beginn
  78. :e
  79. echo 辗转相除法:
  80. echo **(x,0)=x
  81. echo **(x,y)=**(y,mod(x,y))
  82. echo 就这么简单
  83. pause>nul
  84. goto beggin
  85. :q
  86. exit
  87. :f
  88. cls
  89. rem initalize
  90. set stack=1
  91. set heap=1
  92. set number=1 2 3 4 5 6 7 8 9 0
  93. set addsub=+ -
  94. 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
  95. set env_pi=3
  96. set env_e=3
  97. set mem_test_1=100
  98. :entry
  99. set /p input=^>
  100. call :clear_heap
  101. call :clear_state
  102. set state=0
  103. :convert_vector
  104. if not defined input goto next
  105. set this=%input:~0,1%
  106. set input=%input:~1%
  107. for %%i in (%number%) do (
  108. 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))
  109. if !this!==%%i (
  110. set /a state+=1
  111. set input_!state!.value=%%i
  112. set input_!state!.type=number
  113. goto convert_vector
  114. ))
  115. for %%i in (%digit%) do (
  116. 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))
  117. if !this!==%%i (
  118. set /a state+=1
  119. set input_!state!.value=%%i
  120. set input_!state!.type=digit
  121. goto convert_vector
  122. ))
  123. if !this!==+ set /a state+=1&set input_!state!.type=+&goto convert_vector
  124. if !this!==-  set /a state+=1&set input_!state!.type=-&goto convert_vector
  125. if !this!==* set /a state+=1&set input_!state!.type=*&goto convert_vector
  126. if !this!==/ set /a state+=1&set input_!state!.type=/&goto convert_vector
  127. if "%this%"=="(" set /a state+=1&set input_!state!.type=(&goto convert_vector
  128. if "%this%"==")" set /a state+=1&set input_!state!.type=)&goto convert_vector
  129. if "%this%"=="=" set /a state+=1&set input_!state!.type==&goto convert_vector
  130. if "%this%"=="," set /a state+=1&set input_!state!.type=,&goto convert_vector
  131. if "%this%"==" " set input_%state%.next=1&goto convert_vector
  132. echo 词法分析错误:有无法分析的单词 :!this!
  133. :next
  134. if "%input_7.type%"=="=" (call :new2yuan
  135. goto entry)
  136. if "%input_5.type%"=="=" (call :new1yuan
  137. goto entry)
  138. if "%input_2.type%"=="=" (call :expr 3 ret heap.1 input
  139. if defined ret.ret (set env_%input_1.value%=!ret.ret!&goto entry) else (
  140. echo 定义变量错误
  141. goto entry))
  142. call :expr 1 ret heap.1 input
  143. set /a ret.new-=1
  144. if %ret.new% equ %state% ( echo !ret.ret!&goto entry ) else (
  145. echo 表达式计算错误
  146. goto entry)
  147. pause
  148. goto  entry
  149. goto :eof
  150. :expr %1 %2 %3 %4
  151. set /a heap+=1
  152. call :factor %1 %3.a heap.%heap% %4
  153. if not defined %3.a.ret goto :eof
  154. set newlocal=!%3.a.new!
  155. set op=!%4_%newlocal%.type!
  156. if not "%op%"=="+"  (if not "%op%"=="-" set %2.ret=!%3.a.ret!&set %2.new=!%3.a.new!&goto :eof)
  157. set /a newlocal=!%3.a.new!+1
  158. set /a heap+=1
  159. call :expr %newlocal% %3.b heap.%heap% %4
  160. if not defined %3.b.ret set %2.ret=!%3.a.ret!&set %2.new=!%3.a.new!&goto :eof
  161. set newlocal=!%3.a.new!
  162. set op=!%4_%newlocal%.type!
  163. if %op%==+ set /a %2.ret=%3.a.ret+%3.b.ret&set %2.new=!%3.b.new!&goto :eof
  164. if %op%==- set /a %2.ret=%3.a.ret-%3.b.ret&set %2.new=!%3.b.new!&goto :eof
  165. echo error :can't find '+' or '-'
  166. goto :eof
  167. :factor %1 %2 %3 %4
  168. set /a heap+=1
  169. call :term %1 %3.a heap.%heap% %4
  170. if not defined %3.a.ret goto :eof
  171. set newlocal=!%3.a.new!
  172. set op=!%4_%newlocal%.type!
  173. if not "%op%"=="*" (if not "%op%"=="/" set %2.ret=!%3.a.ret!&set %2.new=!%3.a.new!&goto :eof)
  174. set /a newlocal=!%3.a.new!+1
  175. set /a heap+=1
  176. call :factor %newlocal% %3.b heap.%heap% %4
  177. if not defined %3.b.ret set %2.ret=!%3.a.ret!&set %2.new=!%3.a.new!&goto :eof
  178. set newlocal=!%3.a.new!
  179. set op=!%4_%newlocal%.type!
  180. if %op%==* set /a %2.ret=%3.a.ret*%3.b.ret&set %2.new=!%3.b.new!&goto :eof
  181. if %op%==/ set /a %2.ret=%3.a.ret/%3.b.ret&set %2.new=!%3.b.new!&goto :eof
  182. echo error :can't find '*' or '/'
  183. goto :eof
  184. :term %1 %2 %3 %4
  185. if  !%4_%1.type!==number set %2.ret=!%4_%1.value!&set /a %2.new=%1+1
  186. if  !%4_%1.type!==digit call :var %1 %2 %3 %4&goto :eof
  187. if not !%4_%1.type!==( goto :eof
  188. set /a heap+=1
  189. set /a newlocal=%1+1
  190. call :expr %newlocal% %3.a heap.%heap% %4
  191. if not defined %3.a.ret goto :eof
  192. set /a newlocal=!%3.a.new!
  193. if not !%4_%newlocal%.type!==) goto :eof
  194. set %2.ret=!%3.a.ret!
  195. set /a newlocal+=1
  196. set %2.new=%newlocal%
  197. goto :eof
  198. :var %1 %2 %3 %4
  199. set temp=!%4_%1.value!
  200. set /a temp2=%1+1
  201. if not !%4_%temp2%.type!==( set %2.ret=!env_%temp%!&set /a %2.new=%1+1&goto :eof
  202. set /a temp2+=1
  203. set analyze_t=%temp%
  204. if !%4_%temp2%.type!==) set /a %2.new=temp+1&goto var_end
  205. set /a heap+=1
  206. call :push analyze_t
  207. call :expr %temp2% %3.a heap.%heap% %4
  208. call :pop analyze_t
  209. set analyze_t=%analyze_t% !%3.a.ret!
  210. set /a temp2=!%3.a.new!
  211. if !%4_%temp2%.type!==) set /a %2.new=temp2+1&goto var_end
  212. set /a heap+=1
  213. set /a temp2+=1
  214. call :push analyze_t
  215. call :expr %temp2% %3.b heap.%heap% %4
  216. call :pop analyze_t
  217. set analyze_t=%analyze_t% !%3.b.ret!
  218. set /a temp2=!%3.b.new!+1
  219. set %2.new=%temp2%
  220. :var_end
  221. set ret=
  222. call :%analyze_t% 2>nul
  223. if defined ret set %2.ret=%ret%&goto :eof
  224. set temp=mem_!%4_%1.value!_!%3.a.ret!
  225. if defined %temp% set %2.ret=!%temp%!&goto :eof
  226. set temp=mem_!%4_%1.value!_!%3.a.ret!_!%3.b.ret!
  227. if defined %temp% set %2.ret=!%temp%!&goto :eof
  228. set temp=save_!%4_%1.value!
  229. set temp2=!%4_%1.value!
  230. if !yuan_%temp2%! equ 2 goto yuan2
  231. call :push env_!%temp%!
  232. set env_!%temp%!=!%3.a.ret!
  233. set /a heap+=1
  234. call :expr 1 %3.a heap.%heap% envf_%temp2%
  235. set temp=save_!%4_%1.value!
  236. set temp2=env_!%temp%!
  237. set mem_!%4_%1.value!_!%temp2%!=!%3.a.ret!
  238. call :pop env_!%temp%!
  239. set %2.ret=!%3.a.ret!
  240. goto :eof
  241. :yuan2
  242. if defined envf_%temp2%_var1_!%3.a.ret!_1.type goto yuan2_1
  243. if defined envf_%temp2%_var2_!%3.b.ret!_1.type goto yuan2_2
  244. set temp3=save2_!%4_%1.value!
  245. call :push env_!%temp%!
  246. call :push env_!%temp3%!
  247. set env_!%temp%!=!%3.a.ret!
  248. set env_!%temp3%!=!%3.b.ret!
  249. set /a heap+=1
  250. call :expr 1 %3.a heap.%heap% envf_%temp2%
  251. set temp=save_!%4_%1.value!
  252. set tempx=save2_!%4_%1.value!
  253. set tempvar=env_!%temp%!
  254. set tempvar2=env_!%tempx%!
  255. set mem_!%4_%1.value!_!%tempvar%!_!%tempvar2%!=!%3.a.ret!
  256. call :pop env_!%tempx%!
  257. call :pop env_!%temp%!
  258. set %2.ret=!%3.a.ret!
  259. goto :eof
  260. :yuan2_1
  261. set temp=save_!%4_%1.value!_var1_!%3.a.ret!
  262. call :push env_!%temp%!
  263. set env_!%temp%!=!%3.b.ret!
  264. set /a heap+=1
  265. call :expr 1 %3.b heap.%heap% envf_%temp2%_var1_!%3.a.ret!
  266. set temp=save_!%4_%1.value!_var1_!%3.a.ret!
  267. set temp2=env_!%temp%!
  268. set mem_!%4_%1.value!_!%3.a.ret!_!%temp2%!=!%3.b.ret!
  269. call :pop env_!%temp%!
  270. set %2.ret=!%3.b.ret!
  271. goto :eof
  272. :yuan2_2
  273. set temp=save_!%4_%1.value!_var2_!%3.b.ret!
  274. call :push env_!%temp%!
  275. set env_!%temp%!=!%3.a.ret!
  276. set /a heap+=1
  277. call :expr 1 %3.a heap.%heap% envf_%temp2%_!%3.b.ret!
  278. set temp=save_!%4_%1.value!_var2_!%3.b.ret!
  279. set temp2=env_!%temp%!
  280. set mem_!%4_%1.value!_!%temp2%!_!%3.b.ret!=!%3.a.ret!
  281. call :pop env_!%temp%!
  282. set %2.ret=!%3.a.ret!
  283. goto :eof
  284. :square %1 %2
  285. set /a ret=%1*%1+%2*%2
  286. goto :eof
  287. :negative
  288. set /a ret=0-%1
  289. goto :eof
  290. :max %1 %2
  291. if %1 geq %2 set ret=%1&goto :eof
  292. set ret=%2
  293. goto :eof
  294. :min %1 %2
  295. if %1 leq %2 set ret=%1&goto :eof
  296. set ret=%2
  297. goto :eof
  298. :cube
  299. set /a ret=%1*%1*%1
  300. goto :eof
  301. :mod
  302. set /a ret=%1%%%2
  303. goto :eof
  304. :sum %1 %2
  305. set ret=0
  306. for /l %%i in (%1 1 %2) do (
  307. set /a ret+=%%i
  308. )
  309. goto :eof
  310. :clear_heap
  311. for /l %%i in (1 1 %heap%) do (
  312. set heap.%%i.a.ret=
  313. set heap.%%i.a.new=
  314. set heap.%%i.b.ret=
  315. set heap.%%i.b.new=
  316. )
  317. set heap=1
  318. goto :eof
  319. :clear_state
  320. for /l %%i in (1 1 %state%) do (
  321. set input_%%i.type=
  322. set input_%%i.value=
  323. )
  324. goto :eof
  325. :push
  326. set stack.%stack%=!%1!
  327. set /a stack+=1
  328. goto :eof
  329. :pop
  330. set /a stack-=1
  331. set %1=!stack.%stack%!
  332. goto :eof
  333. :new1yuan
  334. if %input_3.type%==number (
  335. call :expr 6 get heap.1 input
  336. set mem_%input_1.value%_%input_3.value%=!get.ret!
  337. goto :eof
  338. )
  339. set save_%input_1.value%=%input_3.value%
  340. set acc=1
  341. for /l %%i in (6 1 %state%) do (
  342. set envf_%input_1.value%_!acc!.type=!input_%%i.type!
  343. set envf_%input_1.value%_!acc!.value=!input_%%i.value!
  344. set /a acc+=1
  345. )
  346. set yuan_%input_1.value%=1
  347. goto :eof
  348. :new2yuan
  349. if %input_3.type%==number ( if %input_5.type%==number (
  350. call :expr 8 get heap.1 input
  351. set mem_%input_1.value%_%input_3.value%_%input_5.value%=!get.ret!
  352. goto :eof
  353. ))
  354. if %input_3.type%==digit ( if %input_5.type%==number (
  355. set acc=1
  356. for /l %%i in (8 1 %state%) do (
  357. set envf_%input_1.value%_var2_%input_5.value%_!acc!.type=!input_%%i.type!
  358. set envf_%input_1.value%_var2_%input_5.value%_!acc!.value=!input_%%i.value!
  359. set /a acc+=1
  360. )
  361. set yuan_%input_1.value%=2
  362. set save_%input_1.value%_var2_%input_5.value%=%input_3.value%
  363. goto :eof
  364. ))
  365. if %input_3.type%==number ( if %input_5.type%==digit (
  366. set acc=1
  367. for /l %%i in (8 1 %state%) do (
  368. set envf_%input_1.value%_var1_%input_3.value%_!acc!.type=!input_%%i.type!
  369. set envf_%input_1.value%_var1_%input_3.value%_!acc!.value=!input_%%i.value!
  370. set /a acc+=1
  371. )
  372. set yuan_%input_1.value%=2
  373. set save_%input_1.value%_var1_%input_3.value%=%input_5.value%
  374. goto :eof
  375. ))
  376. set acc=1
  377. for /l %%i in (8 1 %state%) do (
  378. set envf_%input_1.value%_!acc!.type=!input_%%i.type!
  379. set envf_%input_1.value%_!acc!.value=!input_%%i.value!
  380. set /a acc+=1
  381. )
  382. set save_%input_1.value%=%input_3.value%
  383. set save2_%input_1.value%=%input_5.value%
  384. set yuan_%input_1.value%=2
  385. goto :eof
复制代码
大家看看这是主代码




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2