Board logo

标题: [共同参与] 批处理编写四项基本运算 [打印本页]

作者: youxi01     时间: 2007-1-16 12:28    标题: [共同参与] 批处理编写四项基本运算

要求:(我说的不清楚,举几个例子先)
9-5+(3×5)=
2+(3+0+3)=
(2+1+8)+1=
5-2+(3×5)=
.....
1、随机出现四个数(10以内就行了),对这四个数随机进行加减乘计算(除法因为存在除不尽的问题,暂不考虑)
2、随机出现括号
3、过滤结果为负数的情况
4、对计算结果进行统计计分
作者: zh159     时间: 2007-1-16 12:54
这个随机四项基本运算好像意义不大
作者: everest79     时间: 2007-1-16 15:53

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET CC=50
SET C0=9
SET C1=+-×÷+-*/()
FOR /L %%c IN (1,1,%CC%) DO (
CALL :R3 %C0%
FOR /L %%i IN (1,1,%C0%) DO (
CALL :RANDOM 10 %%i
CALL :RANDOM 4 X%%i
CALL :R2 X%%i
SET C3=!C3!!al%%i!!VAR%%i!!br%%i!!c!
SET CP=!CP!!al%%i!!VAR%%i!!br%%i!!b!)
SET /A C4=!C3! 2>nul
IF NOT !C4! LEQ 0 (
SET /A LOG1+=1,LOG2+=!C4!
IF %%c LSS 100 (SET E100=.) ELSE SET E100=
IF %%c LSS 10 (SET E10=.) ELSE SET E10=
ECHO ...!E100!!E10!%%c: !CP! = !C4!
)
SET C3=&SET CP=
)
SET /A LOG=LOG2/LOG1
ECHO _______________________________________________
ECHO Stat: %LOG1%/%CC% =^> %LOG2%/%LOG1%=%LOG%
pause>nul
GOTO :EOF

:RANDOM
SET /A VAR%2=%RANDOM%%%%1
IF !VAR%2! EQU 0 GOTO %0
GOTO :EOF

:R2
SET a=!VAR%1!
IF "%1" == "X%C0%" SET b=&&SET c=&&GOTO :EOF
SET /A x=%a%+4
SET b=!C1:~%a%,1!
SET c=!C1:~%x%,1!
GOTO :EOF

:R3
FOR /L %%i IN (0,1,%1) DO SET al%%i=&SET br%%i=
SET /A al0=%RANDOM%%%%1
IF %al0% LEQ 1 (
SET /A al0=1,sd=%1-1) ELSE (
SET sd=%1+1)
SET /A br0=%RANDOM%%%(%sd%-%al0%)
IF %br0% EQU 0 (
SET /A br0=%al0%+1) ELSE (
SET /A br0=%br0%+%al0%)
SET al%al0%=%C1:~8,1%
SET br%br0%=%C1:~9,1%
GOTO :EOF

作者: PPdos     时间: 2007-1-16 16:38
需判断取余是否为零
作者: everest79     时间: 2007-1-17 07:47
具体是指那的?
作者: namejm     时间: 2007-1-17 08:40
  帖一段容易看得明白一点的代码,换而言之,也就是比较臃肿的代码:
@echo off
setlocal enabledelayedexpansion

:begin
cls
set count=0
set item=0
set times=0
set expression=
set input1=
set input2=
set /p input1=     请指定参与运算的数字的个数(退出请直接按回车):
set /p input2=     请输入算术表达式可能出现的最大条数(退出请直接按回车):
if not defined input1 exit
if not defined input2 exit
cls

:loop
if %times% equ %input2% goto end
set expression=
set count=0
set str1=+-×
for /l %%i in (1,1,%input1%) do call :build
call :insert_sum
if %sum% lss 0 goto loop
set /a total+=%sum%&set /a item+=1
echo 第 %item% 条(%times%/%input2%): %expression:~1%=%sum%
goto loop

:end
set /a aver=%total%/%item%
set /a mod=%total%%%%item%
echo.
echo %item% 条记录的算术平均值是:%total%/%item%=%aver%……%mod%
echo.
echo          按任意键再来一次
pause>nul
goto begin


:build
:: 构造没有括号对的表达式
set /a num=%random%%%10
set /a SN=%random%%%3
set expression=!str1:~%SN%,1!%num%%expression%
goto :eof

:insert_sum
:: 随机插入括号对并计算每个表达式的结果
set /a length=2*%input1%
set /a num1=%random%%%%input1%
if %num1% equ 0 set num1=1
for /l %%i in (1,2,%length%) do (
    set /a count+=1
    if !count! equ %num1% set num1=%%i&goto next
)
:next
set count=0
set /a bg=%num1%+3
set /a remain=(%length%-%bg%)/2+1
if %remain% neq 1 (
    set /a num2=%random%%%%remain%+1
) else set num2=1
for /l %%i in (%bg%,2,%length%) do (
    set /a count+=1
    if !count! equ %num2% set num2=%%i&goto add
)
:add
set /a dif=%num2%-%num1%
set expression=!expression:~0,%num1%!(!expression:~%num1%,%dif%!)!expression:~%num2%!
set sum=%expression:+=+%
set sum=%sum:-=-%
set sum=%sum:×=*%
set /a sum=%sum:~1%
set /a times+=1
goto :eof

作者: netchxing     时间: 2007-1-17 10:29
太高深了,不懂!
作者: stornager     时间: 2007-4-2 04:55
好难啊,请楼主想一些简单的方法.菜了也在想??????/