c++面向对象程序设计第四章课后习题

这是书上的习题,我使用的是VS2010运行编译的

原习题:

4.有两个矩阵a和b,均为两行三列。求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加。如c=a+b。

#include<iostream>
using namespace std;
class Matrix{
public:
    Matrix();
    friend Matrix operator+(Matrix &a,Matrix &b);
    void input();
    void display();
private:
    int mat[2][3];
};
//定义构造函数 
Matrix::Matrix(){
    for (int i=0;i<2;i++){
        for (int j=0;j<3;j++){
            mat[i][j]=0;
        }
    }
}
//定义重载运算符“+”函数 
Matrix operator+(Matrix &a,Matrix &b){
    Matrix c;
    for (int i=0;i<2;i++){
        for (int j=0;j<3;j++){
            c.mat[i][j]=a.mat[i][j]+b.mat[i][j];        
        }
    }
    return c;
}
void Matrix::input(){
    cout<<"input value of matrix:"<<endl;
    for (int i=0;i<2;i++){
        for (int j=0;j<3;j++){
            cin>>mat[i][j];
        }
    }
}
void Matrix::display(){
    for (int i=0;i<2;i++){
        for (int j=0;j<3;j++){
            cout<<mat[i][j]<<" ";
        }
        cout<<endl; 
    }
}

int main(){
    Matrix a,b,c;
    a.input();
    b.input();
    cout<<endl<<"Matrix a:"<<endl;
    a.display();
    cout<<endl<<"Matrix b:"<<endl;
    b.display();
    c=a+b;
    cout<<endl<<"Matrix c=Matrix a + Matrix b:"<<endl;
    c.display();
    system("pause");
    return 0;
    
}

    

运行结果:

 

 这个运行时是要自己输入一串数字进去的

 

 

改进版:有两个矩阵a和b,均为三行三列。求两个矩阵的和,差和乘积,。重载运算符“+”,”-“,”*“,使之能用于矩阵相加,相减,相乘。如c1=a+b,才=a-b,c3=a*b。

解析:这个其实跟上面的差不多,最主要的区别在矩阵的乘法上,他们不是直接将+改成乘号就好了,矩阵的乘法运算如下

 

 

 

 

#include<iostream>
using namespace std;
class Matrix{
public:
    Matrix();
  //重载友元函数 friend Matrix
operator+(Matrix &a,Matrix &b); friend Matrix operator-(Matrix &a,Matrix &b); friend Matrix operator*(Matrix &a,Matrix &b); void input(); void display(); private: int mat[3][3]; }; //定义构造函数 Matrix::Matrix(){ for (int i=0;i<3;i++){ for (int j=0;j<3;j++){ mat[i][j]=0; } } } //定义重载运算符“+”函数 Matrix operator+(Matrix &a,Matrix &b){ Matrix c; for (int i=0;i<3;i++){ for (int j=0;j<3;j++){ c.mat[i][j]=a.mat[i][j]+b.mat[i][j]; } } return c; } //定义重载运算符“-”函数 Matrix operator-(Matrix &a,Matrix &b){ Matrix c; for (int i=0;i<3;i++){ for (int j=0;j<3;j++){ c.mat[i][j]=a.mat[i][j]-b.mat[i][j]; } } return c; } //定义重载运算符“*”函数 Matrix operator*(Matrix &a,Matrix &b){ Matrix c; for (int i=0;i<3;i++){ for (int j=0;j<3;j++){ for (int k=0;k<3;k++){ c.mat[i][j]=c.mat[i][j]+a.mat[i][k]*b.mat[k][j]; } } } return c; } void Matrix::input(){ cout<<"input value of matrix:"<<endl; for (int i=0;i<3;i++){ for (int j=0;j<3;j++){ cin>>mat[i][j]; } } } void Matrix::display(){ for (int i=0;i<3;i++){ for (int j=0;j<3;j++){ cout<<mat[i][j]<<" "; } cout<<endl; } } int main(){ Matrix a,b,c1,c2,c3; a.input(); b.input(); cout<<endl<<"Matrix a:"<<endl; a.display(); cout<<endl<<"Matrix b:"<<endl; b.display(); c1=a+b; cout<<endl<<"Matrix c1=Matrix a + Matrix b:"<<endl; c1.display(); c2=a-b; cout<<endl<<"Matrix c2=Matrix a - Matrix b:"<<endl; c2.display(); c3=a*b; cout<<endl<<"Matrix c3=Matrix a * Matrix b:"<<endl; c3.display(); //system("pause"); return 0; }

运行结果:

 

 

 

 6.请编写程序,处理一个复数与一个double数相加的运算,结果存放在double型的变量中dl中,输出dl的值,再以复数形式输出此值。定义Complex(复数)类,在成员函数中包含重载类型转换运算符:

operator double(){return real;}

代码:

#include<iostream>
using namespace std;
class Complex{
public:
    Complex(){
        real=0;imag=0;
    }
    Complex(double r){
        real=r;imag=0;
    }
    Complex(double r,double i){
        real=r;imag=i;
    }
    operator double(){
        return real;
    }
    
    void display();
private:
    double real;
    double imag;
};

void Complex::display(){
    cout<<"("<<real<<","<<imag<<")"<<endl;
}
int main(){
    Complex c1(3,4),c2;
    double dl;
    dl=2.5+c1;
    cout <<"dl="<<dl<<endl;
    c2=Complex(dl);
    cout<<"c2=";
    c2.display();
    system("pause");
    return 0;
}

运行结果:

 

 

posted @ 2019-11-30 23:57  水晶bingbing  阅读(722)  评论(0编辑  收藏  举报