首页 > 其他分享 >实验四

实验四

时间:2022-11-03 17:55:49浏览次数:82  
标签:Matrix int double lines cols 实验 vectorInt

实验4.5

#pragma once
#include<iostream>
using namespace std;
class vectorInt {
    friend void output(vectorInt&v);
public:
    vectorInt(int n);
    vectorInt(int n, int value);
    vectorInt(vectorInt& v);
    ~vectorInt() { cout << "destructor called \n"; delete[]vec; }
    
    int &at(int i);
    void get_size()const;
private:
    int size;
    int* vec;
};

//构造函数接口
vectorInt::vectorInt(int n){
    size = n;
    cout << "constructor called \n";
    vec = new int[size]();
}
//构造函数接口
vectorInt::vectorInt(int n, int value) {
    cout << "constructor called"<<endl;
    size = n;
    vec = new int[size];
    for (int i = 0; i < size; i++) {
        vec[i] = value;
    }
}
//复制构造函数接口
vectorInt::vectorInt(vectorInt& v) {
    cout << "copyconstructor called \n";
    size = v.size;
    vec = new int[size];
    for (int i = 0; i < size; i++)
        vec[i] = v.vec[i];
}

//获取数组大小
void vectorInt::get_size()const{
    cout << size<<endl;
}
//返回特定位置数组元素
int &vectorInt::at(int i){
    return vec[i];
}

//输出数组元素 函数接口
void output(vectorInt& v) {
    for (int i = 0; i < v.size; i++)
        cout << v.vec[i]<<" ";
    cout << endl;
}



#include <iostream>
#include "vectorInt.hpp"
void test() {
    using namespace std;
    int n;
    cin >> n;
    vectorInt x1(n);
    for (auto i = 0; i < n; ++i)
        x1.at(i) = i * i;
    output(x1);
    vectorInt x2(n, 42);
    vectorInt x3(x2);
    output(x2);
    output(x3);
    x2.at(0) = 77;
    output(x2);
    output(x3);
}

int main() {
    test();
}

 

实验4.6

#pragma once
#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 (int i = 0; i < lines; i++)
        for (int j = 0; j < cols; j++)
            p[i*cols+j] = X.p[i*cols+j];
}
//析构函数接口
inline Matrix::~Matrix(){
    cout << "default constructor called\n";
    delete[]p;
}

//用pvalue指向的连续内存块数据按行为矩阵赋值
void Matrix::set(const double* pvalue){
    p = new double[lines * cols];
    for (int i = 0; i < lines; i++)
        for (int j = 0; j < cols; j++)
             p[i*cols+j] = *(pvalue++);
        
}

//设置矩阵第i行第j列元素值为value
void Matrix::set(int i, int j, int value){
    p[i*cols+j] = value;
}

//返回矩阵第i行第j列元素的引用
double& Matrix::at(int i, int j) {
    return p[i*cols+j];
}

//返回矩阵第i行第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;
    }
}

#include <iostream>
#include "Matrix.hpp"
using namespace std;
void test() {
    double x[9] = {1,4,5};
    Matrix m1(3, 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(3, 3);
    m2.set(x);
    m2.print();
    cout << "the first line is: " << endl;
    cout << m2.at(0, 0) << " " << m2.at(0, 1) << " " << m2.at(0, 2) << endl;
    cout << endl;
    Matrix m3(m2); 
    m3.set(1, 2, 999);
    m3.print();
}
int main() {
    test();
}

 

标签:Matrix,int,double,lines,cols,实验,vectorInt
From: https://www.cnblogs.com/xdt2/p/16855165.html

相关文章

  • Python第十章实验报告
    一、实验题目Python第十章实例和实战作业二、实验目的和要求1.熟悉Pycharm的运行环境2.学习并掌握Python文件及目录操作三、主要仪器设备联想小新air15硬件:AMDR75......
  • 实验四
    vectorint.hpp#pragmaonce#include<iostream>usingstd::cout;usingstd::endl;classvectorint{private:intsize;int*p;public:vectorint(int......
  • 实验7:基于REST API的SDN北向应用实践
    实验7:基于RESTAPI的SDN北向应用实践一、实验目的能够编写程序调用OpenDaylightRESTAPI实现特定网络功能;能够编写程序调用RyuRESTAPI实现特定网络功能。二、实验......
  • 实现实验室和寝室两台电脑文件实时同步
    考虑到白天去实验室工作,晚上又要回寝室,文件传输会很麻烦,于是寻求能够方便进行文件远程同步的方案。1.使用工具内网穿透:zerotier(全平台均可)文件同步(备份)工具:FreeFileSync(Win......
  • 实验3
     task1#include<stdio.h>#include<stdlib.h>#include<time.h>#include<Windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprint_sp......
  • P4 tutorials实验 - basic-tunnel
    P4tutorials实验-basic-tunnel基础知识隧道(tunnel):可以将IP隧道视为一对节点之间的虚拟点对点链路虚拟链路是在隧道入口处的路由器内创建的,当隧道入口处的路由器想通......
  • Openwrt 跨网实现二层实验
    由于公司项目需要跨网实现二层通信,在咨询大量的大神后他们推荐的方案是vxlan方案。于是就有了下面的实验。网络拓扑:网络环境:内网IP公网IP系统软件路由R1192.168.220.254/241......
  • Python实验报告——第10章 文件及目录操作
    实验报告【实验目的】 1.掌握Python自带的函数进行基本文件操作。2.掌握Python内置的os模块及其子模块os.path进行目录相关的操作。【实验条件】1.PC机或者远程编......
  • 实验三
      #include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函......
  • 实验七:基于REST API的SDN北向应用实践
    一、实验目的1.能够编写程序调用OpenDaylightRESTAPI实现特定网络功能;2.能够编写程序调用RyuRESTAPI实现特定网络功能。二、实验环境1.下载虚拟机软件OracleVisua......