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陈小旭
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值