1.建立一个复数类 complex,使其具有加法、减法、乘法和除法的功能,并编写
主函数,实现复数的加法、减法、乘法和除法,显示输出结果。
#include <iostream>
using namespace std;
class Complex {
private:
double real;
double imag;
public:
Complex(double r, double i) : real(r), imag(i) {}
Complex operator+(const Complex& c) {
return Complex(real + c.real, imag + c.imag);
}
Complex operator-(const Complex& c) {
return Complex(real - c.real, imag - c.imag);
}
Complex operator*(const Complex& c) {
return Complex((real * c.real) - (imag * c.imag), (real * c.imag) + (imag * c.real));
}
Complex operator/(const Complex& c) {
double denom = c.real * c.real + c.imag * c.imag;
double realPart = (real * c.real + imag * c.imag) / denom;
double imagPart = (imag * c.real - real * c.imag) / denom;
return Complex(realPart, imagPart);
}
friend ostream& operator<<(ostream& output, const Complex& c) {
output << c.real << " + " << c.imag << "i";
return output;
}
};
int main() {
int x1, x2, y1, y2;
cout << "请输入复数实部:";
cin >> x1 >> x2;
cout << "请输入复数虚部:";
cin >> y1 >> y2;
Complex c1(x1, x2);
Complex c2(y1, y2);
cout << "=======================" << endl;
cout << "c1 + c2 = " << c1 + c2 << endl;
cout << "c1 - c2 = " << c1 - c2 << endl;
cout << "c1 * c2 = " << c1 * c2 << endl;
cout << "c1 / c2 = " << c1 / c2 << endl;
return 0;
}
2.写一个矩阵类 Matrix,使其至少具有加法、乘法、转置、显示等功能,编写主
函数,实现矩阵的加法、乘法、转置,并显示结果.
#include <iostream>
using namespace std;
class Matrix {
public:
int** data;
int rows;
int cols;
Matrix(int m, int n) : rows(m), cols(n) {
data = new int* [rows];
for (int i = 0; i < rows; ++i) {
data[i] = new int[cols];
}
}
Matrix operator+(const Matrix& other) {
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] + other.data[i][j];
}
}
return result;
}
Matrix operator*(const Matrix& other) {
Matrix result(rows, other.cols);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < other.cols; ++j) {
int sum = 0;
for (int k = 0; k < cols; ++k) {
sum += data[i][k] * other.data[k][j];
}
result.data[i][j] = sum;
}
}
return result;
}
Matrix transpose() {
Matrix result(cols, rows);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
result.data[j][i] = data[i][j];
}
}
return result;
}
void display() {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
std::cout << data[i][j] << " ";
}
std::cout << std::endl;
}
}
~Matrix() {
for (int i = 0; i < rows; ++i) {
delete[] data[i];
}
delete[] data;
}
};
int main() {
int i1, i2, j1, j2;
cout << "第一个矩阵大小(行 x 列):";
cin >> i1 >> j1;
Matrix m1(i1, j1);
cout << "该矩阵为:";
for (int i = 0; i < i1; i++)
{
for (int j = 0; j < j1; j++)
{
cin>>m1.data[i][j];
}
}
cout << "第二个矩阵大小(行 x 列):";
cin >> i2 >> j2;
Matrix m2(i2, j2);
cout << "该矩阵为:";
for (int i = 0; i < i2; i++)
{
for (int j = 0; j < j2; j++)
{
cin >> m2.data[i][j];
}
}
cout << "========================" << endl;
cout << "第一个矩阵为:" << endl;
m1.display();
cout << "========================" << endl;
cout << "第二个矩阵为:" << endl;
m2.display();
cout << "========================" << endl;
Matrix sum = m1 + m2;
std::cout << "进行加法操作:" << std::endl;
sum.display();
cout << "========================" << endl;
Matrix product = m1 * m2;
std::cout << "进行乘法操作:" << std::endl;
product.display();
cout << "========================" << endl;
Matrix transpose = m1.transpose();
std::cout << "进行转置操作:" << std::endl;
transpose.display();
return 0;
}