首页 > 其他分享 >实验四

实验四

时间:2022-11-05 13:56:07浏览次数:54  
标签:const Matrix int lines cols 实验 vectorInt

task5:

//point.hpp

1 #pragma once
2 class point {
3 public:
4     point() {}
5     point(int xx) {x=xx; }
6     int return_x(){ return x; }
7 private:
8     int x;
9 };

//vectorInt.hpp

 1 #include<iostream>
 2 #include<string>
 3 #include"point.hpp"
 4 using namespace std;
 5 class vectorInt {
 6 public:
 7     point &at(int i);//获得下标为i的数组元素
 8     int get_size() { return n; }
 9     vectorInt(){};//默认构造函数
10     vectorInt(int nn);//带一个参数的构造函数
11     vectorInt(int nn, int pp);//带两个参数的构造函数
12     vectorInt(const vectorInt &v);//复制构造函数
13     ~vectorInt() { 
14         cout << "destructor called." << endl; 
15         delete[]p;//析构时需要释放资源 
16     }
17     friend void output(vectorInt &v);
18 private:
19     point *p;//指向数组首地址
20     int n;//数组长度
21 };
22 vectorInt::vectorInt(int nn) {
23     n = nn;
24     p = new point[nn];
25     cout << "constructor 1 called." << endl;
26 }//带一个参数的构造函数ok
27 vectorInt::vectorInt(int nn, int xx) {
28     n = nn;
29     p = new point[nn]; 
30     for (int j = 0; j < nn; j++) { 
31         p[j] = xx;
32     }
33     cout << "constructor 2 called." << endl;
34 }//带两个参数的构造函数ok
35 vectorInt::vectorInt(const vectorInt &v) {
36     n = v.n;
37     p = new point[n];
38     for (int j = 0; j < n; j++) {
39         p[j] = v.p[j];//先给一块新的内存空间再赋值即为深复制。
40     }
41     cout << "copy constructor called." << endl;
42 }//复制构造函数ok
43 void output(vectorInt&v) {
44     for (int i = 0; i < v.n; i++) {
45         cout<<(v.at(i).return_x())<< ",";//出现错误C3867    “point::return_x”:非标准语法请使用 "&" 来创建指向成员的指针    是因为return_x后忘记括号
46 
47     }
48     cout << "\b " << endl;
49 }
50 point &vectorInt::at(int i) {
51     return p[i];
52 }

//task5.cpp

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

运行结果:

task6:

 

//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     void set(const double *pvalue);     // 用pvalue指向的连续内存块数据按行为矩阵赋值
12     void set(int i, int j, int value); //设置矩阵第i行第j列元素值为value
13     double &at(int i, int j);          //返回矩阵第i行第j列元素的引用
14     double at(int i, int j) const;     // 返回矩阵第i行第j列元素的值
15     int get_lines() const;             //返回矩阵行数
16     int get_cols() const;              //返回矩列数
17     void print() const;                // 按行打印输出矩阵
18 private:
19     int lines; // 矩阵行数
20     int cols;  // 矩阵列数
21     double *p; // 指向存放矩阵数据的内存块的首地址
22 };
23 // 类Matrix的实现
24 Matrix::Matrix(int n) {
25     p = new double[n*n];
26     lines = n;
27     cols = n;
28 }
29 Matrix::Matrix(int n, int m) {
30     p = new double[n*m];
31     lines = n;
32     cols = m;
33 }
34 Matrix::Matrix(const Matrix&m) {
35     p = new double[m.lines*m.cols];
36     lines = m.lines;
37     cols = m.cols;
38     for (int i = 0; i < lines*cols; i++) {
39         p[i] = m.p[i];
40     }
41 }
42 Matrix::~Matrix(){}
43 void Matrix::set(const double * pvalue) {
44     for (int i = 0; i < lines*cols; i++) {
45         p[i] =pvalue[i];
46     }
47 }
48 void Matrix::set(int i, int j, int value) {
49     p[cols*i + j] = value;
50 }
51 double& Matrix::at(int i, int j) {
52     return p[cols*i+j];
53 }
54 double Matrix::at(int i, int j) const {
55     return p[cols*i + j ];
56 }
57 int Matrix::get_cols() const { return cols; }
58 int Matrix::get_lines() const { return lines; }
59 void Matrix::print() const {
60     for (int i = 0; i < lines; i++) {
61         for (int j = 0; j < cols; j++) {
62             cout << at(i,j) << ",";
63         }
64         cout <<"\b "<< endl;
65     }
66 }

//task6.cpp

 1 #include <iostream>
 2 #include "matrix.hpp"
 3 void test() {
 4     using namespace std;
 5     double x[] = { 1, 2, 3, 4, 5, 6 };
 6     Matrix m1(3, 2);    // 创建一个3×2的矩阵
 7     m1.set(x);          // 用一维数组x的值按行为矩阵m1赋值
 8     m1.print();         // 打印矩阵m1的值
 9     cout << "the first line is: " << endl;
10     cout << m1.at(0, 0) << " " << m1.at(0, 1) << endl;  // 输出矩阵m1第1行两个元素的值
11     cout << endl;
12 
13     Matrix m2(2, 3);
14     m2.set(x);
15     m2.print();
16     cout << "the first line is: " << endl;
17     cout << m2.at(0, 0) << " " << m2.at(0, 1) << " " << m2.at(0, 2) << endl;
18     cout << endl;
19 
20     Matrix m3(m2);      // 用矩阵m2构造新的矩阵m3
21     m3.set(0, 0, 999);  // 将矩阵m3第0行第0列元素值设为999
22     m3.print();
23 }
24 
25 int main() {
26     test();
27 }

运行结果:

改变数据后:

 

标签:const,Matrix,int,lines,cols,实验,vectorInt
From: https://www.cnblogs.com/jww12/p/16851641.html

相关文章

  • 【数据库系统概论】实验五 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......
  • 实验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......
  • 实验4
    matrix.hpp1#pragmaonce2#include<iostream>3usingstd::cout;4usingstd::endl;5classMatrix{6public:7Matrix(intn);//构造函数,构造一......
  • 实验3 C语言控制语句应用编程
    #include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函数声明voidp......
  • Java MySQL Spring Struts Hibernate 动漫论坛的设计与实现文档 毕设实训实验
    JavaMySQLSpringStrutsHibernate动漫论坛的设计与实现文档实训实验能满足学习和二次开发可以作为熟悉Java的学习,作为老师阶段性学习的一个成功检验不再是单调的理解......
  • 实验四
    实验四vectorInt.hpp#pragmaonce#include<iostream>usingnamespacestd;classvectorInt{private:intsize;int*p;public:vectorInt(intn):si......
  • 实验3 函数应用编辑
    task1.c#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprin......
  • Java swing 连连看小游戏 开发小系统 项目源代码 实训实验毕设
    Javaswing连连看小游戏开发小系统项目源代码实训实验能满足学习和二次开发可以作为初学者熟悉Java的学习,作为老师阶段性学习的一个成功检验不再是单调的理解老师空泛......