[期中][Simple Inheritance] Vector & Matrix

Problem Description
Matrix can be stored in a one-dimensional vector in a row-first way, inheriting from the Vector class. According to Vector.h and Matrix.h, complete the implementation of Vector class and Matrix class.
Function description: Vector::getModule() gets the modulus length of the vector, Matrix::Symmetric() judges whether the matrix is a symmetric matrix.
矩阵可以以行优先的方式存储在一维向量中, 继承于Vector类。根据Vector.h和Matrix.h完成Vector类和Matrix类的实现。
函数说明:Vector::getModule()求得向量的模长, Matrix::Symmetric()判断矩阵是不是对称矩阵。

HINT
向量的模长就是向量的长度(或称模)。

输入
向量长度n
向量n个元素的值

矩阵行row 矩阵列col
矩阵元素

输出
向量模长
矩阵元素个数
矩阵是否为对称矩阵

[Sample Input1]

3
0 0 0

3 3
1 0 0
0 1 0
0 0 1

[Sample Output1]

Vector Module: 0
Total Elements in Matrix: 9
Symmetric matrix: 1

[Sample Input2]

3
3 4 0

3 3
3 0 1
0 4 0
1 0 5

[Sample Output2]

Vector Module: 5
Total Elements in Matrix: 9
Symmetric matrix: 1

主程序(C/C++)


#ifndef VECTOR_MATRIX_H
#define VECTOR_MATRIX_H
#include<iostream>
using namespace std;
class Vector{
public:
    Vector();
    Vector(int dim, const int* elements_);
    ~Vector();
    
    int getDimension() const;
    int *getElements() const;
    double getModule() const;                             //求向量的模长
    
private:
    int dimension;                                  //向量的维度
    int *elements;                                  //存储向量元素的数组
};
class Matrix: public Vector{
public:
    Matrix();
    Matrix(int row, int col, const int* elements_);      //矩阵的行列以及矩阵元素(行优先方式存储在一维数组中)
    bool Symmetric() const;                              //判断是否为对称矩阵
private:
    int row;                                       //矩阵的行
};
#endif


#include "source.cpp"

using namespace std;

int main()
{
    int vector_dimension;
    cin >> vector_dimension;

    int* vector_elements = new int[vector_dimension];
    for(int i=0;i<vector_dimension;i++)
        cin >> vector_elements[i];
    
    int row, col;
    cin >> row >> col;
    int* matrix_elements = new int[row*col];
    for(int i=0;i<row;i++)
    {
        for(int j=0;j<col;j++)
            cin >> matrix_elements[i*row+j];
    }

    Vector v(row,vector_elements);
    cout<<"Vector Module: "<<v.getModule()<<endl;

    Matrix m(row,col,matrix_elements);
    cout<< "Total Elements in Matrix: " << m.getDimension()<<endl;
    cout<< "Symmetric matrix: "<<m.Symmetric()<<endl;
    
    delete []vector_elements;
    delete []matrix_elements;
    return 0;
}

答案

#include<cmath>
//#include"vector_matrix.h"
Vector::Vector(){
    dimension = 0;
    elements = NULL;
}
Vector::Vector(int dim, const int* elements_) {
    dimension = dim;
    elements = new int[dim];
    for(int i=0;i<dim;i++)
        elements[i] = elements_[i];
}
int Vector::getDimension() const {
    return dimension;
}
int* Vector::getElements() const {
    return elements;
}
double Vector::getModule() const {
    int whole = 0;
    for(int i=0;i<dimension;i++)
        whole += elements[i]*elements[i];
    return sqrt(1.0*whole);
} 
Vector::~Vector()
{
    if(elements)
        delete []elements;
}

Matrix::Matrix(){
    row = 0;
}
Matrix::Matrix(int row_, int col_, const int* elements_):Vector(row_*col_, elements_),row(row_){}
bool Matrix::Symmetric() const {
    int col = getDimension()/row;
    if(row!=col) {
        return false;
    }
    int *elements = getElements();
    for(int i=0;i<row;i++)
    {
        for(int j=0;j<i;j++)
        {
            if(elements[i*row+j] != elements[j*row+i])
            {
                return false;
            }
                
        }
    }
    return true;
}

随机测试文本产生程序 (C)

#include <stdio.h>

int main() {
    printf("3\n\
0 0 0\n\
\n\
3 3\n\
1 0 0\n\
0 1 0\n\
0 0 1");
}
支持 makedown语法