zoukankan      html  css  js  c++  java
  • PAT-A 1088. Rational Arithmetic

    1088. Rational Arithmetic

    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<stdio.h>
    long long divisor(long long a,long long b);
    void print(long long a,long long b);
    void add(long long a,long long b,long long c,long long d);
    void minus(long long a,long long b,long long c,long long  d);
    void division(long long a,long long b,long long c,long long d);
    void multiply(long long a,long long b,long long  c,long long d);
    int main()
    {
    	long long a,b,c,d;
    	scanf("%lld/%lld %lld/%lld",&a,&b,&c,&d);
    	add(a,b,c,d);
    	minus(a,b,c,d);
    	multiply(a,b,c,d);
    	division(a,b,c,d);
    	return 0;
    }
    void add(long long a,long long  b,long long c,long long d)
    {
    	print(a,b);
    	printf(" + ");
    	print(c,d);
    	printf(" = ");
    	print(a*d+b*c,b*d);
    	printf("
    ");	
    }
    void minus(long long a,long long b,long long c,long long  d)
    {
            print(a,b);
            printf(" - ");
            print(c,d);
            printf(" = ");
            print(a*d-b*c,b*d);
            printf("
    ");
    	
    }
    void multiply(long long a,long long b,long long  c,long long d)
    {
            print(a,b);
            printf(" * ");
            print(c,d);
            printf(" = ");
            print(a*c,b*d);
            printf("
    ");
    }
    void division(long long a,long long b,long long c,long long d)
    {
    	long long m= a*d;
    	long long n= b*c;
    	print(a,b);
            printf(" / ");
            print(c,d);
            printf(" = ");
            if(n==0)
    		printf("Inf
    ");
    	else 
    	{
    		if(n<0)
    		{
    			m=m*-1;		//把负号调整到分子上
    			n=n*-1;
    		}
    		print(m,n);
    		printf("
    ");    
    	}
    }
    void print(long long a,long long b)//打印函数
    {
    	long long tmp;
    	int flag =0;
    	if(a==0)
    	{
    		printf("0");
    		return ;
    	}
    	else if(a<0)//不管分子为正还是为负,统统按照正整数处理
    	{
    		a= -a;
    		flag = 1;//标记为负数
    	}
    	tmp = divisor(a,b);//求出最大公约数
    	a=a/tmp;//化为最简形式
    	b=b/tmp;
    	long c=a/b;//整数部分
    	if(flag)
    	{
    		if(b==1)	
    			printf("(-%lld)",a);
    		else if(c>0)	
    			printf("(-%lld %lld/%lld)",c,(a%b),b);
    		else
    			printf("(-%lld/%lld)",a%b,b);
    	}
    	else
    	{
    		if(b==1)
    			printf("%lld",a);			
                    else if(c>0)
                            printf("%lld %lld/%lld",c,a%b,b);
                    else
                            printf("%lld/%lld",a%b,b);
    	}
    }
    long long  divisor(long long a,long long b)//辗转相除法求最大公约数
    {
    	long long max = a>b?a:b;
    	long long min =a<b?a:b;
    	long long  r=max%min;
    	while(r)
    	{
    		max=min;
    		min = r;
    		r = max%min;
    	}	
    	return min;
    }
    
    
  • 相关阅读:
    mysql 获取字符串的长度
    mysql 字符类以及重复元字符
    mysql 字段拼接
    mysql 去除字符串中的空格
    mysql 正则表达式
    mysql 选出前五个元素
    mysql regexp 表达式
    mysql 选择所有同学名字
    mysql 获取数学成绩最高以及最低的同学
    mysql 获取单个科目的平均分
  • 原文地址:https://www.cnblogs.com/zhengkang/p/5770599.html
Copyright © 2011-2022 走看看