'********************************************* '* Elementary operations on fractions * '* ----------------------------------------- * '* This program allows user performing * '* operations on fractions such as: * '* 3/4 x 8/9 - 4/5 * '* * '* Sample run: * '* * '* ? 3,4 * '* ? * * '* ? 8,9 * '* ? + * '* ? -4,5 * '* ? = * '* Result is -2/15 * '* ----------------------------------------- * '* Ref.: "Mathématiques par l'informatique * '* individuelle (1) par H. Lehning * '* et D. Jakubowicz, Masson, Paris, * '* 1982" [BIBLI 06]. * '********************************************* cls print '*** enter first fraction p/q (p=0 to exit) *** 10 input p,q : if p=0 or q=0 then end '*** enter operator: ' = to view current result p/q ' s to save current result ' +,-,*,/ arithmetic operator '*** 20 input c$ if c$="=" then print "Result is ";p;"/";q:goto 20 if c$="s" then g=p:h=q:goto 10 '*** enter next fraction r/s (if s=0, take current stored result) *** 50 input r,s : if r=0 then end if s=0 then r=g:s=h 'computing result if c$="+" then gosub 310 if c$="-" then gosub 300 if c$="*" then gosub 360 if c$="/" then gosub 330 gosub 390 goto 20 300 r=-r 310 p=p*s+q*r goto 370 330 b=r r=s:s=b 360 p=p*r 370 q=q*s 'common denominator for all operations return 390 t=p 'simplification section u=q 400 b=t-u*int(t/u) if b=0 then goto 450 t=u:u=b goto 400 450 p=p/u:q=q/u return 'end of file fraction.bas