RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
C++实践分数类中运算符重载的方法参考

【项目-分数类中的运算符重载】

创新互联建站主营甘南网站建设的网络公司,主营网站建设方案,成都app软件开发,甘南h5小程序定制开发搭建,甘南网站营销推广欢迎甘南等地区企业咨询

(1)实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简)、比较(6种关系)的运算。

class CFraction
{
private:
  int nume; // 分子
  int deno; // 分母
public:
  //构造函数及运算符重载的函数声明
};
//重载函数的实现及用于测试的main()函数

(2)在(1)的基础上,实现分数类中的对象和整型数的四则运算。分数类中的对象可以和整型数进行四则运算,且运算符合交换律。例如:CFraction a(1,3),b; int i=2; 可以完成b=a+i;。同样,可以完成i+a, 45+a, a*27, 5/a等各种运算。

(3)定义分数的一目运算+和-,分别代表分数取正和求反,将“按位取反运算符”~重载为分数的求倒数运算。

(4)定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。

【参考解答】

#include 
#include 
using namespace std;
class CFraction
{
private:
  int nume; // 分子
  int deno; // 分母
public:
  CFraction(int nu=0,int de=1):nume(nu),deno(de) {}
  void simplify();
  //输入输出的重载
  friend istream &operator>>(istream &in,CFraction &x);
  friend ostream &operator<<(ostream &out,CFraction x);
  CFraction operator+(const CFraction &c); //两个分数相加,结果要化简
  CFraction operator-(const CFraction &c); //两个分数相减,结果要化简
  CFraction operator*(const CFraction &c); //两个分数相乘,结果要化简
  CFraction operator/(const CFraction &c); //两个分数相除,结果要化简
  CFraction operator+(); //取正一目运算
  CFraction operator-(); //取反一目运算
  CFraction operator~(); //取倒数一目运算
  bool operator>(const CFraction &c);
  bool operator<(const CFraction &c);
  bool operator==(const CFraction &c);
  bool operator!=(const CFraction &c);
  bool operator>=(const CFraction &c);
  bool operator<=(const CFraction &c);
};
// 分数化简
void CFraction::simplify()
{
  int m,n,r;
  n=fabs(deno);
  m=fabs(nume);
  while(r=m%n) // 求m,n的最大公约数
  {
    m=n;
    n=r;
  }
  deno/=n;   // 化简
  nume/=n;
  if (deno<0) // 将分母转化为正数
  {
    deno=-deno;
    nume=-nume;
  }
}
// 重载输入运算符>>
istream &operator>>(istream &in,CFraction &x)
{
  char ch;
  while(1)
  {
    cin>>x.nume>>ch>>x.deno;
    if (x.deno==0)
      cerr<<"分母为0, 请重新输入\n";
    else if(ch!='/')
      cerr<<"格式错误(形如m/n)! 请重新输入\n";
    else
      break;
  }
  return cin;
}
// 重载输出运算符<<
ostream &operator<<(ostream &out,CFraction x)
{
  cout<(const CFraction &c)
{
  int this_nume,c_nume,common_deno;
  this_nume=nume*c.deno;    // 计算分数通分后的分子,同分母为deno*c.deno
  c_nume=c.nume*deno;
  common_deno=deno*c.deno;
  if ((this_nume-c_nume)*common_deno>0) return true;
  return false;
}
// 分数比较大小
bool CFraction::operator<(const CFraction &c)
{
  int this_nume,c_nume,common_deno;
  this_nume=nume*c.deno;
  c_nume=c.nume*deno;
  common_deno=deno*c.deno;
  if ((this_nume-c_nume)*common_deno<0) return true;
  return false;
}
// 分数比较大小
bool CFraction::operator==(const CFraction &c)
{
  if (*this!=c) return false;
  return true;
}
// 分数比较大小
bool CFraction::operator!=(const CFraction &c)
{
  if (*this>c || *this=(const CFraction &c)
{
  if (*thisc) return false;
  return true;
}
int main()
{
  CFraction x,y,s;
  cout<<"输入x: ";
  cin>>x;
  cout<<"输入y: ";
  cin>>y;
  s=+x+y;
  cout<<"+x+y="<y) cout<<"大于";
  if (x

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对创新互联的支持。如果你想了解更多相关内容请查看下面相关链接


名称栏目:C++实践分数类中运算符重载的方法参考
URL地址:http://scyingshan.cn/article/jepdhi.html