牛客网_PAT乙级_10234有理数四则运算(20)【通过5/7:格式错误】
題目描述
本題要求編寫程序,計(jì)算2個(gè)有理數(shù)的和、差、積、商。
輸入描述:
輸入在一行中按照“a1/b1 a2/b2”的格式給出兩個(gè)分?jǐn)?shù)形式的有理數(shù),其中分子和分母全是整型范圍內(nèi)的整數(shù),負(fù)號只可能出現(xiàn)在分子前,分母不為0。
輸出描述:
分別在4行中按照“有理數(shù)1 運(yùn)算符 有理數(shù)2 = 結(jié)果”的格式順序輸出2個(gè)有理數(shù)的和、差、積、商。注意輸出的每個(gè)有理數(shù)必須是該有理數(shù)的最簡形式“k a/b”,其中k是整數(shù)部分,a/b是最簡分?jǐn)?shù)部分;若為負(fù)數(shù),則須加括號;若除法分母為0,則輸出“Inf”。題目保證正確的輸出中沒有超過整型范圍的整數(shù)。
輸入例子:
5/3 0/6
輸出例子:
1 2/3 + 0 = 1 2/3
1 2/3 - 0 = 1 2/3
1 2/3 * 0 = 0
1 2/3 / 0 = Inf
我編寫的本地測試用例
2/3 -4/2
答案:
2/3 + (-2) = (-1 1/3)
2/3 - (-2) = 2 2/3
2/3 * (-2) = (-1 1/3)
2/3 / (-2) = (-1/3)
5/3 0/6
答案:
1 2/3 + 0 = 1 2/3
1 2/3 - 0 = 1 2/3
1 2/3 * 0 = 0
1 2/3 / 0 = Inf
0/3 0/6
我的結(jié)果:
0 + 0 = 0
0 - 0 = 0
0 * 0 = 0
0 / 0 = Inf
-0/3 -0/6
我的結(jié)果:
0 + 0 = 0
0 - 0 = 0
0 * 0 = 0
0 / 0 = Inf
5/3 -4/2
我的結(jié)果:
1 2/3 + (-2) = (-1/3)
1 2/3 - (-2) = 3 2/3
1 2/3 * (-2) = (-3 1/3)
1 2/3 / (-2) = (-5/6)
-65535/65534 -1/2
我的結(jié)果:
(-1 1/65534) + (-1/2) = (-1 16384/32767)
(-1 1/65534) - (-1/2) = (-16384/32767)
(-1 1/65534) * (-1/2) = 65535/131068
(-1 1/65534) / (-1/2) = 2 1/32767
代碼
#define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; class Rational { public:int up;int down;void input(int up, int down); }; //構(gòu)造器 void Rational::input(int up, int down) {this->up = up;this->down = down; }//通分 void reduction(Rational* r1, Rational* r2) {int fenMu = r1->down*r2->down;r1->up *= r2->down;r2->up *= r1->down;r1->down = fenMu;r2->down = fenMu;} //格式化輸出 void SimplifyOutput(Rational num) {int up = num.up;//分母int down = num.down;//分子if (up == 0)//如果分母為0則直接輸出{cout << '0';return;}//找最大公因數(shù)、約分int factor;//臨時(shí)公因數(shù)int maxFactor = 1;//最大公因數(shù)int min = up > down ? up : down;for (factor = 1; factor <= min; factor++){if (up%factor == 0 && down%factor == 0)//找到公因數(shù){maxFactor = factor;}}up /= maxFactor;down /= maxFactor;//打印if (num.up < 0){cout << "(";//先打印符號if (num.up < 0){up *= (-1);cout << "-";}int integer = up / down;if (integer != 0 && up%down == 0){cout << integer;}else if (integer != 0 && up%down != 0){cout << integer << " ";}if (up%down == 0 && integer != 0)//如果分母為0則不輸出{cout << ")";return;}cout << up % down << "/" << down;cout << ")";}else{int integer = up / down;if (integer != 0){cout << integer << " ";}if (up%down == 0 && integer != 0)//如果分母為0則不輸出{return;}cout << up % down << "/" << down;} } //加法 void add(Rational left, Rational right) {reduction(&left, &right);//通分//計(jì)算Rational result;result.up = left.up + right.up;result.down = left.down;//打印SimplifyOutput(left);cout << " + ";SimplifyOutput(right);cout << " = ";SimplifyOutput(result); } //減法 void sub(Rational left, Rational right) {reduction(&left, &right);//通分//計(jì)算Rational result;result.up = left.up - right.up;result.down = left.down;//打印SimplifyOutput(left);cout << " - ";SimplifyOutput(right);cout << " = ";SimplifyOutput(result); } //乘法 void multiply(Rational left, Rational right) {//計(jì)算Rational result;result.up = left.up*right.up;result.down = left.down*right.down;//打印SimplifyOutput(left);cout << " * ";SimplifyOutput(right);cout << " = ";SimplifyOutput(result); } //除法 void division(Rational left, Rational right) {//計(jì)算Rational result;result.up = left.up*right.down;result.down = left.down*right.up;//讓負(fù)號始終在分母上if (result.down < 0){result.down *= (-1);result.up *= (-1);}//打印SimplifyOutput(left);cout << " / ";SimplifyOutput(right);cout << " = ";if (right.up == 0)//如果除數(shù)是0{cout << "Inf";return;}SimplifyOutput(result); }int main() {Rational left, right;//輸入int a1, a2, b1, b2;scanf("%d/%d %d/%d", &a1, &a2, &b1, &b2);left.up = a1;left.down = a2;right.up = b1;right.down = b2;//加法add(left, right);cout << endl;//減法sub(left, right);cout << endl;//乘法multiply(left, right);cout << endl;//除法division(left, right); }總結(jié)
以上是生活随笔為你收集整理的牛客网_PAT乙级_10234有理数四则运算(20)【通过5/7:格式错误】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 牛客网_PAT乙级_1023旧键盘打字(
- 下一篇: 牛客网_PAT乙级_1026跟奥巴马一起