longdiv.prg

来自「用VFP计算平方根(例如sqtr(2))到6万5千位小数的程序.」· PRG 代码 · 共 50 行

PRG
50
字号
* longdiv.prg
* 长除法
parameters a,b
private except eachlen,alllen
* 处理左边的0
l0=len(a)
not0=0
for t=1 to l0
   if substr(a,t,1)="0"
      not0=not0+1
   else
      exit
   endif
next t
a=substr(a,not0+1,l0-not0)
b=substr(b,not0+1,l0-not0)
*eachlen=10
*alllen=len(a)
*s0=replicate("0",alllen)
longdivc=""                 && 商
longdivi=0                  && 除数移位计数
c0=0                 && 部分商
@ 7,10 say "longdiv除数已回缩到第           位"
do while .t.
@ 7,31 say str(longdivi)
   if a>=b
      a=longsub(a,b)
      c0=c0+1
   else
      longdivc=longdivc+str(c0,1)  && 搜集商
      longdivi=longdivi+1
      l=right(b,1)            &&最后一位四舍五入
      if l>"4"
         l2=str(val(substr(b,alllen-1,1))+1,1)
         b="0"+left(b,alllen-2)+l2
      else
         b="0"+left(b,alllen-1)  &&除数右移1位
      endif
      c0=0
   endif
   if longdivi>=alllen             &&.or.a=s0.or.b=s0
      exit
   endif
enddo
a=len(longdivc)
if a<alllen
   longdivc=longdivc+replicate("0",alllen-a)
endif
return longdivc

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?