本题要求编写程序,计算2个有理数的和、差、积、商。
输入格式:
# include<iostream> # include<algorithm> # include<stdio.h> # include<string> using namespace std; bool ISint(int a,int b) { double c = a*1.0 / b; if(c - (int)c == 0) { return true; } else { return false; } } int gcd(int a,int b)//a da { int t; if(a<b) { t=a;a=b;b=t; } if(b==0) { return 0; } else { while(b!=0) { t = a % b; a = b; b = t; } return a; } } int gbs(int x,int y) { return (x*y)/gcd(x,y); } void printhe(int he,int gb,int a1,int b1,int a2,int b2) { if( ISint(a1,b1) && a1>=0) { cout<<a1/b1; } else if(ISint(a1,b1) && a1<0 ) { cout<<"("<<a1/b1<<")"; } else if(!ISint(a1,b1) && a1>=0) { int t1 = gcd(a1,b1); a1 = a1 /t1; b1 = b1 /t1; if(a1>b1) { int k1=0; while(a1>b1) { a1 = a1 - b1; k1++; } cout<<k1<<" "<<a1<<"/"<<b1; } else { cout<<a1<<"/"<<b1; } } else { a1 = -a1; int t1 = gcd(a1,b1); a1 = a1 /t1; b1 = b1 /t1; if(a1>b1) { int k1=0; while(a1>b1) { a1 = a1 - b1; k1++; } cout<<"(-"<<k1<<" "<<a1<<"/"<<b1<<")"; } else { cout<<"(-"<<a1<<"/"<<b1<<")"; } } cout<<" + "; if( ISint(a2,b2) && a2>=0) { cout<<a2/b2; } else if(ISint(a2,b2) && a2<0 ) { cout<<"("<<a2/b2<<")"; } else if(!ISint(a2,b2) && a2>=0) { int t2 = gcd(a2,b2); a2 = a2 /t2; b2 = b2 /t2; if(a2>b2) { int k2=0; while(a2>b2) { a2 = a2 - b2; k2++; } cout<<k2<<" "<<a2<<"/"<<b2; } else { cout<<a2<<"/"<<b2; } } else { a2 = -a2; int t2 = gcd(a2,b2); a2 = a2 /t2; b2 = b2 /t2; if(a2>b2) { int k2=0; while(a2>b2) { a2 = a2 - b2; k2++; } cout<<"("<<-k2<<" "<<a2<<"/"<<b2<<")"; } else { cout<<"(-"<<a2<<"/"<<b2<<")"; } } cout<<" = "; if(he>=0) { if(ISint(he,gb)) { cout<<he; } else { int t = gcd(he,gb); he = he /t; gb = gb /t; if(he>gb) { int k=0; while(he>gb) { he = he - gb; k++; } cout<<k<<" "<<he<<"/"<<gb; } else { cout<<he<<"/"<<gb; } } } else { he = - he; if(ISint(he,gb)) { cout<<"(-"<<he/gb<<")"; } else { int t = gcd(he,gb); he = he /t; gb = gb /t; if(he>gb) { int k=0; while(he>gb) { he = he - gb; k++; } cout<<"("<<-k<<" "<<he<<"/"<<gb<<")"; } else { cout<<"("<<-he<<"/"<<gb<<")"; } } } cout<<endl; } void printcha(int cha,int gb,int a1,int b1,int a2,int b2) { if( ISint(a1,b1) && a1>=0) { cout<<a1/b1; } else if(ISint(a1,b1) && a1<0 ) { cout<<"("<<a1/b1<<")"; } else if(!ISint(a1,b1) && a1>=0) { int t1 = gcd(a1,b1); a1 = a1 /t1; b1 = b1 /t1; if(a1>b1) { int k1=0; while(a1>b1) { a1 = a1 - b1; k1++; } cout<<k1<<" "<<a1<<"/"<<b1; } else { cout<<a1<<"/"<<b1; } } else { a1 = -a1; int t1 = gcd(a1,b1); a1 = a1 /t1; b1 = b1 /t1; if(a1>b1) { int k1=0; while(a1>b1) { a1 = a1 - b1; k1++; } cout<<"("<<-k1<<" "<<a1<<"/"<<b1<<")"; } else { cout<<"(-"<<a1<<"/"<<b1<<")"; } } cout<<" - "; if( ISint(a2,b2) && a2>=0) { cout<<a2/b2; } else if(ISint(a2,b2) && a2<0 ) { cout<<"("<<a2/b2<<")"; } else if(!ISint(a2,b2) && a2>=0) { int t2 = gcd(a2,b2); a2 = a2 /t2; b2 = b2 /t2; if(a2>b2) { int k2=0; while(a2>b2) { a2 = a2 - b2; k2++; } cout<<k2<<" "<<a2<<"/"<<b2; } else { cout<<a2<<"/"<<b2; } } else { a2 = -a2; int t2 = gcd(a2,b2); a2 = a2 /t2; b2 = b2 /t2; if(a2>b2) { int k2=0; while(a2>b2) { a2 = a2 - b2; k2++; } cout<<"("<<-k2<<" "<<a2<<"/"<<b2<<")"; } else { cout<<"(-"<<a2<<"/"<<b2<<")"; } } cout<<" = "; if(cha>=0) { if(ISint(cha,gb)) { cout<<cha; } else { int t = gcd(cha,gb); cha = cha /t; gb = gb /t; if(cha>gb) { int k=0; while(cha>gb) { cha = cha - gb; k++; } cout<<k<<" "<<cha<<"/"<<gb; } else { cout<<cha<<"/"<<gb; } } } else { cha = - cha; if(ISint(cha,gb)) { cout<<"(-"<<cha/gb<<")"; } else { int t = gcd(cha,gb); cha = cha /t; gb = gb /t; if(cha>gb) { int k=0; while(cha>gb) { cha = cha - gb; k++; } cout<<"("<<-k<<" "<<cha<<"/"<<gb<<")"; } else { cout<<"("<<-cha<<"/"<<gb<<")"; } } } cout<<endl; } int main() { int a1,a2,b1,b2; scanf("%d/%d %d/%d",&a1,&b1,&a2,&b2); int gb,gy,he,cha; gb = gbs(b1,b2); he = a1 * gb / b1 + a2 * gb / b2; cha = a1 * gb / b1 - a2 * gb / b2; printhe(he,gb,a1,b1,a2,b2); printcha(cha,gb,a1,b1,a2,b2); return 0; }
输入在一行中按照“a1/b1 a2/b2”的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整数,负号只可能出现在分子前,分母不为0。
输出格式:
分别在4行中按照“有理数1 运算符 有理数2 = 结果”的格式顺序输出2个有理数的和、差、积、商。注意输出的每个有理数必须是该有理数的最简形式“k a/b”,其中k是整数部分,a/b是最简分数部分;若为负数,则须加括号;若除法分母为0,则输出“Inf”。题目保证正确的输出中没有超过整型范围的整数。
输入样例1:
2/3 -4/2
输出样例1:
2/3 + (-2) = (-1 1/3) 2/3 - (-2) = 2 2/3 2/3 * (-2) = (-1 1/3) 2/3 / (-2) = (-1/3)
输入样例2:
5/3 0/6
输出样例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
原文:http://www.cnblogs.com/wshyj/p/6295079.html