1034. 有理数四则运算(20)
本题要求编写程序,计算2个有理数的和、差、积、商。
输入格式:
输入在一行中按照“a1/b1 a2/b2”的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整数,负号只可能出现在分子前,分母不为0。
输出格式:
分别在4行中按照“有理数1 运算符 有理数2 = 结果”的格式顺序输出2个有理数的和、差、积、商。注意输出的每个有理数必须是该有理数的最简形式“k a/b”,其中k是整数部分,a/b是最简分数部分;若为负数,则须加括号;若除法分母为0,则输出“Inf”。题目保证正确的输出中没有超过整型范围的整数。
输入样例1:2/3 -4/2输出样例1:
2/3 + (-2) = (-1 1/3) 2/3 - (-2) = 2 2/3 2/3 * (-2) = (-1 1/3) 2/3 / (-2) = (-1/3)输入样例2:
5/3 0/6输出样例2:
1 2/3 + 0 = 1 2/3 1 2/3 - 0 = 1 2/3 1 2/3 * 0 = 0 1 2/3 / 0 = Inf
分析:
这道题真是恶心···第一它很麻烦,第二,在确定正负符号的时候,总是出现幺蛾子!不知道为什么。没办法,就只有麻烦得重写了~
以为自己写得很烂,结果网上一搜,发现自己还算写得整齐的···不错不错~~
using System; namespace PAT { class Program { static void Main() { string[] infos = Console.ReadLine().Split(' '); RelNumber num1 = new RelNumber(); num1.fenzi = long.Parse(infos[0].Split('/')[0]); num1.fenmu = long.Parse(infos[0].Split('/')[1]); RelNumber num2 = new RelNumber(); num2.fenzi = long.Parse(infos[1].Split('/')[0]); num2.fenmu = long.Parse(infos[1].Split('/')[1]); //加法式子 PrintRelNumber(num1); Console.Write(" + "); PrintRelNumber(num2); Console.Write(" = "); AddRelNumber(num1, num2, 1); Console.WriteLine(); //减法 PrintRelNumber(num1); Console.Write(" - "); PrintRelNumber(num2); Console.Write(" = "); AddRelNumber(num1, num2, 0); Console.WriteLine(); //乘法 PrintRelNumber(num1); Console.Write(" * "); PrintRelNumber(num2); Console.Write(" = "); MultiRelNumber(num1, num2); Console.WriteLine(); //除法 PrintRelNumber(num1); Console.Write(" / "); PrintRelNumber(num2); Console.Write(" = "); DivideRelNumber(num1, num2); } //找到两个数字的最大公约数 static long FindMaxDivisor(long num1, long num2) { long large = num1 > num2 ? num1 : num2; long small = num1 < num2 ? num1 : num2; long temp; while(small != 0) { temp = small; small = large % small; large = temp; } return large; } //输出有理数 static void PrintRelNumber(RelNumber number) { if (number.fenzi == 0) Console.Write("0"); else { int flag1 = -1; if (number.fenmu > 0) flag1 = 1; int flag2 = -1; if (number.fenzi > 0) flag2 = 1; bool symbol = flag1 * flag2 > 0; //表示符号,true代表整数,false代表负数 long divisor = FindMaxDivisor(Math.Abs(number.fenzi), Math.Abs(number.fenmu)); number.fenzi = Math.Abs(number.fenzi) / divisor; number.fenmu = Math.Abs(number.fenmu) / divisor; string msg = ""; if (number.fenmu <= number.fenzi) //假分数 { msg += number.fenzi / number.fenmu; number.fenzi = number.fenzi % number.fenmu; if (number.fenzi > 0) msg += " "; } if (number.fenzi > 0) msg += string.Format("{0}/{1}", number.fenzi, number.fenmu); if (!symbol) msg = "(-" + msg + ")"; Console.Write(msg); } } //计算有理数 //加减法 static void AddRelNumber(RelNumber num1, RelNumber num2, int operateType) { RelNumber result = new RelNumber(); if(operateType == 1) result.fenzi = num1.fenzi * num2.fenmu + num2.fenzi * num1.fenmu; else result.fenzi = num1.fenzi * num2.fenmu - num2.fenzi * num1.fenmu; result.fenmu = num1.fenmu * num2.fenmu; PrintRelNumber(result); } //乘法 static void MultiRelNumber(RelNumber num1, RelNumber num2) { RelNumber result = new RelNumber(); result.fenzi = num1.fenzi * num2.fenzi; result.fenmu = num1.fenmu * num2.fenmu; PrintRelNumber(result); } //除法 static void DivideRelNumber(RelNumber num1, RelNumber num2) { RelNumber result = new RelNumber(); result.fenzi = num1.fenzi * num2.fenmu; result.fenmu = num1.fenmu * num2.fenzi; if (result.fenmu == 0) Console.Write("Inf"); else { PrintRelNumber(result); } } struct RelNumber { public long fenzi; public long fenmu; } } }