正剧开始:
星历2016年01月15日 09:00:39, 银河系厄尔斯星球中华帝国江南行省。
[工程师阿伟]正在和[机器小伟]一起研究分数。
由于小朋友们在吃着西瓜,小伟判断这是在夏天,应该是暑假吧,那怎么还戴着这种棉帽呢?
<span style="font-size:18px;">def fractionCalc(): a = Fraction(1, 4); print(a); b = Fraction(1, 3); print(b); </span>
<span style="font-size:18px;">>>> 1/2 1/4 1/6 1/2 > 1/4 1/4 > 1/6 ### # @usage 分数相关的运算 # @author mw # @date 2016年01月15日 星期五 09:04:31 # @param # @return # ### def fractionCalc(): array = [[1,2],[1,4], [1, 6]]; fraction = []; for i in range(len(array)): fraction.append(Fraction(array[i][0], array[i][1])); for i in range(len(fraction)): print(fraction[i]); for i in range(len(fraction)-1): a = fraction[i]; b = fraction[i+1]; if (a > b): print('{0} > {1}'.format(a, b)); elif (a < b): print('{0} < {1}'.format(a, b)); else: print('{0} = {1}'.format(a, b)); return;</span>
小伟给出了解答:
<span style="font-size:18px;">function myDraw() { var config = new PlotConfiguration(); config.init(); config.setPreference(); //config.setSector(1,1,1,1); //config.axis2D(0, 0, 180); var r = 40; var row = 6, col = 13; var x = (600-r*(col-1))/2, y = (400-r*(row-1))/2; for (var i = 0; i < col; i++) { for (var j = 0; j < row; j++) { shape.strokeRect(x + i*r, y + j*r, r, r); } } var array = [1,1,1,2,1,3,1,4]; plot.save() .setFillStyle('red'); for (var i = 0; i < array.length / 2; i++) { shape.fillRect(x + array[2*i]*r, y + array[2*i+1]*r, r, r); } plot.restore(); x += 2*r; array = [1,1,2,1,3,1,4,1]; plot.save() .setFillStyle('blue'); for (var i = 0; i < array.length / 2; i++) { shape.fillRect(x + array[2*i]*r, y + array[2*i+1]*r, r, r); } plot.restore(); y += 2 * r; array = [1,1,1,2,2,1,2,2]; plot.save() .setFillStyle('pink'); for (var i = 0; i < array.length / 2; i++) { shape.fillRect(x + array[2*i]*r, y + array[2*i+1]*r, r, r); } plot.restore(); x += 3 * r; array = [1,2,2,1,2,2,2,3]; plot.save() .setFillStyle('orange'); for (var i = 0; i < array.length / 2; i++) { shape.fillRect(x + array[2*i]*r, y + array[2*i+1]*r, r, r); } plot.restore(); x += 2 * r; y -= 2 * r; array = [1,1,2,1,2,2,2,3]; plot.save() .setFillStyle('purple'); for (var i = 0; i < array.length / 2; i++) { shape.fillRect(x + array[2*i]*r, y + array[2*i+1]*r, r, r); } plot.restore(); x+= 3 * r; array = [1,1,1,2,2,2,2,3]; plot.save() .setFillStyle('cyan'); for (var i = 0; i < array.length / 2; i++) { shape.fillRect(x + array[2*i]*r, y + array[2*i+1]*r, r, r); } plot.restore(); }</span>
<span style="font-size:18px;"> for i in range(len(fraction)-1): a = fraction[i]; b = fraction[i+1]; print('{0}+{1}={2}'.format(a, b, a+b));</span>
<span style="font-size:18px;">1/2+1/2 = Fraction('1/2')+Fraction('1/2') = 1, 4/5-2/5 = Fraction('4/5')-Fraction('2/5') = 2/5, 1-7/9 = Fraction('1')-Fraction('7/9') = 2/9, 1/7+2/7 = Fraction('1/7')+Fraction('2/7') = 3/7, 1-1/2 = Fraction('1')-Fraction('1/2') = 1/2, 3/5+1/5 = Fraction('3/5')+Fraction('1/5') = 4/5, ### # @usage 分数相关的运算 # @author mw # @date 2016年01月15日 星期五 09:04:31 # @param # @return # ### def fractionCalc(): fin = open('input.txt'); fout= open('output.txt', 'a'); 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: lines = line.split(sep=' '); for i in range(len(lines)): if lines[i]=='': continue; expression = lines[i]; ops = []; length = len(expression); for j in range(length): if expression[j] == '+' or\ expression[j] == '-': ops.append(expression[j]); else: pass; operands = re.split(r'[+-]{1}', expression); operandString = []; length = len(operands); #数字如1/7要转化成Fraction('1/7')这种样式才可以算出正确结果,引号不可以少掉。 for j in range(length): operandString.append('Fraction(\''+operands[j]+'\')'); expression =''; length = len(operandString)-1; for j in range(length): expression+=operandString[j]+ops[j]; expression+=operandString[-1]; s = '{0} = {1} = {2}'.format(lines[i],expression, eval(expression)); print(s, end=', '); fout.write(s + ', '); print('\n'); fout.write('\n'); fout.close(); fin.close(); </span>
本节到此结束,欲知后事如何,请看下回分解。