這裏有沙克拉的觀念的變化,允許加法和乘法......
Format[setDenominator[x_, d_]] := [email protected][x*d, d]
setDenominator /: Plus[left___, setDenominator[x1_, d1_], right___] := left + x1 + right;
setDenominator /: Times[left___, setDenominator[x1_, d1_], right___] :=left*x1*right;
想出來:
a = setDenominator[3/5, 10];
Print[a, " + ", 2/3, " = " , a + 2/3]
Print[a, " + ", 2/3, " = " , setDenominator[a + 2/3, 30]]
Print[a, " × ", 2/3, " = " , a * 2/3]
Print[a, " × ", 2/3, " = " , setDenominator[a * 2/3, 30]]
Print[a, " ÷ ", 2/3, " = " , a /(2/3)]
+1你的建議很好地工作於分數,即使分母沒有被很好地選擇:例如'setDominominator [1/3,5]'。 – DavidC 2011-04-08 21:18:02
我對您的解決方案做了一個變體,允許添加和相乘分數。 – DavidC 2011-04-08 22:05:27