1088. Rational Arithmetic (20)

題目鏈接:http://www.patest.cn/contests/pat-a-practise/1088
題目:

For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient.

Input Specification:

Each input file contains one test case, which gives in one line the two rational numbers in the format "a1/b1 a2/b2". The numerators and the denominators are all in the range of long int. If there is a negative sign, it must appear only in front of the numerator. The denominators are guaranteed to be non-zero numbers.

Output Specification:

For each test case, print in 4 lines the sum, difference, product and quotient of the two rational numbers, respectively. The format of each line is "number1 operator number2 = result". Notice that all the rational numbers must be in their simplest form "k a/b", where k is the integer part, and a/b is the simplest fraction part. If the number is negative, it must be included in a pair of parentheses. If the denominator in the division is zero, output "Inf" as the result. It is guaranteed that all the output integers are in the range of long int.

Sample Input 1:
2/3 -4/2
Sample Output 1:
2/3 + (-2) = (-1 1/3)
2/3 - (-2) = 2 2/3
2/3 * (-2) = (-1 1/3)
2/3 / (-2) = (-1/3)
Sample Input 2:
5/3 0/6
Sample Output 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

分析:
一個帶分數和假分數結構體,包含print成員函數,加減乘數就用各自的分母進行公式運算,特別要注意分子或分母爲0的情況。
把運算過程放到構造函數中,可以很方便得簡化。
注意是long long而不是Int

AC代碼:
#include<cstdio>
#include<iostream>
using namespace std;
long long gcd(long long x, long long y){//計算最大公約數
 if (x < y)swap(x, y);
 if (x % y == 0)return y;
 else return gcd(x - y, y);
}
struct Number{//帶分數形式
 long long flag;//符號
 long long zhengshu;//整數部分
 long long fenmu;
 long long fenzi;
 Number() :flag(0), zhengshu(0), fenmu(1), fenzi(0){}
 void print(){//負責打印的成員函數
  if (fenmu == 0){//如果分母爲0
   cout << "Inf";
   return;
  }
  if (zhengshu == 0 && fenzi == 0){//按照分子和整數是否爲0分爲4種情況
   cout << "0";
   return;
  }
  if (flag == -1)cout << "(-";
  if (zhengshu == 0 && fenzi != 0){
   cout << fenzi << "/" << fenmu;
  }
  else if (zhengshu != 0 && fenzi == 0){
   cout << zhengshu;
  }
  else if (zhengshu != 0 && fenzi != 0){
   cout << zhengshu << " " << fenzi << "/" << fenmu;
  }
  if (flag == -1)cout << ")";
 }
};
struct number{//假分數形式
 long long fenzi;
 long long fenmu;
 number() :fenzi(0), fenmu(0){}
 number(long long x, long long y){//假分數的構造函數
  if (y < 0){ x = -x; y = -y; }//當y爲0時,則把符號轉移到放在分子x處
  if (x == 0 || y == 0){ //如果其中一個爲0,則不能進行gcd計算(因爲其中有x%y)所以直接返回
   fenzi = x;
   fenmu = y;
   return;
  }
  long long tmp = gcd(abs(x), abs(y));//約去最大公約數
  x = x / tmp;
  y = y / tmp;
  fenzi = x;
  fenmu = y;
 }
};
Number* num2Num(number& num){//假分數到帶分數的轉換
 Number *ret = new Number();
 if (num.fenzi < 0){//假分數分子的符號變成帶分數中的符號部分
  num.fenzi = -num.fenzi;
  ret->flag = -1;
 }
 if (num.fenzi  == 0|| num.fenmu == 0){//如果其中一個爲0,則直接賦值返回
  ret->fenzi = num.fenzi;
  ret->fenmu = num.fenmu;
  ret->zhengshu = 0;
  return ret;
 }
 if (num.fenzi % num.fenmu == 0){//計算整數部分
  long long result = num.fenzi / num.fenmu;
  ret->zhengshu = result;
  ret->fenmu = 1;
  ret->fenzi = 0;
 }
 else{//計算整數和餘下分子部分
  ret->zhengshu = num.fenzi / num.fenmu;
  ret->fenmu = num.fenmu;
  ret->fenzi = num.fenzi - ret->fenmu * ret->zhengshu;
 }
 return ret;
}
int main(){
 freopen("F://Temp/input.txt", "r", stdin);
 long long zi1, zi2, mu1, mu2;
 scanf("%lld\/%lld %lld\/%lld", &zi1, &mu1, &zi2, &mu2);//格式化輸入,比string轉化來得方便多了
 number* num1 = new number(zi1, mu1);//都大多用到了假分數的構造函數部分,很方便
 number* num2 = new number(zi2, mu2);
 Number* Num1 = new Number();
 Number* Num2 = new Number();
 Num1 = num2Num(*num1);
 Num2 = num2Num(*num2);
 number* num_jia = new number((zi1*mu2 + mu1*zi2), mu1*mu2);//按照公式計算
 number* num_jian = new number((zi1*mu2 - mu1*zi2), mu1*mu2);
 number* num_cheng = new number(zi1*zi2, mu1*mu2);
 number* num_chu = new number(zi1*mu2, zi2*mu1);
 Number* Num_jia = new Number();
 Number* Num_jian = new Number();
 Number* Num_cheng = new Number();
 Number* Num_chu = new Number();
 Num_jia = num2Num(*num_jia);
 Num_jian = num2Num(*num_jian);
 Num_cheng = num2Num(*num_cheng);
 Num_chu = num2Num(*num_chu);
 Num1->print(); cout << " + "; Num2->print(); cout << " = "; Num_jia->print(); cout << endl;
 Num1->print(); cout << " - "; Num2->print(); cout << " = "; Num_jian->print(); cout << endl;
 Num1->print(); cout << " * "; Num2->print(); cout << " = "; Num_cheng->print(); cout << endl;
 Num1->print(); cout << " / "; Num2->print(); cout << " = "; Num_chu->print(); cout << endl;
 return 0;
}


截圖:

——Apie陳小旭
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章