正剧开始:
星历2016年02月14日 13:47:04, 银河系厄尔斯星球中华帝国江南行省。
[工程师阿伟]正在和[机器小伟]一起研究分数的运算
其实对于分数的所有运算,小伟早就有了阿伟制作的神器,有了它,所有的有关分数的题目就是浮云。
<span style="font-size:18px;">### # @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: 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 = ''; #分割操作数 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]): 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; #表达式结果的运算打印 s = '{0} = {1} = {2}'.format(lines[i],expression, eval(expression)); 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.close(); fin.close();</span>
小伟只需要这样输入:
<span style="font-size:18px;">2/9+5/9 2/7+5/7 5/8+1/8 4+1/4 5/6-1/6 7/10-1/10 7/9-2/9 1-11/30</span>就可以得到以下的结果:
<span style="font-size:18px;">2/9+5/9 = Fraction('2')/Fraction('9')+Fraction('5')/Fraction('9') = 7/9, 2/7+5/7 = Fraction('2')/Fraction('7')+Fraction('5')/Fraction('7') = 1, 5/8+1/8 = Fraction('5')/Fraction('8')+Fraction('1')/Fraction('8') = 3/4, 4+1/4 = Fraction('4')+Fraction('1')/Fraction('4') = 17/4, 5/6-1/6 = Fraction('5')/Fraction('6')-Fraction('1')/Fraction('6') = 2/3, 7/10-1/10 = Fraction('7')/Fraction('10')-Fraction('1')/Fraction('10') = 3/5, 7/9-2/9 = Fraction('7')/Fraction('9')-Fraction('2')/Fraction('9') = 5/9, 1-11/30 = Fraction('1')-Fraction('11')/Fraction('30') = 19/30, </span>
<span style="font-size:18px;">4/15+1/15+7/15 = Fraction('4')/Fraction('15')+Fraction('1')/Fraction('15')+Fraction('7')/Fraction('15') = 4/5, </span>
<span style="font-size:18px;">>>> 1/4 ---> 0.25 1/4 1/4 ---> 0.5 1/4 1/2 1/4 ---> 1.0 1/4 3/4 3/4 1/4 ---> 2.0 1/4 1 3/2 1 1/4 ---> 4.0 1/4 5/4 5/2 5/2 5/4 1/4 ---> 8.0 def tmp(): n = 6; res = [[1]]; try: for i in range(1, n): a = []; for j in range(i+1): if (j == 0 or j == i): a.append(res[i-1][0]); else: a.append(res[i-1][j-1]+res[i-1][j]); res.append(a); except: print('error'); for i in range(len(res)): total = 0; for j in range(len(res[i])): total += res[i][j]; res[i][j] = str(Fraction(res[i][j], 4)); print('{0:^}'.format(res[i][j]), end=' '); print('--->', total/4,'\n'); return;</span>
<span style="font-size:18px;">5/6-1/2+1/3 7/8-5/12+1/6 1/2+1/4-1/6 9/4-1/4+1/5 5/6-(1/2+1/3) 7/8-(5/12+1/6) 1/2+(1/4-1/6) 9/4-(1/4+1/5) 2/5+1/3+3/5 1/4+1/3+1/4+2/3 9/7+1/8+3/8+5/7 5/6-1/2+1/3 = Fraction('5')/Fraction('6')-Fraction('1')/Fraction('2')+Fraction('1')/Fraction('3') = 2/3, 7/8-5/12+1/6 = Fraction('7')/Fraction('8')-Fraction('5')/Fraction('12')+Fraction('1')/Fraction('6') = 5/8, 1/2+1/4-1/6 = Fraction('1')/Fraction('2')+Fraction('1')/Fraction('4')-Fraction('1')/Fraction('6') = 7/12, 9/4-1/4+1/5 = Fraction('9')/Fraction('4')-Fraction('1')/Fraction('4')+Fraction('1')/Fraction('5') = 11/5, 5/6-(1/2+1/3) = Fraction('5')/Fraction('6')-(Fraction('1')/Fraction('2')+Fraction('1')/Fraction('3')) = 0, 7/8-(5/12+1/6) = Fraction('7')/Fraction('8')-(Fraction('5')/Fraction('12')+Fraction('1')/Fraction('6')) = 7/24, 1/2+(1/4-1/6) = Fraction('1')/Fraction('2')+(Fraction('1')/Fraction('4')-Fraction('1')/Fraction('6')) = 7/12, 9/4-(1/4+1/5) = Fraction('9')/Fraction('4')-(Fraction('1')/Fraction('4')+Fraction('1')/Fraction('5')) = 9/5, 2/5+1/3+3/5 = Fraction('2')/Fraction('5')+Fraction('1')/Fraction('3')+Fraction('3')/Fraction('5') = 4/3, 1/4+1/3+1/4+2/3 = Fraction('1')/Fraction('4')+Fraction('1')/Fraction('3')+Fraction('1')/Fraction('4')+Fraction('2')/Fraction('3') = 3/2, 9/7+1/8+3/8+5/7 = Fraction('9')/Fraction('7')+Fraction('1')/Fraction('8')+Fraction('3')/Fraction('8')+Fraction('5')/Fraction('7') = 5/2, </span>
本节到此结束,欲知后事如何,请看下回分解。