实验3 类与数组、指针

发布时间 2023-11-06 01:03:44作者: 春满城

实验任务1

point.hpp

 1 #pragma once
 2 #include <iostream>
 3 using std::cout;
 4 using std::endl;
 5 class Point
 6 {
 7 public:
 8 Point(int x0 = 0, int y0 = 0);
 9 ~Point() = default;
10 int get_x() const;
11 int get_y() const;
12 void show() const;
13 void move(int new_x, int new_y);
14 private:
15 int x, y;
16 };
17 Point::Point(int x0, int y0): x{x0}, y{y0} 
18 {
19 }
20 int Point::get_x() const
21 {
22 return x;
23 }
24 int Point::get_y() const
25 {
26 return y;
27 }
28 void Point::show() const
29 {
30 cout << "(" << x << ", " << y << ")" << endl;
31 }
32 void Point::move(int new_x, int new_y)
33 {
34 x = new_x;
35 y = new_y;
36 }
View Code

task1.cpp

 1 #include <iostream>
 2 #include "point.hpp"
 3 #include <vector>
 4 using std::vector;
 5 using std::cin;
 6 void output(const vector<Point> &v) 
 7 {
 8 for(auto &t: v)
 9 t.show();
10 }
11 void test()
12 {
13 int n;
14 cin >> n;
15 vector<Point> x(n);
16 cout << "x" << endl;
17 output(x);
18 vector<Point> y(x);
19 cout << "\nx " << endl;
20 output(y);
21 cout << "\nupdate x" << endl;
22 x.at(0).move(30, 50);
23 x.push_back(Point(2, 2));
24 cout << "\nx total:" << endl;
25 output(x);
26 cout << "\ny total: " << endl;
27 output(y);
28 }
29 int main()
30 {
31 test();
32 }
View Code

实验任务2

point.hpp

 1 #pragma once
 2 #include <iostream>
 3 using std::cout;
 4 using std::endl;
 5 class Point {
 6 public:
 7 Point(int x0 = 0, int y0 = 0);
 8 ~Point() = default;
 9 int get_x() const;
10 int get_y() const;
11 void show() const;
12 void move(int new_x, int new_y);
13 private:
14 int x, y;
15 };
16 Point::Point(int x0, int y0): x{x0}, y{y0} {
17 }
18 int Point::get_x() const {
19 return x;
20 }
21 int Point::get_y() const {
22 return y;
23 }
24 void Point::show() const {
25 cout << "(" << x << ", " << y << ")" << endl;
26 }
27 void Point::move(int new_x, int new_y) {
28 x = new_x;
29 y = new_y;
30 }
View Code
vectorPoint.hpp
 1 #pragma once
 2 #include "point.hpp"
 3 #include <cassert>
 4 #include <iostream>
 5 class vectorPoint{
 6 public:
 7 vectorPoint(int n);
 8 ~vectorPoint();
 9 int get_size() const;
10 Point& at(int index);
11 Point& at(int index) const;
12 private:
13 int size;
14 Point *ptr;
15 };
16 vectorPoint::vectorPoint(int n) : size{n} {
17 ptr = new Point[n];
18 }
19 vectorPoint::~vectorPoint() {
20 delete[] ptr;
21 }
22 int vectorPoint::get_size() const {
23 return size;
24 }
25 Point& vectorPoint::at(int index) {
26 assert(index >= 0 && index < size);
27 return ptr[index];
28 }
29 Point& vectorPoint::at(int index) const {
30 assert(index >= 0 && index < size);
31 return ptr[index];
32 }
View Code

task2.cpp

 1 #include "vectorPoint.hpp"
 2 #include <iostream>
 3 void output(const vectorPoint &v) {
 4 for(auto i = 0; i < v.get_size(); ++i)
 5 v.at(i).show();
 6 }
 7 void test() {
 8 using namespace std;
 9 int n;
10 cout << "vectorPoint yuansugeshu: ";
11 cin >> n;
12 vectorPoint x(n);
13 cout << "x information: " << endl;
14 output(x);
15 vectorPoint y(x);
16 cout << "\ny information: " << endl;
17 output(y);
18 cout << "\nupdate x information......" << endl;
19 x.at(0).move(30, 50);
20 x.at(1).move(-1, -1);
21 cout << "x information: " << endl;
22 output(x);
23 cout << "\ny information: " << endl;
24 output(y);
25 }
26 int main() {
27 test();
28 }
View Code

实验任务3

point.hpp

 1 #pragma once
 2 
 3 #include <iostream>
 4 using std::cout;
 5 using std::endl;
 6 
 7 class Point {
 8 public:
 9     Point(int x0 = 0, int y0 = 0);
10     ~Point() = default;
11 
12     int get_x() const;
13     int get_y() const;
14     void show() const;
15     void move(int new_x, int new_y);
16 
17 private:
18     int x, y;
19 };
20 
21 Point::Point(int x0, int y0): x{x0}, y{y0} {   
22 }
23 
24 int Point::get_x() const {
25     return x;
26 }
27 
28 int Point::get_y() const {
29     return y;
30 }
31 
32 void Point::show() const {
33     cout << "(" << x << ", " << y << ")" << endl;
34 }
35 
36 void Point::move(int new_x, int new_y) {
37     x = new_x;
38     y = new_y;
39 }
View Code
vectorPoint.hpp
 1 #pragma once
 2 #include "point.hpp"
 3 #include <cassert>
 4 #include <iostream>
 5 class vectorPoint{
 6 public:
 7 vectorPoint(int n);
 8 vectorPoint(const vectorPoint &vp);
 9 ~vectorPoint();
10 int get_size() const;
11 Point& at(int index);
12 Point& at(int index) const;
13 private:
14 int size;
15 Point *ptr;
16 };
17 vectorPoint::vectorPoint(int n) : size{n} {
18 ptr = new Point[n];
19 }
20 vectorPoint::vectorPoint(const vectorPoint &vp): size{vp.size}, ptr{new
21 Point[size]} {
22 for(auto i = 0; i < size; ++i)
23 ptr[i] = vp.ptr[i];
24 }
25 vectorPoint::~vectorPoint() {
26 delete[] ptr;
27 }
28 int vectorPoint::get_size() const {
29 return size;
30 }
31 Point& vectorPoint::at(int index) {
32 assert(index >= 0 && index < size);
33 return ptr[index];
34 }
35 Point& vectorPoint::at(int index) const {
36 assert(index >= 0 && index < size);
37 return ptr[index];
38 }
View Code

task3.cpp

 1 #include "vectorPoint.hpp"
 2 #include <iostream>
 3 void output(const vectorPoint &v) {
 4 for(auto i = 0; i < v.get_size(); ++i)
 5 v.at(i).show();
 6 }
 7 void test() {
 8 using namespace std;
 9 int n;
10 cout << "input vectorPoint how much number: ";
11 cin >> n;
12 vectorPoint x(n);
13 cout << "x information: " << endl;
14 output(x);
15 vectorPoint y(x);
16 cout << "\ny information: " << endl;
17 output(y);
18 cout << "\nupdate x information......" << endl;
19 x.at(0).move(30, 50);
20 x.at(1).move(-1, -1);
21 cout << "x information: " << endl;
22 output(x);
23 cout << "\ny information: " << endl;
24 output(y);
25 }
26 int main() {
27 test();
28 }
View Code

实验任务4

task4_1.cpp
 1 #include <iostream>
 2 using namespace std;
 3 
 4 void swap1(int &rx, int &ry);
 5 void swap2(int *px, int *py);
 6 void print(int x, int y);
 7 
 8 void test() {
 9     int x = 3, y = 4;
10 
11     print(x, y);
12     swap1(x, y);
13     print(x, y);
14 
15     cout << endl;
16 
17     x = 3, y = 4;
18     print(x, y);
19     swap2(&x, &y);
20     print(x, y);
21 }
22 
23 int main() {
24     test();
25 }
26 
27 void swap1(int &rx, int &ry) {
28     int t;
29 
30     t = rx;  rx = ry;  ry = t;
31 }
32 
33 void swap2(int *px, int *py) {
34     int t;
35 
36     t = *px;  *px = *py;  *py = t;
37 }
38 
39 void print(int x, int y) {
40     std::cout << "x = " << x << ", y = " << y << "\n";
41 }
View Code

 

task4_2.cpp
 1 #include <iostream>
 2 #include <typeinfo>
 3 using namespace std;
 4 
 5 int main() {
 6     int a;
 7     
 8     int &ra = a;
 9     ra = 4;
10 
11     int *pa = &a;
12     *pa = 5;
13 
14     cout << "&a = " << hex << &a << endl;
15     cout << "&ra = " << hex << &ra << endl;
16     cout << "&pa = " << hex << &pa << "\n\n";
17     
18     cout << "a = " << a << endl;
19     cout << "ra = " << a << endl;
20     cout << "pa = " << hex << pa << endl;
21     
22     cout << "*pa = " << *pa << "\n\n";
23 
24     cout << "type a: " << typeid(a).name() << endl;
25     cout << "type ra: " << typeid(ra).name() << endl;
26     cout << "type pa: " << typeid(pa).name() << endl;
27 }
View Code

 

task4_3.cpp
 1 #include <iostream>
 2 #include <vector>
 3 
 4 using namespace std;
 5 
 6 template<typename T>
 7 void output(const T &x) {
 8     for(auto i: x)  
 9         std::cout << i << ", ";
10     std::cout << "\b\b \n";
11 }
12 
13 template<typename T>
14 void square1(T &x) {
15     for(auto i: x) 
16         i *= i;
17 }
18 
19 template<typename T>
20 void square2(T &x) {
21     for(auto &i: x) 
22         i *= i;
23 }
24 
25 void test1() {
26     vector<int> x {1, 2, 3, 4, 5};
27 
28     cout << "动态int型数组对象x内的元素值: ";
29     output(x);
30 
31     cout << "调用函数square1()......" << endl;
32     square1(x);
33 
34     cout << "动态int型数组对象x内的元素值: ";
35     output(x);
36 }
37 
38 void test2() {
39     vector<int> x {1, 2, 3, 4, 5};
40 
41     cout << "动态int型数组对象x内的元素值: ";
42     output(x);
43 
44     cout << "调用函数square2()......" << endl;
45     square2(x);
46 
47     cout << "动态int型数组对象x内的元素值: ";
48     output(x);
49 }
50 
51 int main() {
52     cout << "测试1: " << endl;
53     test1();
54 
55     cout << "\n测试2: " << endl;
56     test2();
57 }
View Code

实验任务5

vectorInt.hpp

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

task5.cpp

 1 #include "vectorInt.hpp"
 2 #include <iostream>
 3 
 4 using std::cout;
 5 using std::cin;
 6 using std::endl;
 7 
 8 void output(const vectorInt &vi) {
 9     for(auto i = 0; i < vi.get_size(); ++i)
10         cout << vi.at(i) << ", ";
11     cout << "\b\b \n";
12 }
13 
14 void test() {
15     int n;
16     cout << "输入vectorInt对象中元素个数: ";
17     cin >> n;
18 
19     vectorInt x1(n);
20     for(auto i = 0; i < n; ++i)
21         x1.at(i) = i*i;
22     cout << "vectorInt对象x1: ";
23     output(x1);
24 
25     vectorInt x2(n, 42);
26     cout << "vectorInt对象x2: ";
27     output(x2);
28     vectorInt x3(x2);
29     cout << "vectorInt对象x3: ";
30     output(x3);
31 
32     cout << "更新vectorInt对象x2......\n";
33     x2.at(0) = 77;
34     x2.at(1) = -999;
35 
36     cout << "vectorInt对象x2: ";
37     output(x2);
38     cout << "vectorInt对象x3: ";
39     output(x3);
40 }
41 
42 int main() {
43     test();
44 }
View Code

实验任务6

matrix.hpp

 1 #include <iostream>
 2 #include <cassert>
 3 using std::cout;
 4 using std::endl;
 5 
 6 class Matrix {
 7 public:
 8     Matrix(int n, int m);
 9     Matrix(int n); 
10     Matrix(const Matrix &x); 
11     ~Matrix();
12     void set(const double *pvalue); 
13     void set(int i, int j, double value); 
14     double& at(int i, int j) const;
15     double& at(int i, int j); 
16     int get_lines() const; 
17     int get_cols() const;
18     void print() const; 
19 private:
20     int lines;
21     int cols; 
22     double *ptr;
23 };
24 
25 
26 
27 
28 Matrix::Matrix(int n,int m)
29 {
30     lines=n;cols=m;
31     ptr= new double[n*m];
32 }
33 Matrix::Matrix(int n)
34 {
35     lines=n;cols=n;
36     ptr=new double[lines*cols];
37 }
38 Matrix::Matrix(const Matrix &x)
39 {
40     lines=x.lines;
41     cols=x.cols;
42     ptr=new double[cols*lines];
43     for(int i=0;i<lines;i++)
44         for(int j=0;j<cols;j++)
45             ptr[i*cols+j]=x.ptr[i*cols+j];
46 }
47 Matrix::~Matrix()
48 {
49     delete[] ptr;
50 }
51 void Matrix::set(const double *pvaule)
52 {
53     for(int i=0;i<lines;i++)
54         for(int j=0;j<cols;j++)
55             ptr[i*cols+j]=pvaule[i*cols+j];
56 }
57 void Matrix::set(int i,int j,double value)
58 {
59     ptr[i*cols+j]=value;
60 }
61 double& Matrix::at(int i,int j)
62 {
63     return ptr[i*cols+j];
64 }
65 double& Matrix::at(int i,int j) const
66 {
67     return ptr[i*cols+j];
68 }
69 int Matrix::get_lines() const
70 {
71     return lines;
72 }
73 int Matrix::get_cols() const
74 {
75     return cols;
76 }
77 void Matrix::print() const
78 {
79     for(int i=0;i<lines;i++)
80     {
81         for(int j=0;j<cols;j++)
82         {
83             cout<<ptr[i*cols+j]<<"  ";
84         }
85     cout<<endl;
86     }
87 }
View Code

task6.cpp

 1 #include <iostream>
 2 #include "matrix.hpp"
 3 
 4 using namespace std;
 5 
 6 const int N1 = 3;
 7 const int N2 = 2;
 8 
 9 void output(const Matrix &m, int index) {
10     for(int j = 0; j < m.get_cols(); ++j)
11         cout << m.at(index, j) << ", ";
12     cout << "\b\b \n";
13 }
14 
15 void test() {
16 
17 
18     double x[N1*N2] = {1, 2, 3, 4, 5, 6};
19 
20     Matrix m1(N1, N2);
21     m1.set(x);
22     cout << "矩阵对象m1: " << endl;
23     m1.print();
24     cout << "矩阵对象m1第0行是: " << endl;
25     output(m1, 0);
26     cout << endl;
27 
28     Matrix m2(N2, N1);
29     m2.set(x);
30     cout << "矩阵对象m2: " << endl;
31     m2.print();
32     cout << "矩阵对象m2第0行是: " << endl;
33     output(m2, 0);
34     cout << endl;
35 
36     Matrix m3(m2);
37     m3.set(0, 0, 999);
38     cout << "矩阵对象m3:" << endl;
39     m3.print();
40     cout << endl;
41 
42     Matrix m4(2);
43     m4.set(x);
44     cout << "矩阵对象m4:" << endl;
45     m4.print();
46 }
47 
48 int main() {
49     test();
50 }
View Code