1034 有理数四则运算 (20 分)

简介: 1034 有理数四则运算 (20 分)

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

很繁的一个题目,如果正常的计算要分很多类,用到很多if else,,,因为是分数,没有办法直接进行运算,所以想通分后进行运算,

//注:由于时间原因,代码还未完成,只实现了加法。

#include<iostream>
#include<cmath>
#include<string>
using namespace std;
//返回最大公约数。最小公倍数是a1*a2/最大公倍数
long long gcd(long long t1, long long t2) {
  return t2 == 0 ? t1 : gcd(t2, t1 % t2);
};
void print_fs(int z, int fz, int fm);
void calcu(int afz0, int  afm, int bfz0, int bfm, char ch);
int main()
{
  int afz0, afm, bfz0, bfm;
  char ch;
  scanf_s("%d/%d %d/%d", &afz0, &afm, &bfz0, &bfm);
  int az = afz0 / afm;
  int afz = afz0 - az * afm;
  int bz = bfz0 / bfm;
  int bfz = bfz0 - bz * bfm;
  print_fs(az, afz, afm);
  cout << " + ";
  print_fs(bz, bfz, bfm);
  cout << " = ";
  calcu(afz0, afm, bfz0, bfm, '+');
  return 0;
}
 
void print_fs(int z, int fz,int fm) {
  if (z > 0) {
    if (fz > 0)
      printf("%d %d/%d", z, fz ,fm);
    else
      printf("%d", z);
  }
  if (z < 0) {
    if (fz > 0)
      printf("(%d %d/%d)", z, fz, fm);
    else
      printf("(%d)", z);
  }
  if(z==0) {
    if (fz > 0)
      printf("%d/%d", fz, fm);
    if(fz <0)
      printf("(%d/%d)", fz, fm);
    if(fz==0)
      printf("0");
  }
}
 
void calcu(int afz0, int  afm, int bfz0, int bfm,char ch) {
  int gfm = afm * bfm / gcd(afm, bfm);  //公分母
  int afz = afz0 * gfm / afm;
  int bfz = bfz0 * gfm / bfm;
  switch (ch)
  { 
  case '+': {
    int sum_fz = afz + bfz;
    int sum_z = sum_fz / gfm;
    int sum_fz1 = sum_fz -sum_z*gfm;
    print_fs(sum_z, abs(sum_fz1/gcd(sum_fz1,gfm)),abs( gfm/ gcd(sum_fz1, gfm)));
  }
  default:
    break;
  }
}

//我一开始在print_fs用if else 还用错了,后来去掉了else

一开始写成

if(a>0)...

if(a<0)...

else...

开始脑子抽了 以为这个else 就是a==0的时候。 实际上else是和最近的if(a<0)配对,等价于if(a>=0)


下午回来后继续做,然后是部分通过。

以为是长度问题,把int改成long long,还是错误。

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cmath>
#include<string>
using namespace std;
//返回最大公约数。最小公倍数是a1*a2/最大公倍数
long long gcd(long long t1, long long t2) {
  return t2 == 0 ? t1 : gcd(t2, t1 % t2);
};
void print_fs(long long z, long long fz, long long fm);
void calcu(long long afz0, long long  afm, long long bfz0, long long bfm, char ch);
void print_calcu_result(long long az, long long afz, long long afm, long long bz, long long bfz, long long bfm, long long afz0, long long bfz0, char yunsuan);
 
int main()
{
  long long afz0, afm, bfz0, bfm;
  char ch;
  scanf("%lld/%lld %lld/%lld", &afz0, &afm, &bfz0, &bfm);
  long long az = afz0 / afm;
  long long afz = afz0 - az * afm;
  long long bz = bfz0 / bfm;
  long long bfz = bfz0 - bz * bfm;
  print_calcu_result(az, afz, afm, bz, bfz, bfm, afz0, bfz0, '+');
  print_calcu_result(az, afz, afm, bz, bfz, bfm, afz0, bfz0, '-');
  print_calcu_result(az, afz, afm, bz, bfz, bfm, afz0, bfz0, '*');
  print_calcu_result(az, afz, afm, bz, bfz, bfm, afz0, bfz0, '/');
  return 0;
}
 
void print_fs(long long z, long long fz,long long fm) {
  if (z > 0) {
    if (fz > 0)
      printf("%lld %lld/%lld", z, fz ,fm);
    if(fz ==0)
      printf("%lld", z);
  }
  if (z < 0) {
    if (fz > 0)
      printf("(%lld %lld/%lld)", z, fz, abs(fm));
    if(fz==0)
      printf("(%lld)", z);
  }
  if(z==0) {
    if (fz > 0 && fm>0)
      printf("%lld/%lld", fz, fm);
    if (fz > 0 && fm < 0)
      printf("(%lld/%lld)", -fz, -fm);
    if(fz <0)
      printf("(%lld/%lld)", fz, (fm));
    if(fz==0)
      printf("0");
  }
}
 
void calcu(long long afz0, long long  afm, long long bfz0, long long bfm,char ch) {
  long long gfm = afm * bfm / gcd(afm, bfm);  //公分母
  long long afz = afz0 * gfm / afm;
  long long bfz = bfz0 * gfm / bfm;
  switch (ch)
  { 
  case '-': {
    bfz *= -1;
  }
  case '+': {
    long long sum_fz = afz + bfz;
    long long sum_z = sum_fz / gfm;
    long long sum_fz1 = sum_fz -sum_z*gfm;
    print_fs(sum_z, (sum_fz1/gcd(sum_fz1,gfm)),( gfm/ gcd(sum_fz1, gfm)));
    break;
  } 
  case '/': {
    if (bfz == 0) {
      cout << "Inf";
      break;
    }
    else {
      long long t = bfz0;
      bfz0 = bfm;
      bfm = t;
    }
  }
  case '*': {
    long long ji_fz = afz0 * bfz0;
    gfm = afm * bfm;
    long long ji_z = ji_fz / gfm;
    long long ji_fz1 = ji_fz - ji_z * gfm;
    print_fs(ji_z, (ji_fz1 /(gcd(ji_fz1, gfm)) ),( (gfm /(gcd(ji_fz1, gfm))) ));
    break;
  }
 
  default:
    break;
  }
}
 
void print_calcu_result(long long az,long long afz,long long afm,long long bz,long long bfz,long long bfm,long long afz0,long long bfz0,char yunsuan) {
  print_fs(az, afz, afm);
  cout << " "<<yunsuan<<" ";
  print_fs(bz, bfz, bfm);
  cout << " = ";
  calcu(afz0, afm, bfz0, bfm, yunsuan);
  cout << endl;
}


看着这么长,有有些乱,很难找错。

 

把分数做成了一个类,看起来结构就很比较清楚了。

然而还是有两处测试没有通过。

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cmath>
using namespace std;
int gcd(int t1, int t2) {
  return t2 == 0 ? t1 : gcd(t2, t1 % t2);
}
 
class fenshu {
public:
  int zhengshu;
  int fenzi;
  int zfenzi;
  int fenmu;
public:
  fenshu() :zhengshu(0), fenzi(0), zfenzi(0), fenmu(0) {}
  fenshu(int qzheng, int qfenzi, int qfenmu,int qzfenzi=0) :zhengshu(qzheng), fenzi(qfenzi),
    fenmu(qfenmu),zfenzi(qzfenzi) { }
  //标准形式
  void standard() {
    zhengshu += fenzi / fenmu; //整数部分
    zfenzi = fenzi - zhengshu * fenmu; //真分子
  }
  //输出标准形式
  void print_fs();
  fenshu operator+(fenshu b) {
    int gfm = fenmu * b.fenmu / gcd(fenmu, b.fenmu);  //公分母
    int afz = fenzi * gfm / fenmu;
    int bfz = b.fenzi * gfm / b.fenmu;
    int sum_fz = afz + bfz;
    int sum_z = sum_fz / gfm;
    int sum_fz1 = sum_fz - sum_z * gfm;
    return fenshu(sum_z, (sum_fz1 / gcd(sum_fz1, gfm)), (gfm / gcd(sum_fz1, gfm)), (sum_fz1 / gcd(sum_fz1, gfm)));
  }
  fenshu operator-(fenshu b) {
    fenshu fub = fenshu(b.zhengshu, b.fenzi*-1, b.fenmu);
    return (*this + fub);
  }
  fenshu operator*(fenshu b) {
    int ji_fz = fenzi * b.fenzi;
    int gfm = fenmu * b.fenmu;
    int ji_z = ji_fz / gfm;
    int ji_fz1 = ji_fz - ji_z * gfm;
    return fenshu(ji_z, ji_fz1 / gcd(ji_fz1, gfm), gfm / gcd(ji_fz1, gfm), ji_fz1 / gcd(ji_fz1, gfm));
  }
  fenshu operator/(fenshu b) {
    if (b.fenzi == 0) {
      cout << "Inf";
      return fenshu(1, -1, -1,-1);
    }
    else {
      fenshu t = fenshu(b.zhengshu, b.fenmu, b.fenzi);
      return (*this *t);
    }
  }
};
 
void fenshu::print_fs() {
  if (zhengshu > 0) {
    if (zfenzi > 0)
      printf("%d %d/%d", zhengshu, zfenzi ,fenmu);
    if(zfenzi ==0)
      printf("%d", zhengshu);
  }
  if (zhengshu < 0) {
    if (zfenzi > 0)
      printf("(%d %d/%d)", zhengshu, zfenzi, abs(fenmu));
    if(zfenzi==0)
      printf("(%d)", zhengshu);
  }
  if(zhengshu==0) {
    if (zfenzi > 0 && fenmu>0)
      printf("%d/%d", zfenzi, fenmu);
    if (zfenzi > 0 && fenmu < 0)
      printf("(%d/%d)", -zfenzi, -fenmu);
    if(zfenzi <0)
      printf("(%d/%d)", zfenzi, (fenmu));
    if(zfenzi==0)
      printf("0");
  }
}
 
int main()
{
 
  fenshu a, b;
  char ch;
  scanf("%d/%d %d/%d", &a.fenzi, &a.fenmu, &b.fenzi, &b.fenmu);
 
  a.standard();
  b.standard();
  a.print_fs(); cout << " + "; b.print_fs(); cout << " = "; (a + b).print_fs(); cout << endl;
  a.print_fs(); cout << " - "; b.print_fs(); cout << " = "; (a - b).print_fs(); cout << endl;
  a.print_fs(); cout << " * "; b.print_fs(); cout << " = "; (a * b).print_fs(); cout << endl;
  a.print_fs(); cout << " / "; b.print_fs(); cout << " = "; (a / b).print_fs(); cout << endl;
  return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cmath>
using namespace std;
 
int gcd(int t1, int t2) {
  return t2 == 0 ? t1 : gcd(t2, t1 % t2);
}
class fenshu {
public:
  int zhengshu;
  int fenzi;
  int zfenzi;
  int fenmu;
public:
  fenshu() :zhengshu(0), fenzi(0), zfenzi(0), fenmu(0) {}
  fenshu(int qzheng, int qfenzi, int qfenmu,int qzfenzi=0) :zhengshu(qzheng), fenzi(qfenzi),
    fenmu(qfenmu),zfenzi(qzfenzi) { }
  //标准形式
  fenshu standard() {
    int gcdfz_fm = gcd(fenmu, fenzi);
    if (gcdfz_fm != 0) {
    fenzi /= gcdfz_fm;
    fenmu /= gcdfz_fm;
    }
    zhengshu += fenzi / fenmu; //整数部分
    zfenzi = fenzi - zhengshu * fenmu; //真分子
    return fenshu(zhengshu, fenzi, fenmu, zfenzi);
  }
  //输出标准形式
  void print_fs();
  fenshu operator+(fenshu b) {
    int r_fenzi = fenzi * b.fenmu + b.fenzi*fenmu;
    int r_fenmu = fenmu * b.fenmu;
    return fenshu(0,r_fenzi,r_fenmu,0);
  }
  fenshu operator-(fenshu b) {
    b.fenzi *= -1;
    return *this + b;
  }
  fenshu operator*(fenshu b) {
    int ji_fz = fenzi * b.fenzi;
    int gfm = fenmu * b.fenmu;
    return fenshu(0, ji_fz, gfm, 0);
  }
  fenshu operator/(fenshu b) {
    if (b.fenzi == 0) {
      cout << "Inf";
      return fenshu(1, -1, -1,-1);
    }
    else {
      fenshu t = fenshu(b.zhengshu, b.fenmu, b.fenzi);
      return (*this *t);
    }
  }
};
 
void fenshu::print_fs() {
  if (zhengshu > 0) {
    if (zfenzi > 0)
      printf("%d %d/%d", zhengshu, zfenzi ,fenmu);
    if(zfenzi ==0)
      printf("%d", zhengshu);
  }
  if (zhengshu < 0) {
    if (zfenzi > 0)
      printf("(%d %d/%d)", zhengshu, zfenzi, fenmu);
    if(zfenzi==0)
      printf("(%d)", zhengshu);
    if (zfenzi < 0)
      printf("(%d %d/%d)", zhengshu, abs(zfenzi), fenmu);
  }
  if(zhengshu==0) {
    if (zfenzi > 0 )
      printf("%d/%d", zfenzi, fenmu);
    if(zfenzi <0)
      printf("(%d/%d)", zfenzi, (fenmu));
    if(zfenzi==0)
      printf("0");
  }
}
 
int main()
{
  fenshu a, b;
  scanf("%d/%d %d/%d", &a.fenzi, &a.fenmu, &b.fenzi, &b.fenmu);
  a.standard();
  b.standard();
  a.print_fs(); cout << " + "; b.print_fs(); cout << " = "; (a + b).standard().print_fs(); cout << endl;
  a.print_fs(); cout << " - "; b.print_fs(); cout << " = "; (a - b).standard().print_fs(); cout << endl;
  a.print_fs(); cout << " * "; b.print_fs(); cout << " = "; (a * b).standard().print_fs(); cout << endl;
  a.print_fs(); cout << " / "; b.print_fs(); cout << " = "; (a / b).standard().print_fs(); cout << endl;
  return 0;
}
相关文章
|
5月前
|
BI
1051 复数乘法 (15 分)
1051 复数乘法 (15 分)
|
6月前
|
算法 测试技术 C#
【数学】 【分数】 【字符串】972. 相等的有理数
【数学】 【分数】 【字符串】972. 相等的有理数
|
6月前
|
存储 算法
计算器——可支持小数的任意四则运算(中缀表达式转为后缀表达式算法)
计算器——可支持小数的任意四则运算(中缀表达式转为后缀表达式算法)
68 1
|
6月前
leetcode-592:分数加减运算
leetcode-592:分数加减运算
51 0
wustojc2002整数四则运算
wustojc2002整数四则运算
63 0
PTA 1034 有理数四则运算 (20 分)
本题要求编写程序,计算 2 个有理数的和、差、积、商。
112 0
7-2 一元多项式的乘法与加法运算 (20 分)
7-2 一元多项式的乘法与加法运算 (20 分)
143 0
L1-080 乘法口诀数列 (20 分)
L1-080 乘法口诀数列 (20 分)
221 0