1034. 有理数四则运算(20)
本题要求编写程序,计算2个有理数的和、差、积、商。
输入格式:
输入在一行中按照“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
这道题真的是逼着新手去练习函数了。当然,不觉得主函数函数体太长看着难受还可以继续反抗。把加、减、乘、除四个模块化为函数能使程序可读性大大加强。我是分是否为0写的函数,还有几个负责求最大公因数和输出的函数,这次的代码就有点长了:
#include
#include
void case1(); //a1=0,a2=0
void case2(long a2,long b2); //a1=0,a2!=0
void case3(long a1,long b1); //a1!=0,a2=0
void case4(long a1,long b1,long a2,long b2); //a1!=0,a2!=0
void pri(long a,long b,long c);
int zuida(long a,long b);
int main(void)
{
long a1,a2,b1,b2;
scanf("%ld%*c%ld %ld%*c%ld",&a1,&b1,&a2,&b2);
if (a1==0 && a2 ==0) case1();
else if (a1==0 && a2!=0) case2(a2,b2);
else if (a1!=0 && a2==0) case3(a1,b1);
else case4(a1,b1,a2,b2);
return 0;
}
void case1()
{
printf("0 + 0 = 0\n");
printf("0 - 0 = 0\n");
printf("0 * 0 = 0\n");
printf("0 / 0 = Inf");
}
void case2(long a2,long b2) //a1=0,a2!=0
{
printf("0 + ");pri(a2,b2,zuida(fabs(a2),fabs(b2)));printf(" = ");pri(a2,b2,zuida(fabs(a2),fabs(b2)));
printf("\n0 - ");pri(a2,b2,zuida(fabs(a2),fabs(b2)));printf(" = ");pri(-a2,b2,zuida(fabs(a2),fabs(b2)));
printf("\n0 * ");pri(a2,b2,zuida(fabs(a2),fabs(b2)));printf(" = 0");
printf("\n0 / ");pri(a2,b2,zuida(fabs(a2),fabs(b2)));printf(" = 0");
}
void case3(long a1,long b1) //a1!=0,a2=0
{
pri(a1,b1,zuida(fabs(a1),fabs(b1)));printf(" + 0 = ");pri(a1,b1,zuida(fabs(a1),fabs(b1)));printf("\n");
pri(a1,b1,zuida(fabs(a1),fabs(b1)));printf(" - 0 = ");pri(a1,b1,zuida(fabs(a1),fabs(b1)));printf("\n");
pri(a1,b1,zuida(fabs(a1),fabs(b1)));printf(" * 0 = 0");printf("\n");
pri(a1,b1,zuida(fabs(a1),fabs(b1)));printf(" / 0 = Inf");
}
void case4(long a1,long b1,long a2,long b2) //a1!=0,a2!=0
{
pri(a1,b1,zuida(fabs(a1),fabs(b1)));printf(" + ");pri(a2,b2,zuida(fabs(a2),fabs(b2)));printf(" = ");pri(a1*b2+a2*b1,b1*b2,zuida(fabs(a1*b2+a2*b1),fabs(b1*b2)));printf("\n");
pri(a1,b1,zuida(fabs(a1),fabs(b1)));printf(" - ");pri(a2,b2,zuida(fabs(a2),fabs(b2)));printf(" = ");pri(a1*b2-a2*b1,b1*b2,zuida(fabs(a1*b2-a2*b1),fabs(b1*b2)));printf("\n");
pri(a1,b1,zuida(fabs(a1),fabs(b1)));printf(" * ");pri(a2,b2,zuida(fabs(a2),fabs(b2)));printf(" = ");pri(a1*a2,b1*b2,zuida(fabs(a1*a2),fabs(b1*b2)));printf("\n");
pri(a1,b1,zuida(fabs(a1),fabs(b1)));printf(" / ");pri(a2,b2,zuida(fabs(a2),fabs(b2)));printf(" = ");pri(a1*b2,b1*a2,zuida(fabs(a1*b2),fabs(b1*a2)));
}
void pri(long a,long b,long c)
{
a=a/c;b=b/c;
if (b<0)
{
a=-a;b=-b;
}
if (a%b==0)
{
if (a<0)
{
printf("(%d)",a/b);
}
else
{
printf("%d",a/b);
}
}
else
{
if (a<0)
{
if (-a>b)
{
printf("(%d %d/%d)",a/b,-a%b,b);
}
else
{
printf("(%d/%d)",a,b);
}
}
else
{
if (a>b)
{
printf("%d %d/%d",a/b,a%b,b);
}
else
{
printf("%d/%d",a,b);
}
}
}
}
int zuida(long a,long b)
{
long c;
if (a