利用重载后的运算符求任意两个数的加减乘除

#include<iostream.h>
class Complex //定义一个Complex类
{
private:
double real;//实部
double imag; //虚部
public:
Complex(){} //定义构造函数
Complex(double r,double i) //重载构造函数
{
real=r;
imag=i;
}
void display()
{
cout<<"("<<real<<","<<imag<<")"<<endl;
}
Complex operator+(Complex i2)//重载加法运算符
{
Complex a;
a.real=this->real+i2.real;
a.imag=this->imag+i2.imag;
return a;
}
Complex operator-(Complex i2) //重载减法运算符
{
Complex b;
b.real=this->real-i2.real;
b.imag=this->imag-i2.imag;
return b;
}
Complex operator*(Complex i2) //重载乘法运算符
{
Complex c;
c.real=this->real*i2.real;
c.imag=this->imag*i2.imag;
return c;
}
Complex operator/(Complex i2) //重载除法运算符
{
Complex d;
d.real=this->real/i2.real;
d.imag=this->imag/i2.imag;
return d;
}

};
void main()
{

int x1,y1,x2,y2;

cin>>x1>>y1>>x2>>y2;
Complex a,b,c,d,i1(x1,y1),i2(x2,y2);
a=i1+i2; // 利用重载后的运算符求c1,c2的和
b=i1-i2;// 利用重载后的运算符求c1,c2的差

c=i1*i2;// 利用重载后的运算符求c1,c2的积

d=i1/i2;// 利用重载后的运算符求c1,c2相除

cout<<"i1=";i1.display();
cout<<"i2=";i2.display();
cout<<"i1+i2=";a.display();
cout<<"i1-i2=";b.display();
cout<<"i1*i2=";c.display();
cout<<"i1/i2=";d.display();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个关于矩阵类类型加减乘除的示例代码,使用运算符重载实现: ```c++ #include <iostream> #include <vector> using namespace std; class Matrix { public: Matrix(int rows, int cols) { this->rows = rows; this->cols = cols; data = vector<vector<double>>(rows, vector<double>(cols, 0)); } Matrix operator+(const Matrix& m) const { if (rows != m.rows || cols != m.cols) { throw invalid_argument("Cannot add matrices of different sizes"); } Matrix result(rows, cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { result.data[i][j] = data[i][j] + m.data[i][j]; } } return result; } Matrix operator-(const Matrix& m) const { if (rows != m.rows || cols != m.cols) { throw invalid_argument("Cannot subtract matrices of different sizes"); } Matrix result(rows, cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { result.data[i][j] = data[i][j] - m.data[i][j]; } } return result; } Matrix operator*(const Matrix& m) const { if (cols != m.rows) { throw invalid_argument("Cannot multiply matrices of incompatible sizes"); } Matrix result(rows, m.cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < m.cols; j++) { double sum = 0; for (int k = 0; k < cols; k++) { sum += data[i][k] * m.data[k][j]; } result.data[i][j] = sum; } } return result; } Matrix operator/(double scalar) const { Matrix result(rows, cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { result.data[i][j] = data[i][j] / scalar; } } return result; } friend ostream& operator<<(ostream& os, const Matrix& m) { for (int i = 0; i < m.rows; i++) { for (int j = 0; j < m.cols; j++) { os << m.data[i][j] << " "; } os << endl; } return os; } private: int rows; int cols; vector<vector<double>> data; }; int main() { Matrix a(2, 3); a = {{1, 2, 3}, {4, 5, 6}}; Matrix b(3, 2); b = {{7, 8}, {9, 10}, {11, 12}}; Matrix c = a * b; cout << c << endl; Matrix d = a + a; cout << d << endl; Matrix e = a - a; cout << e << endl; Matrix f = a / 2; cout << f << endl; return 0; } ``` 这个示例代码包含了矩阵的加减乘除四个运算符重载,以及一个友元函数输出矩阵。你可以在这个示例代码的基础上,自己实现一个矩阵类类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值