//【任务3】实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简)、求反、比较(6种关系)的运算。
/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称: CFractioncpp
* 作 者: 计114-3 王兴锋
* 完成日期: 2012 年 4 月 9 日
* 版 本 号: V 1.0
* 对任务及求解方法的描述部分
* 输入描述:实现分数类中的运算符重载
* 问题描述:在分数类中可以完成分数的加减乘除(运算后再化简)、求反、比较(6种关系)的运算
* 程序输出:运算后的结果
* 程序头部的注释结束
*/
#include <iostream>
using namespace std;
class CFraction
{
private:
int nume; // 分子
int deno; // 分母
int gcd(int m, int n);
public:
//构造函数及运算符重载的函数声明
CFraction(int n = 1, int d = 1){if (d == 0) return;nume = n, deno = d;}
friend CFraction operator + (CFraction &c1, CFraction &c2);
friend CFraction operator - (CFraction &c1, CFraction &c2);
friend CFraction operator * (CFraction &c1, CFraction &c2);
friend CFraction operator / (CFraction &c1, CFraction &c2);
friend CFraction operator - (CFraction &c);
bool operator > (CFraction &c);
bool operator < (CFraction &c);
bool operator >= (CFraction &c);
bool operator <= (CFraction &c);
bool operator == (CFraction &c);
bool operator != (CFraction &c);
void simplify();
void display();
};
void CFraction::simplify()
{
int n = gcd(deno, nume);
deno /= n; // 化简
nume /= n;
}
int CFraction::gcd(int m, int n)
{
int r;
if (m < n){r = m; m = n; n = r;}
while(r = m % n) // 求m,n的最大公约数
{
m = n;
n = r;
}
return n;
}
void CFraction::display()
{
cout << nume << "/" << deno << endl;
}
CFraction operator + (CFraction &c1, CFraction &c2)
{
CFraction c;
c.deno = c1.deno * c2.deno;
c.nume = c1.nume*c2.deno + c2.nume*c1.deno;
c.simplify();
return c;
}
CFraction operator - (CFraction &c1, CFraction &c2)
{
CFraction c;
c.deno = c1.deno * c2.deno;
c.nume = c1.nume*c2.deno - c2.nume*c1.deno;
c.simplify();
return c;
}
CFraction operator * (CFraction &c1, CFraction &c2)
{
CFraction c;
c.deno = c1.deno * c2.deno;
c.nume = c1.nume * c2.nume;
c.simplify();
return c;
}
CFraction operator / (CFraction &c1, CFraction &c2)
{
CFraction c;
c.deno = c1.deno * c2.nume;
c.nume = c1.nume * c2.deno;
c.simplify();
return c;
}
CFraction operator - (CFraction &c)
{
return CFraction(-c.nume, c.deno);
}
bool CFraction::operator > (CFraction &c)
{
CFraction cf(*this / c);
if (cf.nume/cf.deno > 1)
return true;
else
return false;
}
bool CFraction::operator < (CFraction &c)
{
CFraction cf(*this / c);
if (cf.nume/cf.deno < 1)
return true;
else
return false;
}
bool CFraction::operator >= (CFraction &c)
{
CFraction cf(*this / c);
if (cf.nume/cf.deno >= 1)
return true;
else
return false;
}
bool CFraction::operator <= (CFraction &c)
{
CFraction cf(*this / c);
if (cf.nume/cf.deno <= 1)
return true;
else
return false;
}
bool CFraction::operator == (CFraction &c)
{
CFraction cf(*this / c);
if (cf.nume/cf.deno == 1)
return true;
else
return false;
}
bool CFraction::operator != (CFraction &c)
{
CFraction cf(*this / c);
if (cf.nume/cf.deno != 1)
return true;
else
return false;
}
//重载函数的实现及用于测试的main()函数
void main()
{
CFraction c1(3, 8), c2(1, 8), c3(1, 8);
cout << "c1=";
c1.display();
cout << "c2=";
c2.display();
cout << "c3=";
c3.display();
if (c1 > c2) cout << "c1 > c2" << endl;
if (c2 < c1) cout << "c2 < c1" << endl;
if (c2 >= c3) cout << "c2 >= c3" << endl;
if (c2 <= c3) cout << "c2 <= c3" << endl;
if (c2 == c3) cout << "c2 == c3" << endl;
if (c1 != c2) cout << "c1 != c2" << endl;
cout << "c1+c2=";
(c1 + c2).display();
cout << "c1-c2=";
(c1 - c2).display();
cout << "c1*c2=";
(c1 * c2).display();
cout << "c1/c2=";
(c1 / c2).display();
c3= -c3;
c3.display();
system("PAUSE");
}
/*
要认识的函数的返回值类型,不可随意的比较。
因其返回值类型并不是你想象中的数值。
*/
分享到:
相关推荐
主要是C++中,运算符重载的编写的实验,环境VS2010,内容很基本,与类相关
自己做的小型分数运算器 实现了运算符的重载。
自己很久前写的运算符重载小例子,实现了个别操作
C++程序设计实验——运算符重载,涉及单目、双目运算符的重载
C++单目运算符重载 单目运算符只有一个操作数,如!a,-b,&c,*p,还有最常用的++i和–i等。重载单目运算符的方法与重载双目运算符的方法是类似的。但由于单目运算符只有一个操作数,因此运算符重载函数只有一个参数...
(1)实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简)、比较(6种关系)的运算。 class CFraction { private: int nume; // 分子 int deno; // 分母 public: //构造函数及运算符...
实验七 运算符重载 一、实验目的 (1).编写程序熟悉运算符重载函数的定义和使用; (2).编写程序熟悉重载单目和双目运算符的方法; (3).编写程序熟悉流插入和提取运算符的重载; 二、实验要求 1、创建一个二维...
复数类的实现 运算符的重载 适合初学者学习查看
c++中,有一种集合类运算符重载的算法,该示例程序中提供了很多的重载类型。
这个是使用C++是实现的矩阵基本运算,使用了运算符重载技术和动态生成数组技术!
如运算符重载和函数重载 字符串连接,+=运算符重载 求子串: 左边几个字符,,, Left(int nCount) 右边几个,, Right(int nCount) 某个位置开始的几个字符 Mid(int nPos,int nCout) 2..进一步熟悉掌握对类的构造...
C++实验11--运算符重载.html
分数 2/4 等价于 1/2,并将作为分子中的 1 和分母中的 2 存储在对象中。 如果没有提供初始值设定项,请提供默认值为 1 的无参数构造函数。 提供执行以下每个操作的公共方法: a) 将两个有理数相加:相加的结果应...
详细介绍 C++运算符重载 的PPT 文中部分内容: 下标运算符“[ ]”重载 C++把下标运算符[ ]看成一个双目运算符,其操作数为<基本表达式>和<表达式>,对应的运算符为operator[ ],必须重载为一个成员函数。对于下标...
c++ demo,运算符重载,成员函数的实现
定义一个二维方阵类 matrix。通过重载二元运算符“+”、“-”、“*”和一元运算符“~”, 来实现矩阵加、矩阵减、矩阵乘以及矩阵转置。
一个c++运算符重载例子.属于原创,代码比较清晰
请定义一个Point类,将前置++和后置++运算符重载为成员函数,实现成员变量m_x和m_y的加一操作 同时重载流插入运算符,实现对Point类对象的格式化输出。例如 Point p; cout; 输出结果为: (0,0) 请根据给定的main...
定义了MTX类,用于保存矩阵,为了方便计算,用运算符重载来实现矩阵运算。
C++ 上课/复习ppt运算符重载.pptx