PAT Rational Arithmetic (20),rationalarithmetic

來源:互聯網
上載者:User

PAT Rational Arithmetic (20),rationalarithmetic
題目描述

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

輸入描述:
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.


輸出描述:
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.

輸入例子:
5/3 0/6

輸出例子:
1 2/3 + 0 = 1 2/31 2/3 - 0 = 1 2/31 2/3 * 0 = 0

1 2/3 / 0 = Inf

#include <iostream>#include <cmath>using namespace std;//分數的結構體typedef struct fraction{int isNegative;//加號或減號long int i,numerator,denominator;//整數部分、分子、分母//初始化分數fraction():isNegative(1),i(0),numerator(0),denominator(1){};}fraction;//重載 == 操作符bool operator==(fraction& f, long int x){return f.numerator==0 && f.i*f.isNegative==x;}//myabs_求絕對值long int myabs(long int x){if(x<0)return -x;return x;}//將分數轉換為要求的形式fraction simply(long int lhs,long  int rhs){fraction temp;if(0==lhs*rhs)return temp;if(lhs*rhs<0){lhs=myabs(lhs);rhs=myabs(rhs);temp.isNegative=-1;}//化簡為整數+分數形式temp.i=lhs/rhs;lhs%=rhs;//如果化簡後,分子為0,如12/3化簡後為4,則返回if(0==lhs)return temp;temp.numerator=lhs;temp.denominator=rhs;//找出最大公約數long int t;while(rhs%lhs){t=rhs;rhs=lhs;lhs=t%lhs;}//化為最簡形式temp.numerator/=lhs;temp.denominator/=lhs;return temp;}void print(fraction& f){if(f==0){cout<<"0";return;}if(f.isNegative==-1)cout<<"(-";if(f.i){cout<<f.i;if(f.numerator)cout<<" "<<f.numerator<<"/"<<f.denominator;}else if(f.numerator){cout<<f.numerator<<"/"<<f.denominator;}if(f.isNegative==-1)cout<<")";}void printop(fraction& f1, fraction& f2, fraction& f3,char o){print(f1);cout<<" "<<o<<" ";print(f2);cout<<" = ";print(f3);cout<<endl;}fraction op(long int n1, long int n2,long int n3,long int n4,char o){fraction temp;long int numerator, denominator;denominator=n2*n4;switch(o){case '+':numerator=n1*n4+n3*n2;break;case '-':numerator=n1*n4-n3*n2;break;case '*':numerator=n1*n3;break;case '/':denominator=n2*n3;numerator=n1*n4;break;}temp=simply(numerator,denominator);return temp;}int main(){long int a1,a2,a3,a4;fraction f1,f2,f3;char c1,c2;cin>>a1>>c1>>a2>>a3>>c2>>a4;f1=simply(a1,a2);f2=simply(a3,a4);a1=f1.isNegative*(f1.i*f1.denominator+f1.numerator);a2=f1.denominator;a3=f2.isNegative*(f2.i*f2.denominator+f2.numerator);a4=f2.denominator;//進行+、-、*、/、運算f3=op(a1,a2,a3,a4,'+');printop(f1,f2,f3,'+');f3=op(a1,a2,a3,a4,'-');printop(f1,f2,f3,'-');f3=op(a1,a2,a3,a4,'*');printop(f1,f2,f3,'*');f3=op(a1,a2,a3,a4,'/');if(f2==0){print(f1);cout<<" / ";print(f2);cout<<" = Inf"<<endl;}elseprintop(f1,f2,f3,'/');return 0;}


貼個圖

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.