通过C++实现加减乘除

 通过C++实现加减乘除

#include<iostream>
using namespace std;
#include<string>
class hui 
{
public:
	int jisuan(string kk)
	{
		if (kk == "+")
		{
			return m_num1 + m_num2;
		}
		else if(kk=="-")
		{
			return m_num1 - m_num2;
		}
		else if(kk=="*")
		{
			return m_num1 * m_num2;
		}
		else if (kk == "/")
		{
			return m_num1 / m_num2;
		}

	
	}
	int m_num1;
	int m_num2;
};

void test01()
{
	hui y1;
	y1.m_num1 = 10;
	y1.m_num2 = 10;
	
	cout << y1.m_num1 << "+" << y1.m_num2 << "=" << y1.jisuan("+") << endl;
	cout << y1.m_num1 << "-" << y1.m_num2 << "=" << y1.jisuan("-") << endl;
	cout << y1.m_num1 << "*" << y1.m_num2 << "=" << y1.jisuan("*") << endl;
	cout << y1.m_num1 << "/" << y1.m_num2 << "=" << y1.jisuan("/") << endl;

	
}



int main()
{
	test01();
	system("pause");
	return 0;
}

 

 

  • 6
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来为您介绍一下C++中如何用类来实现矩阵的加减乘除操作。 首先,我们需要定义一个矩阵类,可以包含矩阵的行列数和矩阵元素。下面是一个矩阵类的示例代码: ```c++ class Matrix { private: int rows; // 矩阵的行数 int cols; // 矩阵的列数 vector<vector<double>> data; // 矩阵元素 public: Matrix(int r, int c) : rows(r), cols(c), data(r, vector<double>(c, 0)) {}; // 构造函数,初始化矩阵元素为0 // 获取矩阵的行数和列数 int getRows() const { return rows; } int getCols() const { return cols; } // 获取矩阵中某个位置的元素 double getElem(int i, int j) const { return data[i][j]; } void setElem(int i, int j, double val) { data[i][j] = val; } // 矩阵加法 Matrix operator+(const Matrix& other) const { if (rows != other.rows || cols != other.cols) { throw "两个矩阵的行列数不匹配"; } Matrix res(rows, cols); for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { res.data[i][j] = data[i][j] + other.data[i][j]; } } return res; } // 矩阵减法 Matrix operator-(const Matrix& other) const { if (rows != other.rows || cols != other.cols) { throw "两个矩阵的行列数不匹配"; } Matrix res(rows, cols); for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { res.data[i][j] = data[i][j] - other.data[i][j]; } } return res; } // 矩阵乘法 Matrix operator*(const Matrix& other) const { if (cols != other.rows) { throw "两个矩阵的行列数不匹配"; } Matrix res(rows, other.cols); for (int i = 0; i < rows; ++i) { for (int j = 0; j < other.cols; ++j) { for (int k = 0; k < cols; ++k) { res.data[i][j] += data[i][k] * other.data[k][j]; } } } return res; } // 矩阵除法 Matrix operator/(const Matrix& other) const { if (other.rows != other.cols || other.rows != 1) { throw "除数必须是1阶方阵"; } double div = other.data[0][0]; if (div == 0) { throw "除数不能为0"; } Matrix res(rows, cols); for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { res.data[i][j] = data[i][j] / div; } } return res; } }; ``` 在上面的代码中,我们定义了一个矩阵类Matrix,包含了矩阵的行列数和元素,以及加减乘除四种运算符重载。其中,矩阵的加减乘法比较基本,需要注意的是除法时,除数必须是1阶方阵,且不能为0。 接下来,我们可以通过以下方式来使用矩阵类: ```c++ // 定义一个2*3的矩阵a Matrix a(2, 3); a.setElem(0, 0, 1); a.setElem(0, 1, 2); a.setElem(0, 2, 3); a.setElem(1, 0, 4); a.setElem(1, 1, 5); a.setElem(1, 2, 6); // 定义一个3*2的矩阵b Matrix b(3, 2); b.setElem(0, 0, 7); b.setElem(0, 1, 8); b.setElem(1, 0, 9); b.setElem(1, 1, 10); b.setElem(2, 0, 11); b.setElem(2, 1, 12); // 矩阵加法 Matrix c = a + b; // 矩阵减法 Matrix d = a - b; // 矩阵乘法 Matrix e = a * b; // 矩阵除法 Matrix f = a / Matrix(1, 1, 2); // 将矩阵a中的每个元素除以2 ``` 以上就是用类来实现矩阵加减乘除的代码示例。希望能够对您有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值