Blockly的实际应用
在课堂中我学会了运用Blockly来解决简单的算法问题,接下来便将运用Blockly来对π/4在误差范围内进行近似求值。
首先要知道π/4近似等于1-1/3+1/5-1/7+……+(-1)^(x+1)·[1/(2x+1)]。
注:因为此程序中的初始赋值为1,所以将通项改为(-1)^x·[1/(2x+10]。
XML代码:<xml xmlns="[http://www.w3.org/1999/xhtml">](http://www.w3.org/1999/xhtml">)**;
**
<variables>
<variable id="ebV92L\#%auU\)JspR{3\[9" type="">x</variable>
<variable id="}fGdqrSohSlCbR.b$P1F" type="">y</variable>
</variables>
<block id="{@-]qI*vKQmn7A@Dz$;i" type="variables_set" x="-87" y="-38">
<field id="ebV92L\#%auU\)JspR{3\[9" name="VAR" variableType="">x</field>
<value name="VALUE">
<block id="[email protected]\|Tp.1Zv" type="math\_number">
<field name="NUM">1</field>
</block>
</value>
<next>
<block id="K\`2P!I\|nfC\#MWg\`zf-AM" type="variables\_set">
<field id="}fGdqrSohSlCbR.b$P1F" name="VAR" variableType="">y</field>
<value name="VALUE">
<block id="fQ?{.5eAR{:nPZU\]s=SJ" type="math\_number">
<field name="NUM">1</field>
</block>
</value>
<next>
<block id="o\#dlXpbL\#:argV8VsG3\|" type="controls\_if">
<mutation else="1" />
<value name="IF0">
<block id="Zxr}\(=H\`\*KpSW~q\(gaEI" type="logic\_compare">
<field name="OP">LT</field>
<value name="A">
<block id="5UjH\#9bf4t3w$%\|xQ\`=c" type="math\_arithmetic">
<field name="OP">POWER</field>
<value name="A">
<shadow id="$3:\[N1=\[E=YYm1QgP{DI" type="math\_number">
<field name="NUM">1</field>
</shadow>
<block id="--LzQ;Xg8K/xFVdv-^rO" type="variables\_get">
<field id="}fGdqrSohSlCbR.b$P1F" name="VAR" variableType="">y</field>
</block>
</value>
<value name="B">
<shadow id="\(\|;FPVvqcwanzdk\|GCO;" type="math\_number">
<field name="NUM">1</field>
</shadow>
<block id="f-tU^\_\|x\]e1\]!9;\)PST8" type="math\_arithmetic">
<field name="OP">DIVIDE</field>
<value name="A">
<shadow id="bqSvO\`N\|E\*hq7{Cp~Iqo" type="math\_number">
<field name="NUM">1</field>
</shadow>
<block id="qL{9aG/;vZN;S\#Ap0\#ES" type="math\_constant">
<field name="CONSTANT">PI</field>
</block>
</value>
<value name="B">
<shadow id="UMjBcPa6,\]Y2\*YGuX\[%{" type="math\_number">
<field name="NUM">1</field>
</shadow>
<block id="9W\[FP\*,9/\`5\|gF\)cDgP!" type="math\_number">
<field name="NUM">4</field>
</block>
</value>
</block>
</value>
</block>
</value>
<value name="B">
<block id="L^hQoLujWh.-\*Z,U\]\[kZ" type="math\_number">
<field name="NUM">0.12</field>
</block>
</value>
</block>
</value>
<statement name="DO0">
<block id="7~xo^PoqR0Ach\*8Wm2@b" type="text\_print">
<value name="TEXT">
<shadow id="{FMJAmDB\(q\*+qD8,8Vo:" type="text">
<field name="TEXT">abc</field>
</shadow>
<block id="nv{T7@tF,\`2IH/dY=:h/" type="variables\_get">
<field id="}fGdqrSohSlCbR.b$P1F" name="VAR" variableType="">y</field>
</block>
</value>
</block>
</statement>
<statement name="ELSE">
<block id="ved\_-!uG7cPDpM,oKL\)M" type="controls\_whileUntil">
<field name="MODE">UNTIL</field>
<value name="BOOL">
<block id="e;1CgVq~\|C46tv~JI\*Sg" type="logic\_compare">
<field name="OP">LT</field>
<value name="A">
<block id="9RQXia4v$Vd=\)i@:nF%\(" type="math\_arithmetic">
<field name="OP">MINUS</field>
<value name="A">
<shadow id="@@\[-\*B00%mlP:\*J\]c@nS" type="math\_number">
<field name="NUM">1</field>
</shadow>
<block id="IK\[U02{s^8dMa4N\|hsu3" type="variables\_get">
<field id="}fGdqrSohSlCbR.b$P1F" name="VAR" variableType="">y</field>
</block>
</value>
<value name="B">
<shadow id="\*\]pIQyl:p4,PM:Sye\]bL" type="math\_number">
<field name="NUM">1</field>
</shadow>
<block id="\|P8.UCoO$\`Qh!T\]I,Kp\`" type="math\_arithmetic">
<field name="OP">DIVIDE</field>
<value name="A">
<shadow id="WKxWQQb/L/\`Ecm+4P9l1" type="math\_number">
<field name="NUM">1</field>
</shadow>
<block id="}mzm}tBYB28X/t;%.W.." type="math\_constant">
<field name="CONSTANT">PI</field>
</block>
</value>
<value name="B">
<shadow id="\]\(q6c\_S.O5x2\]\]v{\`E\*Q" type="math\_number">
<field name="NUM">1</field>
</shadow>
<block id="4e;a-a9v\|x^\)O5~D6\#F0" type="math\_number">
<field name="NUM">4</field>
</block>
</value>
</block>
</value>
</block>
</value>
<value name="B">
<block id="O.iLXu9qyI?/eg\*JJYbN" type="math\_number">
<field name="NUM">0.12</field>
</block>
</value>
</block>
</value>
<statement name="DO">
<block id=";\[PYcuyd/mXT$HuyY\|\*S" type="variables\_set">
<field id="}fGdqrSohSlCbR.b$P1F" name="VAR" variableType="">y</field>
<value name="VALUE">
<block id="v2YqxEb;UyYOZULRrY\*d" type="math\_arithmetic">
<field name="OP">ADD</field>
<value name="A">
<shadow id="NZk2$?\)ueE}s5t\)}A6gl" type="math\_number">
<field name="NUM">1</field>
</shadow>
<block id="yeuaXRuSNI.v3AgdD\`O\[" type="variables\_get">
<field id="}fGdqrSohSlCbR.b$P1F" name="VAR" variableType="">y</field>
</block>
</value>
<value name="B">
<shadow id="@zGdsvAnQa\*d}.qjY\*w\)" type="math\_number">
<field name="NUM">1</field>
</shadow>
<block id="t\]qXaWV{/s6qhTaOu$-\_" type="math\_arithmetic">
<field name="OP">MULTIPLY</field>
<value name="A">
<shadow id="}ZocQC2O\_2RtKAy\`Xz!d" type="math\_number">
<field name="NUM">1</field>
</shadow>
<block id="8pNVkq\|k\(l,l\_5S88Hy2" type="math\_arithmetic">
<field name="OP">POWER</field>
<value name="A">
<shadow id="%9bDe74,a,L\)k4se6\`3q" type="math\_number">
<field name="NUM">1</field>
</shadow>
<block id="1%q~156\*kR3oO6BtvZ\[\]" type="math\_number">
<field name="NUM">-1</field>
</block>
</value>
<value name="B">
<shadow id="u\#\#P{SnhZt5\#x{Mx1kOo" type="math\_number">
<field name="NUM">1</field>
</shadow>
<block id="1nR,VmTfjZW5rzOYV.0F" type="variables\_get">
<field id="ebV92L\#%auU\)JspR{3\[9" name="VAR" variableType="">x</field>
</block>
</value>
</block>
</value>
<value name="B">
<shadow id="~YdgvTk5Z\|0y\)zX7o1+K" type="math\_number">
<field name="NUM">1</field>
</shadow>
<block id="VdoRM-\`PtK\]uX\*nC6J\*O" type="math\_arithmetic">
<field name="OP">DIVIDE</field>
<value name="A">
<shadow id="-;-dNsE~LA:Rnpb}Gh3J" type="math\_number">
<field name="NUM">1</field>
</shadow>
<block id="\)mb@R-xh\[jFax+$DC?l0" type="math\_number">
<field name="NUM">-1</field>
</block>
</value>
<value name="B">
<shadow id="+RSL1mmlSrk;Ms\`~UhLb" type="math\_number">
<field name="NUM">1</field>
</shadow>
<block id="vi$JK%G4p+:\|Tz\]uJN9M" type="math\_arithmetic">
<field name="OP">ADD</field>
<value name="A">
<shadow id="}GTh?CY3WIk-CrF8.z$\*" type="math\_number">
<field name="NUM">1</field>
</shadow>
<block id="vvs=~nd\_dc\[-:}EnogC3" type="math\_arithmetic">
<field name="OP">ADD</field>
<value name="A">
<shadow id="y:fi+/~ov\*h70u8CaHWw" type="math\_number">
<field name="NUM">1</field>
</shadow>
<block id="FT5\[S\|TgT8DetzG@7kTR" type="math\_number">
<field name="NUM">2</field>
</block>
</value>
<value name="B">
<shadow id="p4dX5Q-\_\#V=w!\#I+\)I@n" type="math\_number">
<field name="NUM">1</field>
</shadow>
<block id="HGaL!;dy2^IGn40j?Zmg" type="variables\_get">
<field id="ebV92L\#%auU\)JspR{3\[9" name="VAR" variableType="">x</field>
</block>
</value>
</block>
</value>
<value name="B">
<shadow id="y9!7nCtX7+}tD717P\#q~" type="math\_number">
<field name="NUM">1</field>
</shadow>
<block id="rO4U50\`}w6\*%A9sX$hOf" type="math\_number">
<field name="NUM">1</field>
</block>
</value>
</block>
</value>
</block>
</value>
</block>
</value>
</block>
</value>
<next>
<block id="pUUVM\[^6b\[?viM8zU%sK" type="variables\_set">
<field id="ebV92L\#%auU\)JspR{3\[9" name="VAR" variableType="">x</field>
<value name="VALUE">
<block id="Xam~y?Go%:9}::C,g7E-" type="math\_arithmetic">
<field name="OP">ADD</field>
<value name="A">
<shadow id="C+\]W\)\)jq\#\`txL?$$Hs.Y" type="math\_number">
<field name="NUM">1</field>
</shadow>
<block id="WiVqXRrm+SJ0nr,M\_X\|W" type="variables\_get">
<field id="ebV92L\#%auU\)JspR{3\[9" name="VAR" variableType="">x</field>
</block>
</value>
<value name="B">
<shadow id=",Q\`O/Nk}xRQFHL^YtjlO" type="math\_number">
<field name="NUM">1</field>
</shadow>
<block id="}sOi@3P3PF$w\(yDF5B.;" type="math\_number">
<field name="NUM">1</field>
</block>
</value>
</block>
</value>
</block>
</next>
</block>
</statement>
<next>
<block id="0@2\_DvV+I=lXe7{o^2wP" type="text\_print">
<value name="TEXT">
<shadow id="p$0Q\(;oc@}:\`.6npsi2\(" type="text">
<field name="TEXT">abc</field>
</shadow>
<block id="fkVFH=6SE~;ZE0E\|^zO7" type="variables\_get">
<field id="}fGdqrSohSlCbR.b$P1F" name="VAR" variableType="">y</field>
</block>
</value>
</block>
</next>
</block>
</statement>
</block>
</next>
</block>
</next>
首先将初始变量x赋值为1,初始变量y赋值为1,限定误差范围小于0.12,因此如果y-π/4<0.12,则直接打印出y,否则执另一个命令,即进入循环结构。赋值y到y+(-1)^x·[1/(2x+1)],赋值x到x+1,重复直到y-π/4<0.12。打印y,此时输出的y值即为π/4的近似值。