题目大意
对于两个有理数,您的任务是实现基本算术,即计算它们的和,差,乘积和商
解题思路
分别用格式化输入读取分子和分母,然后分别进行四则运算,
实际操作中格式化的输出应注意细节问题
- 第一次提交只通过了两个用例,先贴一波错误代码
`
#include<bits/stdc++.h>
using namespace std;
int num;
vector<int> vec;
//最大公因数
long long gcd(long long a, long long b){
return b == 0 ? abs(a) : gcd(b, a % b);
}
//分数相加
void sumRational(long long& r1, long long& r2, long long& R1, long long& R2) {
//分数相加,先通分,再相加
long long tem = r2;
r1 = r1 * R2;
r2 = r2 * R2;
R1 = R1 * tem;
R2 = R2 * tem;
r1 = r1 + R1;
long long vlue = gcd(r1, r2);
r1 /= vlue;
r2 /= vlue;
}
//相减
void reduceRational(long long& r1, long long& r2, long long& R1, long long& R2) {
//分数相加,先通分,再相加
long long tem = r2;
r1 = r1 * R2;
r2 = r2 * R2;
R1 = R1 * tem;
R2 = R2 * tem;
r1 = r1 - R1;
long long vlue = gcd(r1, r2);
r1 /= vlue;
r2 /= vlue;
}
//相乘
void mulRational(long long& r1, long long& r2, long long& R1, long long& R2) {
r1 *= R1;
r2 *= R2;
long long vlue = gcd(r1, r2);
r1 /= vlue;
r2 /= vlue;
}
//相除
bool diviseRational(long long& r1, long long& r2, long long& R1, long long& R2) {
if (R1 == 0) {
return false;
}
if (R1 < 0) {
R1 *= -1;
R2 *= -1;
}
r1 *= R2;
r2 *= R1;
long long vlue = gcd(r1, r2);
r1 /= vlue;
r2 /= vlue;
return true;
}
void myprint(long long& r1, long long r2) {
if (r1 == 0) {
cout << '0';
return;
}
else if (r1 > 0) {
if (r1 < r2) {
cout << r1 << '/' << r2;
}
else {
if (r1 % r2 == 0) {
cout << r1 / r2;
}
else {
cout << r1 / r2;
cout << ' ' << r1 % r2 << '/' << r2;
}
}
return;
}
else {
cout << "(-";
r1 *= -1;
if (r1 < r2) {
cout << r1 << '/' << r2;
}
else {
if (r1 % r2 == 0) {
cout << r1 / r2;
}
else {
cout << r1 / r2;
cout << ' ' << r1 % r2 << '/' << r2;
}
}
cout << ')';
return;
}
}
int main() {
long long numerator, denominator, res1, res2, tem1, tem2, tem3, tem4;
long long value;
scanf_s("%lld/%lld", &res1, &res2);
scanf_s("%lld/%lld", &numerator, &denominator);
//化简
value =gcd(res1, res2);
res1 /= value;
res2 /= value;
value = gcd(numerator, denominator);
numerator /= value;
denominator /= value;
tem1 = res1;
tem2 = res2;
tem3 = numerator;
tem4 = denominator;
sumRational(tem1, tem2, tem3, tem4);
if (tem1 != 0) {
myprint(res1, res2);
cout << " + ";
myprint(tem3, tem4);
cout << " = ";
myprint(tem1, tem2);
}
else {
cout << '0';
}
cout << endl;
tem1 = res1;
tem2 = res2;
tem3 = numerator;
tem4 = denominator;
reduceRational(tem1, tem2, tem3, tem4);
if (tem1 != 0) {
myprint(res1, res2);
cout << " - ";
myprint(tem3, tem4);
cout << " = ";
myprint(tem1, tem2);
}
else {
cout << '0';
}
cout << endl;
tem1 = res1;
tem2 = res2;
tem3 = numerator;
tem4 = denominator;
mulRational(tem1, tem2, tem3, tem4);
myprint(res1, res2);
cout << " * ";
myprint(tem3, tem4);
cout << " = ";
myprint(tem1, tem2);
cout << endl;
tem1 = res1;
tem2 = res2;
tem3 = numerator;
tem4 = denominator;
bool flag = diviseRational(tem1, tem2, tem3, tem4);
myprint(res1, res2);
cout << " / ";
myprint(numerator, denominator);
cout << " = ";
if (flag == true)
myprint(tem1, tem2);
else
cout << "Inf";
return 0;
}
修改策略
使用函数分离操作,修改函数内参数,使用3*2=6个形参,最后两个表示结果,通过引用传出结果。
#include<bits/stdc++.h>
using namespace std;
//计算最大公因数
long long gcd(long long a, long long b){
return b == 0 ? abs(a) : gcd(b, a % b);
}
//分数相加
void sumRational(long long r1, long long r2, long long R1, long long R2,long long& res1,long long& res2) {
//分数相加,先通分,再相加,再化简
res2 = r2 * R2;
res1 = r1 * R2 + R1 * r2;
long long vlue = gcd(res1, res2);
res1 /= vlue;
res2 /= vlue;
}
//相减
void reduceRational(long long r1, long long r2, long long R1, long long R2, long long& res1, long long& res2) {
res2 = r2 * R2;
res1 = r1 * R2 - R1 * r2;
long long vlue = gcd(res1, res2);
res1 /= vlue;
res2 /= vlue;
}
//相乘
void mulRational(long long r1, long long r2, long long R1, long long R2, long long& res1, long long& res2) {
res1 = r1 * R1;
res2 = r2 * R2;
long long vlue = gcd(res1, res2);
res1 /= vlue;
res2 /= vlue;
}
//相除
bool diviseRational(long long r1, long long r2, long long R1, long long R2, long long& res1, long long& res2) {
if (R1 == 0) {
return false;
}
if (R1 < 0) {
R1 *= -1;
R2 *= -1;
}
res1 = r1 * R2;
res2 = r2 * R1;
long long vlue = gcd(res1, res2);
res1 /= vlue;
res2 /= vlue;
return true;
}
//格式化打印分数
void myprint(long long r1, long long r2) {
//分子为0
if (r1 == 0) {
cout << '0';
return;
}
//分子大于0
else if (r1 > 0) {
if (r1 < r2) {//真分数
cout << r1 << '/' << r2;
}
else {//假分数
if (r1 % r2 == 0) {//整数
cout << r1 / r2;
}
else {//假分数
cout << r1 / r2;
cout << ' ' << r1 % r2 << '/' << r2;
}
}
return;
}
else {
cout << "(-";
r1 *= -1;
if (r1 < r2) {
cout << r1 << '/' << r2;
}
else {
if (r1 % r2 == 0) {
cout << r1 / r2;
}
else {
cout << r1 / r2;
cout << ' ' << r1 % r2 << '/' << r2;
}
}
cout << ')';
return;
}
}
int main() {
long long numerator, denominator, res1, res2, tem1 = 0, tem2 = 0, value;
//数据读入,使用long long类型记录数据
scanf_s("%lld/%lld", &res1, &res2);
scanf_s("%lld/%lld", &numerator, &denominator);
//化简
value =gcd(res1, res2);
res1 /= value;
res2 /= value;
value = gcd(numerator, denominator);
numerator /= value;
denominator /= value;
sumRational(res1, res2, numerator, denominator, tem1, tem2);
myprint(res1, res2);
cout << " + ";
myprint(numerator, denominator);
cout << " = ";
myprint(tem1, tem2);
cout << endl;
reduceRational(res1, res2, numerator, denominator, tem1, tem2);
myprint(res1, res2);
cout << " - ";
myprint(numerator, denominator);
cout << " = ";
myprint(tem1, tem2);
cout << endl;
mulRational(res1, res2, numerator, denominator, tem1, tem2);
myprint(res1, res2);
cout << " * ";
myprint(numerator, denominator);
cout << " = ";
myprint(tem1, tem2);
cout << endl;
bool flag = diviseRational(res1, res2, numerator, denominator, tem1, tem2);
myprint(res1, res2);
cout << " / ";
myprint(numerator, denominator);
cout << " = ";
if (flag == true)
myprint(tem1, tem2);
else
cout << "Inf";
return 0;
}