PAT 1034 有理数四则运算 C语言实现
作者:互联网
本题要求编写程序,计算 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
#include<stdio.h>
#include<stdlib.h>
void yue_fen(long long int *fen_zi,long long int *fen_mu,long long int *k)
{
*k = *fen_zi / *fen_mu;
if(*k != 0)
{
*fen_zi = llabs(*fen_zi % *fen_mu);
}
else
{
*fen_zi = *fen_zi % *fen_mu;
}
*fen_mu = llabs(*fen_mu);
for(int i = 10 ; i >= 2; i--)
{
if(0 == (*fen_zi % i) && 0 == (*fen_mu % i))
{
*fen_zi = *fen_zi / i;
*fen_mu = *fen_mu / i;
i = 11;
}
}
}
int main()
{
long long int a1 = 0, b1 = 0, a2 = 0, b2 = 0,k1 = 0,k2 = 0;
long long int resu_k[4]= {},resu_a[4] = {},resu_b[4] = {};
scanf("%lld/%lld %lld/%lld",&a1,&b1,&a2,&b2);
char symbol[4] = {'+','-','*','/'};
//和、差
if(0 == a1)
{
resu_a[0] = a2;
resu_b[0] = b2;
resu_a[1] = a2;
resu_b[1] = b2;
}
else if(0 == a2)
{
resu_a[0] = a1;
resu_b[0] = b1;
resu_a[1] = a1;
resu_b[1] = b1;
}
else
{
resu_a[0] = a1 * b2 + a2 * b1;
resu_b[0] = b1 * b2;
resu_a[1] = a1 * b2 - a2 * b1;
resu_b[1] = b1 *b2;
}
yue_fen(&resu_a[0],&resu_b[0],&resu_k[0]);
yue_fen(&resu_a[1],&resu_b[1],&resu_k[1]);
//积
if(0 == a1 || 0 == a2)
{
resu_a[2] = 0;
resu_k[2] = 0;
}
else
{
resu_a[2] = a1 * a2;
resu_b[2] = b1 * b2;
yue_fen(&resu_a[2],&resu_b[2],&resu_k[2]);
}
//商
int mark = 0;
if(0 == a1 || 0 == a2)
{
mark = 1;
}
else if(a1 < 0 && a2 >0 || (a2 < 0 && a1 > 0))
{
resu_a[3] = -1*llabs(a1) * llabs(b2);
resu_b[3] = llabs(a2)* llabs(b1);
yue_fen(&resu_a[3],&resu_b[3],&resu_k[3]);
}
else
{
resu_a[3] = -1 * a1 * b2;
resu_b[3] = -1 * a2 * b1;
yue_fen(&resu_a[3],&resu_b[3],&resu_k[3]);
}
//分子分母有理化
yue_fen(&a1,&b1,&k1);
yue_fen(&a2,&b2,&k2);
for(int i = 0 ; i < 4 ; i++) //分别输出四则运算
{
if(0 == a1) //判断第一个有理数
{
if(k1 < 0)
printf("(%lld)",k1);
else
printf("%lld",k1);
}
else
{
if(0 == k1)
{
if(a1 > 0)
printf("%lld/%lld",a1,b1);
else
printf("(%lld/%lld)",a1,b1);
}
else if(k1 < 0)
printf("(%lld %lld/%lld)",k1,a1,b1);
else
printf("%lld %lld/%lld",k1,a1,b1);
}
printf(" %c ",symbol[i]);
if(0 == a2) //判断第二个有理数
{
if(k2 < 0)
printf("(%lld)",k2);
else
printf("%lld",k2);
}
else
{
if(0 == k2)
{
if(a2 > 0)
printf("%lld/%lld",a2,b2);
else
printf("(%lld/%lld)",a2,b2);
}
else if(k2 < 0)
printf("(%lld %lld/%lld)",k2,a2,b2);
else
printf("%lld %lld/%lld",k2,a2,b2);
}
printf(" = ");
if(3 == i && 1 == mark)
{
printf("Inf\n");
}
else if(0 == resu_a[i]) //判断结果
{
if(resu_k[i] < 0)
printf("(%lld)\n",resu_k[i]);
else
printf("%lld\n",resu_k[i]);
}
else
{
if(resu_k[i] < 0)
printf("(%lld %lld/%lld)\n",resu_k[i],resu_a[i],resu_b[i]);
else if(0 == resu_k[i] && resu_a[i] > 0)
printf("%lld/%lld\n",resu_a[i],resu_b[i]);
else if(0 == resu_k[i] && resu_a[i] < 0)
printf("(%lld/%lld)\n",resu_a[i],llabs(resu_b[i]));
else
printf("%lld %lld/%lld\n",resu_k[i],resu_a[i],resu_b[i]);
}
}
return 0;
}
标签:PAT,fen,resu,a1,else,a2,C语言,1034,lld 来源: https://blog.csdn.net/weixin_45393375/article/details/99601125