首页 > 其他分享 >实验4 类与数组 指针

实验4 类与数组 指针

时间:2022-11-05 14:26:23浏览次数:76  
标签:const Matrix int double 矩阵 实验 数组 vectorInt 指针

实验五:

vectorInt.hpp

 1 #pragma once
 2 #include<cassert>
 3 using namespace std;
 4 
 5 class vectorInt{
 6     public:
 7         vectorInt(int n);
 8         vectorInt(int n,int value);
 9         vectorInt(const vectorInt &);
10         ~vectorInt(){
11             delete p;
12             cout << "destructor called" << endl;
13         }
14         int &at(int index);
15         int get_size(){
16             return size;
17         }
18         friend void output(vectorInt &v);
19             
20     private:
21         int size;
22         int *p;
23         
24 };
25 int &vectorInt::at(int index){
26     assert(index>=0&&index<size);
27     return p[index];
28 }
29 
30 vectorInt::vectorInt(int n):size{n}{
31         p=new int[size];
32     cout << "constructor 1 called" << endl;
33 };
34 
35 vectorInt::vectorInt(int n,int value):size{n}{
36     p=new int[size];
37     for(int i=0;i<size;i++){
38         p[i]=value;
39     }
40     cout << "constructor 2 called" << endl;
41 }
42 
43 vectorInt::vectorInt(const vectorInt &a):size{a.size}{
44     p=new int[size];
45     for(int i=0;i<size;i++){
46         p[i]=a.p[i];
47     }
48     cout << "copy constructor called" << endl;
49 }
50     
51 void output(vectorInt &v){
52     int i;
53     for(i=0;i<v.size-1;i++){
54         cout << v.p[i] << ",";
55     }
56     cout << v.p[v.size-1] << endl;
57 }

task5.cpp

 1 #include <iostream>
 2 #include "vectorInt.hpp"
 3 
 4 void test() {
 5     using namespace std;
 6 
 7     int n;
 8     cin >> n;
 9     
10     vectorInt x1(n);
11     for(auto i = 0; i < n; ++i)
12         x1.at(i) = i*i;
13 
14     output(x1);
15 
16     vectorInt x2(n, 42);
17     vectorInt x3(x2);
18 
19     output(x2);
20     output(x3);
21 
22     x2.at(0) = 77;
23 
24     output(x2);
25     output(x3);
26 }
27 
28 int main() {
29     test();
30 }

 

实验六:

Matrix.hpp

 1 #pragma once
 2 #include <iostream>
 3 using std::cout;
 4 using std::endl;
 5 class Matrix {
 6 public:
 7     Matrix(int n);                     // 构造函数,构造一个n*n的矩阵
 8     Matrix(int n, int m);              // 构造函数,构造一个n*m的矩阵
 9     Matrix(const Matrix &X);           // 复制构造函数,使用已有的矩阵X构造
10     ~Matrix();                         //析构函数
11 
12     void set(const double *pvalue);     // 用pvalue指向的连续内存块数据按行为矩阵赋值
13     void set(int i, int j, int value); //设置矩阵第i行第j列元素值为value
14     double &at(int i, int j);          //返回矩阵第i行第j列元素的引用
15     double at(int i, int j) const;     // 返回矩阵第i行第j列元素的值
16     int get_lines() const;             //返回矩阵行数
17     int get_cols() const;              //返回矩列数
18     void print() const;                // 按行打印输出矩阵
19 
20 private:
21     int lines; // 矩阵行数
22     int cols;  // 矩阵列数
23     double *p; // 指向存放矩阵数据的内存块的首地址
24 };
25 
26 Matrix::Matrix(int n):lines{n},cols{n}{
27     p=new double[n*n];
28 }
29 
30 Matrix::Matrix(int n,int m):lines{n},cols{m}{
31     p=new double[n*m];
32 }
33 
34 Matrix::Matrix(const Matrix &x):lines{x.lines},cols{x.cols}{
35     p=new double[x.lines*x.cols];
36     for(int i=0;i<x.lines;i++){
37         for(int j=0;j<x.cols;j++){
38             p[i*x.cols+j]=x.p[i*x.cols+j];
39         }
40     }
41 }
42 
43 Matrix::~Matrix(){
44     delete p;
45 }
46 
47 void Matrix::set(const double *pvalue){
48     for(int line=0;line<lines;line++){
49         for(int col=0;col<cols;col++){
50             p[line*cols+col]=pvalue[line*cols+col];
51         }    
52         }
53 }
54 
55 void Matrix::set(int i,int j,int value){
56     p[i*cols+j]=value;
57 }
58 
59 double &Matrix::at(int i,int j){
60     return p[i*cols+j];
61 }
62 
63 double Matrix::at(int i,int j)const{
64     return p[i*cols+j];
65 }
66 
67 int Matrix::get_lines()const{
68     return lines;
69 }
70 
71 int Matrix::get_cols()const{
72     return cols;
73 }
74 
75 void Matrix::print() const{
76     int i,j;
77     for(i=0;i<lines;i++){
78         for(j=0;j<cols-1;j++){
79             cout << p[i*cols+j] <<",";
80         }
81         cout << p[i*cols+cols-1] << endl;
82     }
83 }

task6.cpp

 1 #include <iostream>
 2 #include "matrix.hpp"
 3 
 4 void test() {
 5     using namespace std;
 6 
 7     double x[] = {1, 2, 3, 4, 5, 6};
 8 
 9     Matrix m1(3, 2);    
10     m1.set(x);          
11     m1.print();        
12     cout << "the first line is: " << endl;
13     cout << m1.at(0, 0) << " " << m1.at(0, 1) << endl;  
14     cout << endl;
15 
16     Matrix m2(2, 3);
17     m2.set(x);
18     m2.print();
19     cout << "the first line is: " << endl;
20     cout << m2.at(0, 1) << " " << m2.at(0, 2) << " " << m2.at(0, 3) << endl;
21     cout << endl;
22 
23     Matrix m3(m2);      
24     m3.set(0, 0, 888);  
25     m3.print();
26 }
27 
28 int main() {
29     test();
30 }

 

标签:const,Matrix,int,double,矩阵,实验,数组,vectorInt,指针
From: https://www.cnblogs.com/ljhakuna/p/16860087.html

相关文章

  • 实验三
    task1.c#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprint_spa......
  • 数组扁平化,函数柯理化
    数组扁平化将一个多维数组编程一个一维数函数柯理化将一个函数的两个参数当中两个函数一个参数来进行执行函数的作用:将多个拥有相同功能代码封装在一起,让我们写......
  • 实验四
    task5://point.hpp1#pragmaonce2classpoint{3public:4point(){}5point(intxx){x=xx;}6intreturn_x(){returnx;}7private:8i......
  • 【数据库系统概论】实验五 SQL数据库安全控制
    一、实验目的1.掌握SQLServer数据库用户基本操作2.掌握SQLServer数据库授权及回收权限的方法二、实验内容创建登录用户st1,st2使st1,st2成为stu_db的合法用户EXECsp_grant......
  • 搭建华为VRP实验平台WinPcap-Virtualbox-wireshark-eNSP
    WinPcap-Virtualbox-wireshark-eNSPWireshark的安装顺序可以放在eNSP前,也可以放在eNSP之后(如果安装的是最新版的eNSP,Wireshark的安装必须放在eNSP之前)注意事项:①装wireshar......
  • Object Pascal与Delphi中的指针
    ObjectPascal的指针1.https://blog.csdn.net/peixiaobin_blog/article/details/43191171ObjectPascal中,动态分配内存的函数是GetMem(),与之对应的释放函数为FreeMem()(......
  • 实验3 函数应用编程
     实验任务1#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprin......
  • 实验四 类与数组、指针
    实验任务五vectorInt.hpp1#include<iostream>2#include<cassert>3usingnamespacestd;45classvectorInt{67public:8vectorInt(int......
  • C# Linq将DataTable中的某列转换成数组或者List
    //获取到的数据DataTablepicDt=GetPdmPoroductModelPictureData(productModelCode);//将productCode列转数组string[]arrPic=picDt.AsEnumerable().Select(d......
  • 实验4
    matrix.hpp1#pragmaonce2#include<iostream>3usingstd::cout;4usingstd::endl;5classMatrix{6public:7Matrix(intn);//构造函数,构造一......