+运算符重载

题目已定义了一个分数类,所有成员函数均已实现。
请通过类外定义运算符+的重载函数,完成:分数1+分数2,分数+整数,整数+分数的运算;

算法提示
假定有Fraction对象f1、f2,f1和f2相加结果的简要算法描述如下:

分子=f1.分子 * f2.分母 + f2.分子 * f1.分母
分母=f1.分母 * f2.分母
Fraction f3(分子,分母)
f3.约分
函数接口定义:
class Fraction      //分数类
{
private: 
    int num = 0;    //分子
    int den = 1;    //分母
public:    
    //构造函数
    Fraction() {}
    Fraction(int num, int den);
    //打印分数 
    void PrintFraction() const;
    //约分
    Fraction Reduct(); 
    //提取分子 
    int GetNum() const;   
    //提取分母 
    int GetDen() const;   
};
裁判测试程序样例:
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
class Fraction      //分数
{
private: 
    int num = 0;    //分子
    int den = 1;    //分母
public:    
    //构造函数
    Fraction() {}
    Fraction(int num, int den);
    //打印分数 
    void PrintFraction() const;
    //约分
    Fraction Reduct(); 
    //提取分子 
    int GetNum() const;   
    //提取分母 
    int GetDen() const;   
};
//类的成员函数均已定义,代码略
/* 请在这里填写答案 */
int main()
{
    int n1,n2,n3,n4,n5,n6;
    char c;
    cin>>n1>>c>>n2>>n3>>c>>n4>>n5>>c>>n6;
    Fraction f1(n1,n2),f2(n3,n4),f3(n5,n6),f4;
    f4=1+f1+f2+f3+2;
    f4.PrintFraction();
    return 0;
}
输入样例:
1/2 1/12 1/12
输出样例:
11/3

 

// 重载运算符+,实现两个分数相加的功能
Fraction operator+(const Fraction& f1, const Fraction& f2) {
    // 计算分子
    int num = f1.GetNum() * f2.GetDen() + f2.GetNum() * f1.GetDen();
    // 计算分母
    int den = f1.GetDen() * f2.GetDen();
    // 构造新的Fraction对象
    Fraction result(num, den);
    // 对结果进行约分
    return result.Reduct();
}

// 重载运算符+,实现分数加整数的功能
Fraction operator+(const Fraction& f, int n) {
    // 将整数转换为分数,分子为整数乘以原分母再加上原分子,分母不变
    Fraction fraction(n * f.GetDen() + f.GetNum(), f.GetDen());
    // 对结果进行约分
    return fraction.Reduct();
}

// 重载运算符+,实现整数加分数的功能
Fraction operator+(int n, const Fraction& f) {
    // 调用前面定义的重载运算符+(Fraction, int)函数
    return f + n;
}

 

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Procast Procast Procast MeshCAST PreCAST DataCAST ProCAST PostCAST ViewCAST Ideas 1 (simulation + master modeler): * 2 Partition 3 simulation+ meshing 4 : file, export, ideas simulation universal file, *.unv OK Meshcast 1 Dos meshcast 2 File/open I-deas surface mesh(*.unv) 3 Check mesh, Check intersection Message Window 4 Tet mesher, full layer no layer , gen tet mesh 5 Display Ops ( bad element, Negative Jac) Smoothing (smooth ) save 6 Exit *.mesh Precast ( ) 1 precast * * 2 Geometry, units(mm), meshcast *.mesh Apply 3 check geom 4 Material , database add assign T F T F 5 Interface database( ) create( yes, Apply), assign 6 Boundary temperature , heat, velocity temperature velocity , heat symmetry( ) database add velocity u,v,w Temperature film coff ambi temp assign surface add Temperature, Velocity, Heat symmetry , assign select(Temperature velocity Heat select all store 7 Process Gravity ( 9.81m/s2 + - 8 Initial condition constant, Free surface, empty yes empty No 9 Run parameters, units ; general (INLEV 0, NSTEP , TFINAL ) thermal(TFREQ, QFREQ 5 10) flow VFREQ 5 10 FREESURFACE 1 2 2 LVSURF 1 10 Exit, go back, continue *d.dat *p.dat Datacast * Procast * dos Prostat * Number of steps=100 Simulated time=11.698071 S Time step=0.658118 S Percent filled=100% Solid fraction=0.713875% … Viewcast * Viewcast 1 A Materials, B X Y Z Ctrl,+Shift C F2 F3 D VIEW HIDDEN 2 A Contour, thermal temperature( ) B Steps, start=0 end= ,freq=1( Precast ) C PARAMETERS CONTINUOUS SINGLE-STEP D VIEW, PICTURE 3 A Viewcast PARAMETERS SEMI-AUTO(BASE= DELTA= ) MANUAL B PARAMETERS UNITS( Temperature ) C PARAMETERS FREE SURFACE D VIEW, PICTURE 4 A PARAMETERS, CONTINUOUS SINGLE-STEP ST G GIF P PostScrip B VIEW, PICTURE 5 A Contour,NONE B VECTOR, FLUID VELOCITY C PARAMETERS, COLOR VECTORS, MAGNITUDE D VIEW, PICTURE Ctrl+B Ctrl+S 6 A 0 1 Contour THERMAL, FRACTION SOLID B Reverse Video, Parameters REVERSE VIDEO C VIEW, PICTURE 7 Cut-off CUT-OFF FRACTION SOLID, A Contour THERMAL, FRACTION SOLID B PARA

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值