1088 Rational Arithmetic

1088 Rational Arithmetic (20 分)

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
#include <iostream>
#include <cmath>
using namespace std;
long long gcd(long long a,long long b){
    return b==0 ? a:gcd(b,a%b) ;
}
void func(long long m,long long n){
    int flag1=0,flag2=0,flag=0;
    if(n==0){
        printf("Inf");
        return ;
    }
    if(m==0) {
        printf("0");
        return ;
    }
    if(m<0) flag1=1;
    if(n<0) flag2=1;
    m=abs(m),n=abs(n);
    if(flag1==1 && flag2==1) flag=0;
    else if(flag1==1 || flag2==1) flag=1;
    if(m==n){
        if(flag==1) printf("(-1)");
        else printf("1");
        return ;
    }
    long long x=m%n,y=m/n;
    if(x==0){
        if(flag==0) printf("%d",y);
        else printf("(-%d)",y);
        return ;
    }else{
        long long t1=m-y*n,t2=n,t=gcd(t1,t2);
        t1/=t,t2/=t;
        if(flag==1){
            printf("(-");
            if(y==0){
                printf("%lld/%lld",t1,t2);
            }else{
                printf("%lld %lld/%lld",y,t1,t2);
            }
            printf(")");
        }else{
            if(y==0){
                printf("%lld/%lld",t1,t2);
            }else{
                printf("%lld %lld/%lld",y,t1,t2);
            }
        }
    }
}
int main(){
    long long int a,b,c,d;
    scanf("%lld/%lld %lld/%lld",&a,&b,&c,&d);
//  printf("%lld %lld %lld %lld",a,b,c,d);
    func(a,b);printf(" + ");func(c,d);printf(" = ");func(a*d+c*b,b*d);printf("\n");
    func(a,b);printf(" - ");func(c,d);printf(" = ");func(a*d-c*b,b*d);printf("\n");
    func(a,b);printf(" * ");func(c,d);printf(" = ");func(a*c,b*d);printf("\n");
    func(a,b);printf(" / ");func(c,d);printf(" = ");func(a*d,b*c);printf("\n");
    return 0;
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市沛鸵,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌涌乳,老刑警劉巖悉默,帶你破解...
    沈念sama閱讀 222,252評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件曲梗,死亡現(xiàn)場離奇詭異哮缺,居然都是意外死亡弄跌,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,886評論 3 399
  • 文/潘曉璐 我一進店門尝苇,熙熙樓的掌柜王于貴愁眉苦臉地迎上來铛只,“玉大人,你說我怎么就攤上這事糠溜〈就妫” “怎么了?”我有些...
    開封第一講書人閱讀 168,814評論 0 361
  • 文/不壞的土叔 我叫張陵非竿,是天一觀的道長蜕着。 經(jīng)常有香客問我,道長红柱,這世上最難降的妖魔是什么承匣? 我笑而不...
    開封第一講書人閱讀 59,869評論 1 299
  • 正文 為了忘掉前任蓖乘,我火速辦了婚禮,結(jié)果婚禮上悄雅,老公的妹妹穿的比我還像新娘。我一直安慰自己铁蹈,他們只是感情好宽闲,可當我...
    茶點故事閱讀 68,888評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著握牧,像睡著了一般容诬。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上沿腰,一...
    開封第一講書人閱讀 52,475評論 1 312
  • 那天览徒,我揣著相機與錄音,去河邊找鬼颂龙。 笑死习蓬,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的措嵌。 我是一名探鬼主播躲叼,決...
    沈念sama閱讀 41,010評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼企巢!你這毒婦竟也來了枫慷?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,924評論 0 277
  • 序言:老撾萬榮一對情侶失蹤浪规,失蹤者是張志新(化名)和其女友劉穎或听,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體笋婿,經(jīng)...
    沈念sama閱讀 46,469評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡誉裆,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,552評論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了缸濒。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片找御。...
    茶點故事閱讀 40,680評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖绍填,靈堂內(nèi)的尸體忽然破棺而出霎桅,到底是詐尸還是另有隱情,我是刑警寧澤讨永,帶...
    沈念sama閱讀 36,362評論 5 351
  • 正文 年R本政府宣布滔驶,位于F島的核電站,受9級特大地震影響卿闹,放射性物質(zhì)發(fā)生泄漏揭糕。R本人自食惡果不足惜萝快,卻給世界環(huán)境...
    茶點故事閱讀 42,037評論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望著角。 院中可真熱鬧揪漩,春花似錦、人聲如沸吏口。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,519評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽产徊。三九已至昂勒,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間舟铜,已是汗流浹背戈盈。 一陣腳步聲響...
    開封第一講書人閱讀 33,621評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留谆刨,地道東北人塘娶。 一個月前我還...
    沈念sama閱讀 49,099評論 3 378
  • 正文 我出身青樓,卻偏偏與公主長得像痊夭,于是被迫代替她去往敵國和親血柳。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,691評論 2 361