
(1)复数运算
#include
using namespace std;
/********* Begin *********/
class Complex
{
//复数类的声明
public:
Complex(float r,float i)
:real(r),image(i)
{ }
void Print();
Complex();
Complex& operator += (const Complex&);
// float real () const { return re; }
// float imag () const { return im; }
Complex operator + (Complex &obj);
Complex operator - (Complex &obj);
Complex operator * (Complex &obj);
Complex operator / (Complex &obj);
private:
float real;
float image;
};
Complex::Complex()
{ }
Complex Complex::operator + (Complex &obj)
{
return Complex ( real+ obj.real,
image+ obj.image);
}
Complex Complex::operator - (Complex &obj)
{
return Complex ( real- obj.real,
image- obj.image);
}
Complex Complex::operator * (Complex &obj)
{
Complex c;
c.real=real*obj.real-image*obj.image;
c.image=image*obj.real+obj.image*real;
return c;
}
Complex Complex::operator / (Complex &obj)
{
Complex c;
c.real=real*obj.real+image*obj.image;
c.image=image*obj.real-obj.image*real;
float A=obj.real*obj.real+obj.image*obj.image;
c.real=c.real/A;
c.image=c.image/A;
return c;
}
void Complex::Print()
{
if(Complex::image<0)
{
cout < } else cout < } //复数类的定义 /********* End *********/ (2)学生信息转换 #include #include #include using namespace std; /********* Begin *********/ // 前置声明 Teacher 类 class Teacher; class Student { //学生类的声明 public: Student(int num,string nam,string se); void Print(); int get_num() { return number; } string get_name() { return name; } string get_sex() { return sex; } private: int number; string name; string sex; }; //学生类的定义 class Teacher { //教师类的声明 public: Teacher() {}; Teacher(Student &); Teacher(int num,string nam,string se); void Print(); private: int number; string name; string sex; }; //教师类的定义 Student::Student(int num,string nam,string se) { number=num; name=nam; sex=se; } void Student::Print() { cout<<"学生:"< } Teacher::Teacher(int num,string nam,string se) { number=num; name=nam; sex=se; } void Teacher::Print() { cout<<"教师:"< } Teacher::Teacher(Student &stu) { number=stu.get_num(); name=stu.get_name(); sex=stu.get_sex(); } /********* End *********/ (3)矩阵运算 #include #include using namespace std; /********* Begin *********/ class Matrix { //矩阵类的声明 public: Matrix(int r,int c); Matrix(); void Fill(int value); void Set(int r,int c,int value); int Get(int r,int c); void Print(); friend Matrix operator + (Matrix& m1,Matrix &m2); friend Matrix operator - (Matrix& m1,Matrix &m2); friend Matrix operator * (Matrix& m1,Matrix &m2); private: int row, column; //成员变量行和列 int pt[10][10]; }; //矩阵类的定义 Matrix::Matrix() { } Matrix::Matrix(int r,int c) { row = r; column = c; } void Matrix::Print(){ //输出矩阵 for(int i = 0; i < row; i++) { for (int j = 0; j < column; j++) cout << this->pt[i][j] << " "; cout << endl; } } void Matrix::Fill(int value) { for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) this->pt[i][j] =value; } } void Matrix::Set(int r,int c,int value) { this->pt[r][c] =value; } int Matrix::Get(int r,int c) { return this->pt[r-1][c-1]; } Matrix operator+(Matrix& m1,Matrix &m2) { //实现矩阵加法 Matrix temp(m1.row, m1.column); for (int i = 0; i < m1.row; i++){ for (int j = 0; j < m1.column; j++) temp.pt[i][j] = m1.pt[i][j] + m2.pt[i][j]; } return temp; } Matrix operator-(Matrix& m1,Matrix &m2) { //实现矩阵减法 Matrix temp(m1.row, m1.column); for (int i = 0; i < m1.row; i++){ for (int j = 0; j < m1.column; j++) temp.pt[i][j] = m1.pt[i][j] - m2.pt[i][j]; } return temp; } Matrix operator*(Matrix& m1,Matrix &m2) { //实现矩阵乘法 Matrix temp(m1.row, m2.column); for (int i = 0; i < m1.row; i++) { for (int j = 0; j < m2.column; j++) { long sum=0; for (int k = 0; k < m1.column; k++) { sum += m1.pt[i][k] * m2.pt[k][j]; //累加第i行与第j列的元素乘积 } temp.pt[i][j] = sum; //赋给i,j元素 } } return temp; } /********* End *********/ 欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)