正剧开始:
星历2016年02月25日 14:48:26, 银河系厄尔斯星球中华帝国江南行省。
[工程师阿伟]正在和[机器小伟]一起进行有理数的复习。
<span style="font-size:18px;">//题1 function myDraw() { var config = new PlotConfiguration(); config.init(); config.setPreference(); config.setSector(1,1,1,1); config.axis1D(0, 0, 280); var r = 2*(280-10); //刻度 scale; graduation ; var scale = r / 10; var array = [3.5,-3.5,0,2,-2,-1.6,-0.33,0.5]; var size = array.length; array.sort(function(a, b) { return a-b;}); plot.fillText(array.join(' < '), -240, 60, 400); //存放点阵列 var points = []; for (var i = 0; i < size; i++) { points.push([array[i]*scale, 0]); } //由于shape采用的是弹出数据的方式,所以拷贝一个副本来进行操作 var tmp = points; shape.pointDraw(tmp, 'green'); for (var i = 0; i < size; i++) { plot.fillText(array[i].toString(), array[i]*scale-10, 20, 50); } } </span>
<span style="font-size:18px;">//题2 function myDraw() { var config = new PlotConfiguration(); config.init(); config.setPreference(); config.setSector(1,1,1,1); config.axis1D(0, 0, 280); var r = 2*(280-10); //刻度 scale; graduation ; var scale = r / 10; var array = []; for (var i = -2; i < 4; i++) { array.push(i); } var size = array.length; array.sort(function(a, b) { return a-b;}); //plot.fillText(array.join(' < '), -240, 60, 400); //存放点阵列 var points = []; for (var i = 0; i < size; i++) { points.push([array[i]*scale, 0]); } //由于shape采用的是弹出数据的方式,所以拷贝一个副本来进行操作 var tmp = points; shape.pointDraw(tmp, 'green'); for (var i = 0; i < size; i++) { plot.fillText(array[i].toString(), array[i]*scale-5, 20, 50); } }</span>
<span style="font-size:18px;">>>> -2, 绝对值是2,相反数是2,倒数是-0.5。 -0.67, 绝对值是0.67,相反数是0.67,倒数是-1.5。 5.5, 绝对值是5.5,相反数是-5.5,倒数是0.18。 #题3 def tmp(num): print('{0}, 绝对值是{1},相反数是{2},倒数是{3}。'.format(\ round(num,2), round(abs(num),2), round(-num, 2), round(1/num, 2))); return; if __name__ == '__main__': a = -2; b=-2/3; c = 5.5; tmp(a); tmp(b); tmp(c);</span>
<span style="font-size:18px;">#题5 -150+250 = 100, -15+(-23) = -38, -5-65 = -70, -26-(-15) = -11, -6*(-16) = 96, -1/3*27 = -9 = -900%, 8/(-16) = -1/2 = -0.5 = -50%, -25/(-2/3) = 75/2 = 37.5, (-0.02)*(-20)*(-5)*4.5 = -9 = -900%, (-6.5)*(-2)/(-1/3)/(-5) = 39/5 = 7.8 = 780%, 6+(-1/5)-2-(-1.5) = 53/10 = 5.3 = 530%, -66*4-(-2.5)/(-0.1) = -289, (-2)**2*5-(-2)**3/4 = 22, -(3-5)+3**2*(1-3) = -16, </span>
<span style="font-size:18px;">>>> 245.635精确到小数点后1位是245.6。 175.65精确到小数点后0位是176。 12.004精确到小数点后2位是12.00。 6.5378精确到小数点后2位是6.54。 #近似数 def approximate(num, accuracy): if accuracy > 0: result = str(round(num, accuracy)); else: result = str(round(num)); #对于要求保留小数的情况,由于一般末尾的0会被舍弃, #导致精度位数不够,所以在此进行判断补充。 if accuracy > 0: pos = result.find('.'); diff = accuracy-(len(result)-pos-1); result += '0'*diff; print('{0}精确到小数点后{1}位是{2}。'.format(num, accuracy, result)); #题6 def tmp(): approximate(245.635,1); approximate(175.65, 0); approximate(12.004,2); approximate(6.5378, 2);</span>
<span style="font-size:18px;">>>> 100000000 用科学计数法表示是 1e+08 -4500000 用科学计数法表示是 -4.50e+06 692400000000 用科学计数法表示是 6.924e+11 #题7 def tmp(): a = 100000000; print('{0} 用科学计数法表示是 {1:.0e}'.format(a, a)); a = -4500000; print('{0} 用科学计数法表示是 {1:.2e}'.format(a, a)); a = 692400000000; print('{0} 用科学计数法表示是 {1:.3e}'.format(a, a)); >>> -5 1</span>
<span style="font-size:18px;">#题8 def tmp(): print(-2-abs(-3)); print(abs(-2-(-3)));</span>
<span style="font-size:18px;">#题9 82+83+78+66+95+75+56+93+82+81 = 791, (82+83+78+66+95+75+56+93+82+81)/10 = 791/10 = 79.1, #题11 458-(-27.8-70.3+200+138.1-8+188) = 38, </span>
<span style="font-size:18px;">//题12 function myDraw() { var config = new PlotConfiguration(); config.init(); config.setPreference(); config.setSector(1,1,1,1); config.axis2D(0, 0, 180); var array = []; array.push([15, -15*2]); array.push([60, -60*2]); array.push([115, -5*2]); var tmp = [].concat(array); shape.pointDraw(tmp, 'red'); tmp = [].concat(array); shape.strokeDraw(tmp, 'red'); }</span>
<span style="font-size:18px;">>>> 149600000.0 用科学计数法表示是 1.496e+08 #题13 def tmp(): a = 1.4960*10**8; print('{0} 用科学计数法表示是 {1:.3e}'.format(a, a));</span>
<span style="font-size:18px;">>>> 1 * 1 = 1 11 * 11 = 121 111 * 111 = 12321 1111 * 1111 = 1234321 11111 * 11111 = 123454321 111111 * 111111 = 12345654321 1111111 * 1111111 = 1234567654321 11111111 * 11111111 = 123456787654321 111111111 * 111111111 = 12345678987654321 1111111111 * 1111111111 = 1234567900987654321 11111111111 * 11111111111 = 123456790120987654321 111111111111 * 111111111111 = 12345679012320987654321 1111111111111 * 1111111111111 = 1234567901234320987654321 11111111111111 * 11111111111111 = 123456790123454320987654321 111111111111111 * 111111111111111 = 12345679012345654320987654321 1111111111111111 * 1111111111111111 = 1234567901234567654320987654321 #题16 def tmp(): a = []; a.append(1); for i in range(15): b = a[-1]; a.append(b*10+1); size = len(a); for i in range(size): print('{0} * {0} = {1}'.format(a[i], a[i]*a[i])); </span>
<span style="font-size:18px;">### # @usage 百分数,分数, 小数各结果的运算 # @author mw # @date 2016年02月19日 星期五 10:42:16 # @param # @return # ### def percentCalc(): # 可以将任意的四则运算表达式用分数进行计算, # 而不是将浮点结果简单转化成分数。 fin = open('input.txt'); fout= open('output.txt', 'a'); #结果为整数,此时小数表示和分数结果相同。 resultIsInt = False; #结果绝对值大于10, 此时用百分数表示不合适。 noPercentExpr = False; for line in fin.readlines(): if line[-1] == '\n': line = line[:-1]; if line == '': continue; elif line.startswith('#'): print(line); fout.write(line+'\n'); else: try: lines = line.split(sep=' '); for i in range(len(lines)): if lines[i]=='': continue; #消除空格 lines[i] = lines[i].replace(' ', ''); expCount = len(lines[i]); expression = ''; resultIsInt = False; noPercentExpr = False; originExpr = lines[i]; #看表达式中是否有百分号 havePercentOp = False; if lines[i].find('%') != -1: havePercentOp = True; OpsArray = []; if havePercentOp == True: for j in range(expCount): #操作符直接添加 if isOps(lines[i][j]): OpsArray.append(lines[i][j]); #处理百分号 if lines[i][j] == '%': if len(OpsArray) > 0 and OpsArray[-1] == '/': expression+='*100'; else: expression+='/100'; else: expression+=lines[i][j]; lines[i] = expression; expCount = len(lines[i]); expression = ''; #小数结果 floatResult = 0; #百分数结果 percentResult = 0; result = eval(lines[i]); if (abs(result)>10): noPercentExpr = True; if (abs(result - int(result)) < 0.000001): resultIsInt = True; #小数结果 floatResult = int(result); percentResult = int(result)*100; else: #小数结果 floatResult = round(result, 3); result2 = result * 100; if (abs(result2 - int(result2)) < 0.000001): percentResult = int(result2); else: percentResult = round(result2, 3); #分割操作数 operands = []; stmp = ''; indexBeg = 0; indexEnd = 0; for j in range(expCount): if isOps(lines[i][j]): if stmp != '': operands.append(stmp); stmp = ''; else: stmp+=lines[i][j]; if stmp != '': operands.append(stmp); print(operands); #操作数修饰 operandCount = len(operands); operandString = []; #数字如1/7要转化成Fraction('1/7')这种样式才可以算出正确结果,引号不可以少掉。 for j in range(operandCount): stmp = ''; stmp = 'Fraction(\''+operands[j]+'\')'; operandString.append(stmp); #组装新表达式 typechange = 1; index = 0; expression = ''; for j in range(expCount): #操作符直接添加 if isOps(lines[i][j]): #记录操作符 OpsArray.append(lines[i][j]); expression+=lines[i][j]; else: if j > 0 and typechange == 0 and isOps(lines[i][j-1]): typechange = 1; #从操作数序列中选择对应操作数。 if typechange == 1: if index > len(operandString): break; expression += operandString[index]; index+=1; typechange = 0; #设置打印格式 if noPercentExpr == True: if resultIsInt == True: s = '{0} = {1}'.format(originExpr,eval(expression)); else: s = '{0} = {1} = {2}'.format(originExpr,eval(expression), floatResult); else: if resultIsInt == True: s = '{0} = {1} = {2}%'.format(originExpr,eval(expression),percentResult); else: s = '{0} = {1} = {2} = {3}%'.format(originExpr,eval(expression), floatResult, percentResult); print(s, end=', '); fout.write(s + ', '); print('\n'); fout.write('\n'); except: #traceback.print_exc(); ex = '{0} 表达式有误,无法进行计算。'.format(lines[i]); print(ex); fout.write(ex); fout.write('\n'); fout.close(); fin.close(); </span>