C++ 加减乘除重载

#include <iostream>
#include <string>
#include <stdlib.h>
class Rational
{
public:
    Rational(int num, int denom);//num 分子,denom 分母
    Rational operator+(Rational rhs);//rhs == right hand side
    Rational operator-(Rational rhs);
    Rational operator*(Rational rhs);
    Rational operator/(Rational rhs);

    void print();
private:
    void normalize(); //负责对分数的简化处理

    int numerator; //分子
    int denominator; //分母

};

Rational::Rational(int num, int denom)
{
    numerator = num;
    denominator = denom;

    normalize();
}

void Rational::normalize()
{
    if (denominator < 0)
    {
        numerator = -numerator;
        denominator = -denominator;
    }
    //欧里几德算法,分子分母除以最大公约数化简。
    int a = abs(numerator);
    int b = abs(denominator);

    while (b>0)
    {
        int t = a%b;
        a = b;
        b = t;
    }
    numerator /= a;
    denominator /= a;
}

Rational Rational::operator+(Rational rhs)
{
    int a = numerator;
    int b = denominator;
    int c = rhs.numerator;
    int d = rhs.denominator;

    int e = a*b + c*d;
    int f = b*d;

    return Rational(e,f);
}

Rational Rational::operator-(Rational rhs)
{
    rhs.numerator = -rhs.numerator;
    return operator+(rhs);
}

Rational Rational::operator*(Rational rhs)
{
    int a = numerator;
    int b = denominator;
    int c = rhs.numerator;
    int d = rhs.denominator;   

    int e = a*c;
    int f = b*d;

    return Rational(e,f);
}

Rational Rational::operator/(Rational rhs)
{
    int t = rhs.numerator;
    rhs.numerator = rhs.denominator;
    rhs.denominator = t;

    return operator*(rhs);
}

void Rational::print()
{
    if(numerator%denominator == 0)
        std::cout << numerator / denominator;
    else
        std::cout << numerator << "/" << denominator;
}


int main()
{
    Rational f1(2,16);
    Rational f2(7,8);

    Rational res = f1 + f2;
    f1.print();
    std::cout<<" + ";
    f2.print();
    std::cout<<" = ";
    res.print();
    std::cout<<"\n";

    res = f1 - f2;
    f1.print();
    std::cout<<" - ";
    f2.print();
    std::cout<<" = ";
    res.print();
    std::cout<<"\n";

    res = f1 * f2;
    f1.print();
    std::cout<<" * ";
    f2.print();
    std::cout<<" = ";
    res.print();
    std::cout<<"\n";

    res = f1 / f2;
    f1.print();
    std::cout<<" / ";
    f2.print();
    std::cout<<" = ";
    res.print();
    std::cout<<"\n";
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我可以通过类重载运算符来实现矩阵的加减乘除。下面是一个简单的示例代码: ```c++ #include <iostream> using namespace std; class Matrix { private: int rows; int cols; double* data; public: Matrix(int m, int n) : rows(m), cols(n), data(new double[m*n]) {} Matrix(const Matrix& mat) : rows(mat.rows), cols(mat.cols), data(new double[mat.rows * mat.cols]) { for (int i = 0; i < rows * cols; i++) { data[i] = mat.data[i]; } } ~Matrix() { delete[] data; } double& operator()(int i, int j) { return data[i*cols + j]; } const double& operator()(int i, int j) const { return data[i*cols + j]; } Matrix& operator=(const Matrix& mat) { if (this != &mat) { delete[] data; rows = mat.rows; cols = mat.cols; data = new double[rows * cols]; for (int i = 0; i < rows * cols; i++) { data[i] = mat.data[i]; } } return *this; } Matrix operator+(const Matrix& mat) const { Matrix result(rows, cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { result(i, j) = (*this)(i, j) + mat(i, j); } } return result; } Matrix operator-(const Matrix& mat) const { Matrix result(rows, cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { result(i, j) = (*this)(i, j) - mat(i, j); } } return result; } Matrix operator*(const Matrix& mat) const { if (cols != mat.rows) { throw runtime_error("Matrix dimensions do not match for multiplication."); } Matrix result(rows, mat.cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < mat.cols; j++) { double sum = 0; for (int k = 0; k < cols; k++) { sum += (*this)(i, k) * mat(k, j); } result(i, j) = sum; } } return result; } Matrix operator/(const Matrix& mat) const { if (mat.rows != mat.cols || rows != cols || mat.rows != rows) { throw runtime_error("Matrix dimensions do not match for division."); } Matrix result(rows, cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { result(i, j) = (*this)(i, j) / mat(i, j); } } return result; } friend ostream& operator<<(ostream& os, const Matrix& mat) { for (int i = 0; i < mat.rows; i++) { for (int j = 0; j < mat.cols; j++) { os << mat(i, j) << " "; } os << endl; } return os; } }; int main() { Matrix A(2, 3); A(0, 0) = 1; A(0, 1) = 2; A(0, 2) = 3; A(1, 0) = 4; A(1, 1) = 5; A(1, 2) = 6; cout << "Matrix A:" << endl; cout << A << endl; Matrix B(3, 2); B(0, 0) = 1; B(0, 1) = 4; B(1, 0) = 2; B(1, 1) = 5; B(2, 0) = 3; B(2, 1) = 6; cout << "Matrix B:" << endl; cout << B << endl; Matrix C = A + B; cout << "A + B:" << endl; cout << C << endl; Matrix D = A - B; cout << "A - B:" << endl; cout << D << endl; Matrix E = A * B; cout << "A * B:" << endl; cout << E << endl; Matrix F(2, 3); F(0, 0) = 2; F(0, 1) = 4; F(0, 2) = 6; F(1, 0) = 8; F(1, 1) = 10; F(1, 2) = 12; cout << "Matrix F:" << endl; cout << F << endl; Matrix G = A / F; cout << "A / F:" << endl; cout << G << endl; return 0; } ``` 在这个示例中,我们定义了一个 `Matrix` 类,并重载了 `operator+`、`operator-`、`operator*` 和 `operator/` 运算符。这些运算符可以用来执行矩阵加减乘除运算。我们还重载了括号运算符 `()`,以方便访问矩阵元素。 在 `main` 函数中,我们创建了两个矩阵 `A` 和 `B`,并演示了如何使用重载运算符来执行矩阵加减乘除运算。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alocus_

如果我的内容帮助到你,打赏我吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值