实验3

vector_int.hpp

#ifndef VECTOR_INT_HPP
#define VECTOR_INT_HPP
#include<iostream>
#include<cassert>
using namespace std;
class Vector_int{
public:
    Vector_int(int n, int m = 0);
    Vector_int(const Vector_int& a);
    int& at(int index);
    ~Vector_int();
private:
    int size;
    int* p;
};

Vector_int::Vector_int(int n,int m)
{
    cout << "constructor called." << endl;
    size = n;
    p = new int[size];
    for (int i = 0; i < size; i++)
    {
        p[i] = m;
    }
}

Vector_int::Vector_int(const Vector_int& a):size(a.size)
{
    cout << "copying constructor called." << endl;
    p = new int[size];
    for (auto i = 0; i < size; ++i)
    {
        p[i] = a.p[i];
    }
}

int& Vector_int::at(int index)
{
    assert(index >= 0 && index < size);
    return p[index];
}

Vector_int::~Vector_int()
{
    cout << "deleting..." << endl;
    delete[] p;
}

#endif

task4.cpp

#include"vector_int.hpp"
#include<iostream>
using namespace std;
#define n 8
int main()
{
    Vector_int x(n, 5);
    cout << "x数组的值为:" << endl;
    for (int i = 0; i < n; i++)
    {
        cout << x.at(i) << " ";
    }
    cout << endl;
    Vector_int y(x);
    y.at(0) = 10;
    cout << "y数组的值为:" << endl;
    for (int i = 0; i < n; i++)
    {
        cout << y.at(i) << " ";
    }
    cout << endl;
}

 

Matrix.hpp

#ifndef MATRIX_HPP
#define MATRIX_HPP
#include<iostream>
using namespace std;
class Matrix {
public:
    Matrix(int n);
    Matrix(int n, int m);
    Matrix(const Matrix& x);
    ~Matrix();
    void set(const double* pvalue);
    void set(int i, int j, int value);
    double& at(int i, int j);
    double at(int i, int j) const;
    int get_lines() const;
    int get_cols() const;
    void print() const;
private:
    int lines;
    int cols;
    double* p;
};

Matrix::Matrix(int n):lines(n),cols(n)
{
    p = new double[n*n];
}

Matrix::Matrix(int n, int m) : lines(n), cols(m)
{
    p = new double[n*m];
}

Matrix::Matrix(const Matrix& x):lines(x.lines),cols(x.cols)
{
    p = new double[lines * cols];
    for (auto i = 0; i < lines * cols; i++)
    {
        p[i] = x.p[i];
    }
}

Matrix::~Matrix()
{
    delete[] p;
}

void Matrix::set(const double* pvalue)
{
    for (int i = 0; i < lines * cols; i++)
    {
        p[i] = pvalue[i];
    }
}

void Matrix::set(int i, int j, int value)
{
    p[i * cols + j] = value;
}

double& Matrix::at(int i, int j)
{
    return p[i * cols + j];
}

double Matrix::at(int i, int j) const
{
    return p[i * cols + j];
}

int Matrix::get_lines() const
{
    return lines;
}

int Matrix::get_cols() const
{
    return cols;
}

void Matrix::print() const
{
    for (int i = 0; i < lines; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            cout << p[i * cols + j] << " ";
        }
        cout << endl;
    }
}
#endif

task5.cpp

#include <iostream>
#include "Matrix.hpp"

int main()
{
    using namespace std;

    double x[] = { 9,8,7,6,5,4 };

    Matrix m1(2,3);    
    m1.set(x);          
    m1.print();         
    cout << "the first line is: " << endl;
    cout << m1.at(0, 0) << " " << m1.at(0, 1) <<" " << m1.at(0, 2) << endl;
    cout << endl;

    Matrix m2(2, 3);
    m2.set(x);
    m2.print();
    cout << "the first line is: " << endl;
    cout << m2.at(0, 0) << " " << m2.at(0, 1)  << endl;
    cout << endl;

    Matrix m3(m2);
    m3.set(0, 0, 999);
    m3.print();
}

 

posted @ 2021-11-06 16:30  天问非攻  阅读(30)  评论(3编辑  收藏  举报