1088 Rational Arithmetic(20 分)

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

参考:https://blog.csdn.net/tuzigg123/article/details/47156719

下列代码有问题,不知道为啥:

#include<iostream>
#include<string>
#include<sstream>
#include<math.h>
using namespace std;

int maxy(int a,int b){
	if(b==0) {		
		return abs(a);
	}		
	else 
		maxy(b,a%b);
}

string f(long long x,long long y){
	//cout<<"x="<<x<<"   y="<<y<<endl;
	if(x==0)
		return "0";
	else{
		long long yue=maxy(x,y);
		x/=yue;
		y/=yue;
		stringstream ss;//核心
		string s;
		int flag=0;
		if(x<0){
			flag=1;
			ss<<"(-";
			x=-x;
		}

		if(y==1){
			ss<<x;		
		}else{
			long long inti=x/y;			
			if(inti!=0){
				ss<<inti<<" ";
				//cout<<s<<endl;
			}
			x=x%y;	
			//cout<<"x="<<x<<endl;
			ss<<x<<"/"<<y;		
		}			
		//cout<<"s="<<s<<endl;
		if(flag==1){
			ss<<")";
		}
		getline(ss,s);
		return s;		
	}
}


int main() {
	//while (1) {	
		long long a[2],b[2];
		long long yue;
		for(int i=0;i<2;++i){
			scanf("%lld/%lld",&a[i],&b[i]);
			//cout<<a[i]<<"/"<<b[i]<<endl;
		}
		//+ - *
		long long sum1,sum2,dif1,dif2,cheng1,cheng2,quo1,quo2;
		sum1=a[0]*b[1]+a[1]*b[0];
		sum2=b[1]*b[0];		
		dif1=a[0]*b[1]-a[1]*b[0];
		dif2=b[1]*b[0];		
		cheng1=a[0]*a[1];
		cheng2=b[0]*b[1];		
		string s1,s2,s3;
		s1=f(a[0],b[0]);
		s2=f(a[1],b[1]);
		s3=f(sum1,sum2);
		cout<<s1<<" + "<<s2<<" = "<<s3<<endl;
		s3=f(dif1,dif2);
		cout<<s1<<" - "<<s2<<" = "<<s3<<endl;
		s3=f(cheng1,cheng2);
		cout<<s1<<" * "<<s2<<" = "<<s3<<endl;
		//"/"
		if(a[1]==0){
			cout<<s1<<" / "<<s2<<" = Inf"<<endl;
		}else{
			quo1=a[0]*b[1];
			quo2=b[0]*a[1];			
			if((quo1<0&&quo2<0)||(quo1>0&&quo2<0)){
				quo1=-quo1;
				quo2=-quo2;
			}

			s3=f(quo1,quo2);
			cout<<s1<<" / "<<s2<<" = "<<s3<<endl;
		}
	//}
		
}

猜你喜欢

转载自blog.csdn.net/qq_31647835/article/details/81913930
今日推荐