c++重载运算符实现分数加减乘除

实现结果如下图所示:
代码如下所示:
#include <iostream>

using namespace std;

class Rational {
public:
	Rational operator+(Rational rhs);
	Rational operator-(Rational rhs);
	Rational operator*(Rational rhs);
	Rational operator/(Rational rhs);
	Rational(int num, int denom);

private:
	void normalize(); //负责对分数做化简工作

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

	friend std::ostream& operator<<(ostream& os, Rational f);
};

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);

	//求出最大公约数,a
	//eg: a=12,b=9.
	//t=3 a=9 b=3
	//t=0 a=3 b=0
	while (b > 0) {
		int t = a % b;//t为余数
		a = b; //a取除数
		b = t; //b取余数
	}

	//分子、分母分别处以最大公约数
	numerator /= a;
	denominator /= a;
}

//重载运算符函数是类的成员函数
//分数是先通分再加减
// a*d+b*c / b*d
Rational Rational::operator+(Rational rhs) {
	int a = numerator;
	int b = denominator;
	int c = rhs.numerator;
	int d = rhs.denominator;

	return Rational(a*d+b*c,b*d);
}
Rational Rational::operator-(Rational rhs) {
	int a = numerator;
	int b = denominator;
	int c = rhs.numerator;
	int d = rhs.denominator;

	return Rational(a*d - b*c, b*d);

	/*
	**这里有个偷懒的写法
	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;

	return Rational(a*c, b*d);
}
Rational Rational::operator/(Rational rhs) {
	int t = rhs.denominator;
	rhs.denominator = rhs.numerator;
	rhs.numerator = t;

	return operator*(rhs);
}
//该函数是友元函数,因此不用加空间域名
ostream& operator<<(ostream& os, Rational f);

int main() {
	Rational r1(4, 18);
	Rational r2(14, 18);

	cout << r1 << " + " << r2 << " = " << (r1 + r2) << endl;
	cout << r1 << " - " << r2 << " = " << (r1 - r2) << endl;
	cout << r1 << " * " << r2 << " = " << (r1 * r2) << endl;
	cout << r1 << " / " << r2 << " = " << (r1 / r2) << endl;

	system("pause");
	return 0;
}

ostream& operator<<(ostream& os, Rational f) {
	os << f.numerator << "/" << f.denominator;
	return os;
}


  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
可以利用运算符重载实现矩阵的加减乘除操作。以下是一个简单的实现示例: ```c++ #include <iostream> #include <vector> using namespace std; // 矩阵定义 class Matrix { public: Matrix(int r, int c) : rows(r), cols(c), data(r, vector<double>(c)) {} // 矩阵加法运算符重载 Matrix operator+(const Matrix& other) const { if (this->rows != other.rows || this->cols != other.cols) { throw runtime_error("Matrix dimensions do not match."); } Matrix result(rows, cols); for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { result.data[i][j] = this->data[i][j] + other.data[i][j]; } } return result; } // 矩阵减法运算符重载 Matrix operator-(const Matrix& other) const { if (this->rows != other.rows || this->cols != other.cols) { throw runtime_error("Matrix dimensions do not match."); } Matrix result(rows, cols); for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { result.data[i][j] = this->data[i][j] - other.data[i][j]; } } return result; } // 矩阵乘法运算符重载 Matrix operator*(const Matrix& other) const { if (this->cols != other.rows) { throw runtime_error("Matrix dimensions do not match."); } Matrix result(this->rows, other.cols); for (int i = 0; i < this->rows; ++i) { for (int j = 0; j < other.cols; ++j) { double sum = 0; for (int k = 0; k < this->cols; ++k) { sum += this->data[i][k] * other.data[k][j]; } result.data[i][j] = sum; } } return result; } // 矩阵除法运算符重载 Matrix operator/(const Matrix& other) const { if (this->cols != other.rows) { throw runtime_error("Matrix dimensions do not match."); } Matrix inverse = other.inverse(); // 先求逆矩阵 return (*this) * inverse; // 矩阵乘法实现除法 } // 求逆矩阵 Matrix inverse() const { if (this->rows != this->cols) { throw runtime_error("Matrix is not square."); } int n = this->rows; Matrix result(n, n); // 先做初等变换,将原矩阵变成单位矩阵 for (int i = 0; i < n; ++i) { result.data[i][i] = 1; } for (int i = 0; i < n; ++i) { double factor = this->data[i][i]; for (int j = 0; j < n; ++j) { this->data[i][j] /= factor; result.data[i][j] /= factor; } for (int k = 0; k < n; ++k) { if (k == i) { continue; } double factor = this->data[k][i]; for (int j = 0; j < n; ++j) { this->data[k][j] -= factor * this->data[i][j]; result.data[k][j] -= factor * result.data[i][j]; } } } return result; } private: int rows, cols; // 矩阵行数和列数 vector<vector<double>> data; // 矩阵数据 }; int main() { Matrix A(2, 3); A.data = {{1, 2, 3}, {4, 5, 6}}; Matrix B(3, 2); B.data = {{7, 8}, {9, 10}, {11, 12}}; Matrix C = A + A; C = C - A; Matrix D = A * B; Matrix E = D / B; return 0; } ``` 在上面的示例代码中,我们定义一个 `Matrix` 来表示矩阵,并实现了矩阵加减乘除四种运算符重载。其中矩阵除法是通过先求逆矩阵再做矩阵乘法实现的。在 `Matrix` 中还实现了求逆矩阵的数 `inverse()`,用于求矩阵的逆矩阵。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值