[机器小伟]在[工程师阿伟]的陪同下进入了筑基中期的修炼,
这次要修炼的目标是[二次根式 小结与复习题]。
正剧开始:
星历2016年03月18日 12:19:31, 银河系厄尔斯星球*江南行省。
[工程师阿伟]正在和[机器小伟]一起研究[二次根式 小结与复习题]。
<span style="font-size:18px;"> 'x>=-3', 'x>1/2', 'x<2/3', 'x!=1', '10*5^[0.5]', '2*3x^[0.5]', '2*(2/3)^[0.5]', '(2/3)^[0.5]/a', 'xy*2y^[0.5]', '(5a/6)^[0.5]a^[2]', </span>
<span style="font-size:18px;">function myDraw(xGlobal, yGlobal) { var config = new PlotConfiguration(); config.init(); config.setPreference(); var r = 20; //config.setSector(1,1,1,1); //config.graphPaper2D(0, 0, r); //config.axis3D(0, 0,0,180); plot.setFillStyle('blue'); var mathText = new MathText(); var s = [ '(24^[0.5]-(1/2)^[0.5])-((1/8)^[0.5]+6^[0.5])', '2*12^[0.5]*3^[0.5]/4/(5*2^[0.5])', '(2*3^[0.5]+6^[0.5])*(2*3^[0.5]-6^[0.5])', '(2*48^[0.5]-3*27^[0.5])/6^[0.5]', '(2*2^[0.5]+3*3^[0.5])^[2]', '(3/2*(5/3)^[0.5]-(5/4)^[0.5])^[2]', ]; var x =40, y=40; var len = s.length; for (var i = 0; i < len; i++) { if (s[i] == '') { if (x < 100) { x += 300; y-=30*3; } else { x = 20; y += 30; } } else { mathText.print(s[i], x, y); y+=30; } } }</span>
<span style="font-size:18px;">def tmp(): s = ['(24^[0.5]-(1/2)^[0.5])-((1/8)^[0.5]+6^[0.5])', '2*12^[0.5]*3^[0.5]/4/(5*2^[0.5])', '(2*3^[0.5]+6^[0.5])*(2*3^[0.5]-6^[0.5])', '(2*48^[0.5]-3*27^[0.5])/6^[0.5]', '(2*2^[0.5]+3*3^[0.5])^[2]', '(3/2*(5/3)^[0.5]-(5/4)^[0.5])^[2]']; for i in range(len(s)): s1 = s[i].replace('^[', '**'); s1 = s1.replace(']', ''); print('{0} = {1}'.format(s1, round(eval(s1), 3)));</span>
<span style="font-size:18px;">>>> (24**0.5-(1/2)**0.5)-((1/8)**0.5+6**0.5) = 1.389 2*12**0.5*3**0.5/4/(5*2**0.5) = 0.424 (2*3**0.5+6**0.5)*(2*3**0.5-6**0.5) = 6.0 (2*48**0.5-3*27**0.5)/6**0.5 = -0.707 (2*2**0.5+3*3**0.5)**2 = 64.394 (3/2*(5/3)**0.5-(5/4)**0.5)**2 = 0.67</span>
<span style="font-size:18px;">>>> f = lambda x : x**2+5*x-6; >>> f(5**0.5-1); 1.7082039324993694 >>> f2 = lambda x : (7+4*3**0.5)*x**2+(2+3**0.5)*x+3**0.5; >>> f2(2-3**0.5); 3.732050807568878 >>> I = lambda Q, R, t: (Q/R/t)**0.5; >>> I(30, 5, 1); 2.449489742783178</span>
<span style="font-size:18px;">function myDraw(xGlobal, yGlobal) { var config = new PlotConfiguration(); config.init(); config.setPreference(); var r = 20; //config.setSector(1,1,1,1); //config.graphPaper2D(0, 0, r); //config.axis3D(0, 0,0,180); plot.setFillStyle('red'); var mathText = new MathText(); var s = [ 'PI*R_[1]^[2] = PI*R_[0]^[2]/4 => R_[1] = 1/2*R_[0]', 'PI*(R_[2]^[2]-R_[1]^[2]) = PI*R_[0]^[2]/4 => R_[2] = 2^[0.5]/2*R_[0]', 'R_[3] = 3^[0.5]/2*R_[0]', 'OA = R_[0]', 'OD = R_[1]', 'OC = R_[2]', 'OB = R_[3]', ]; var x =40, y=40; var r1 = 40; var len = s.length; for (var i = 0; i < len; i++) { if (s[i] == '') { if (x < 100) { x += 300; y-=r1*3; } else { x = 20; y += r1; } } else { mathText.print(s[i], x, y); y+=r1; } } }</span>
<span style="font-size:18px;">def tmp(): s = ['(24^[0.5]-(1/2)^[0.5])-((1/8)^[0.5]+6^[0.5])', '2*12^[0.5]*3^[0.5]/4/(5*2^[0.5])', '(2*3^[0.5]+6^[0.5])*(2*3^[0.5]-6^[0.5])', '(2*48^[0.5]-3*27^[0.5])/6^[0.5]', '(2*2^[0.5]+3*3^[0.5])^[2]', '(3/2*(5/3)^[0.5]-(5/4)^[0.5])^[2]']; for i in range(len(s)): s1 = s[i].replace('^[', '**'); s1 = s1.replace(']', ''); print('{0} = {1}'.format(s1, round(eval(s1), 3))); def tmp(): for i in range(1, 189): n = (189*i)**0.5; if (abs(n-round(n)) < 0.0001): print(i); break; >>> 21</span>
为了增加这个下标,小伟的工具又进化了一下:
<span style="font-size:18px;">/** * @usage 数学表达式,代数式的书写 * @author mw * @date 2016年03月12日 星期六 11:05:12 * @param * @return * */ function MathText() { //上标标记形式为...^[内容]... //分数不进行处理, 根式不进行处理,都转成指数式进行 //特殊数学符号设想加\[alpha]进行转义,待续 //可以进行指数上标代数式的书写 //可扩展下标,待续 this.setNormalFont = function() { plot.setFont("normal normal normal 24px Times Lt Std"); } this.setScriptFont = function() { plot.setFont("italic normal bold 16px Dark Courier "); } this.print = function(text, xPos, yPos) { xPos = xPos ? xPos : 0; yPos = yPos ? yPos : 0; plot.save(); var s = text ? text : ''; if (s != '') { s = s.replace(/\/\//ig, '÷'); s = s.replace(/>=/ig, '≥'); s = s.replace(/<=/ig, '≤'); s = s.replace(/!=/ig, '≠'); s = s.replace(/pi/ig, 'π'); } //字符串长度 var len = s.length; //不同字体大小设置在此 var r1 = 20; //单个字符暂存处 var c; //文本显示位置 var x = xPos, y = yPos; //正常文本暂存 var s0 = ''; //字符串打印长度 var measure; //记录上一个x位置,可记录三层 var xMem = [x, x, x]; //记录每一层的左括号位置 var bracketPos = [x, x, x]; //记录括号层次 var bracketsLevel = 0; //记录根号层次 var radicalLevel = 0; //设置正常字体 this.setNormalFont(); for (var i = 0; i < len; i++) { if (s[i] == '_') { //下标开始 //下标标记形式为..._[内容]... if (s0 != '') { //先把正常字符打印出 if (r1 != 20) { //字体字号大小还在上标状态 r1 = 20; this.setNormalFont(); } measure = plot.measureText(s0); plot.fillText(s0, x, y, measure); s0 = ''; x += measure; } var subScript = ''; var j = 0; for (j = i+1; s[j]!=']'; j++) { if (s[j] != '[') { subScript+=s[j]; } } if (r1 != 10) {//正常字体状态,需要改为上标字体 r1 = 10; this.setScriptFont(); } measure = plot.measureText(subScript); plot.fillText(subScript, x, y+8, measure); if (j < len-1 && s[j+1] == '^') { } else { x += 1.2*measure; } i = j; } else if (s[i] == '^') { //上标开始 //上标标记形式为...^[内容]... if (s0 != '') { //先把正常字符打印出 if (r1 != 20) { //字体字号大小还在上标状态 r1 = 20; this.setNormalFont(); } measure = plot.measureText(s0); plot.fillText(s0, x, y, measure); s0 = ''; x += measure; } var upperScript = ''; var j = 0; for (j = i+1; s[j]!=']'; j++) { if (s[j] != '[') { upperScript+=s[j]; } } var x1, y1; if (i > 0 && s[i-1] == ')') { x1 = bracketPos[bracketsLevel], y1 = y-20-5*radicalLevel; } else { x1 = xMem[bracketsLevel], y1 = y-20-5*radicalLevel; } //二次根式 if (upperScript == '1/2' || upperScript == '0.5') { plot.save() .setLineWidth(1); plot.beginPath() .moveTo(x1-5, y+5) .lineTo(x1-8, y-3) .moveTo(x1-5, y+5) .lineTo(x1+5, y1) .moveTo(x1+5, y1) .lineTo(x, y1) .closePath() .stroke(); plot.restore(); } else { if (r1 != 10) {//正常字体状态,需要改为上标字体 r1 = 10; this.setScriptFont(); } measure = plot.measureText(upperScript); plot.fillText(upperScript, x, y-8, measure); if (j < len-1 && s[j+1] == '_') { } else { x += 1.2*measure; } } //直接跳跃过上标字符区段 i = j; } else { c = s[i]; if (c == ')') { s0 += c; bracketsLevel -= 1; } else if (c == '(') { //如果整个括号被开根式,根号在括号左边 bracketPos[bracketsLevel] = x + plot.measureText(s0); s0 += c; bracketsLevel+=1; //过了括号就是过了一道关,要刷新坐标 xMem[bracketsLevel] = x + plot.measureText(s0); } else if (c == '+' || c == '-' || c == '*' || c == '/' || c == '÷' || c == '=' || c == ' ') { if (c == '*') { if (i > 0 && /[0-9]/.test(s[i-1]) && /[0-9]/.test(s[i+1])) { //对于乘号前后都是数字的情况,把乘号改成叉号 c = ' \u00D7 '; } else { //对于代数式中,乘号改为点号 c = ' \u00B7 '; } } //如果是运算符后的数被开根式,根号在运算符右边 if (c == '-' || c == '/') { s0 += ' '+c+' '; } else { s0 += c; } if (bracketsLevel < 3) { xMem[bracketsLevel] = x+plot.measureText(s0); } } else { s0 += c; } } } if (s0 != '') { //先把正常字符打印出 if (r1 != 20) { //字体字号大小还在上标状态 r1 = 20; this.setNormalFont(); } measure = plot.measureText(s0); plot.fillText(s0, x, y, measure); x += measure; } plot.restore(); } } </span>
基本上到这里也是个底了吧,再向里塞功能怕要撑破肚皮了。
本节到此结束,欲知后事如何,请看下回分解。