首页 > 其他分享 >实验三

实验三

时间:2023-11-03 18:56:14浏览次数:27  
标签:index const Point int 实验 vectorPoint size

任务一

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 for(auto &t: v)
 8 t.show();
 9 }
10 void test() {
11 int n;
12 cin >> n;
13 vector<Point> x(n);
14 cout << "x" << endl;
15 output(x);
16 vector<Point> y(x);
17 cout << "\nx " << endl;
18 output(y);
19 cout << "\nupdate x" << endl;
20 x.at(0).move(30, 50); 
21 x.push_back(Point(2, 2)); 
22 cout << "\nx total:" << endl;
23 output(x);
24 cout << "\ny total: " << endl;
25 output(y);
26 }
27 int main() {
28 test();
29 }
View Code Point.cpp
 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

 

  任务二 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 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 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

 

任务三 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 Point.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 Point[size]} {
21 for(auto i = 0; i < size; ++i)
22 ptr[i] = vp.ptr[i];
23 }
24 vectorPoint::~vectorPoint() {
25 delete[] ptr;
26 }
27 int vectorPoint::get_size() const {
28 return size;
29 }
30 Point& vectorPoint::at(int index) {
31 assert(index >= 0 && index < size);
32 return ptr[index];
33 }
34 Point& vectorPoint::at(int index) const {
35 assert(index >= 0 && index < size);
36 return ptr[index];
37 }
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

 

  任务四 task4_1.cpp
 1 #include <iostream>
 2 using namespace std;
 3 
 4 // 函数声明
 5 void swap1(int &rx, int &ry);    // 引用作为形参
 6 void swap2(int *px, int *py);    // 指针作为形参
 7 void print(int x, int y);        // 普通变量作为形参
 8 
 9 // 测试代码
10 void test() {
11     int x = 3, y = 4;
12 
13     print(x, y);
14     swap1(x, y);        // 函数调用,注意:引用作为形参时,实参形式
15     print(x, y);
16 
17     cout << endl;
18 
19     x = 3, y = 4;
20     print(x, y);
21     swap2(&x, &y);        // 函数调用,注意:指针作为形参时,实参形式
22     print(x, y);
23 }
24 
25 int main() {
26     test();
27 }
28 
29 // 函数定义:交换两个变量(引用变量作为形参)
30 void swap1(int &rx, int &ry) {
31     int t;
32 
33     t = rx;  rx = ry;  ry = t;
34 }
35 
36 // 函数定义:交换两个变量(指针变量作为形参)
37 void swap2(int *px, int *py) {
38     int t;
39 
40     t = *px;  *px = *py;  *py = t;
41 }
42 
43 // 函数定义:输出两个变量(普通变量作为形参)
44 void print(int x, int y) {
45     std::cout << "x = " << x << ", y = " << y << "\n";
46 }
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     // 以十六进制形式输出普通变量a, 引用变量ra,指针变量pa的地址
15     cout << "&a = " << hex << &a << endl;
16     cout << "&ra = " << hex << &ra << endl;
17     cout << "&pa = " << hex << &pa << "\n\n";
18     
19     // 输出普通变量a, 引用变量ra,指针变量pa的值
20     cout << "a = " << a << endl;
21     cout << "ra = " << a << endl;
22     cout << "pa = " << hex << pa << endl;
23     
24     // 输出指针变量pa指向的变量的值
25     cout << "*pa = " << *pa << "\n\n";
26 
27     // 输出普通变量a,引用变量ra, 指针变量pa的类型信息
28     cout << "type a: " << typeid(a).name() << endl;
29     cout << "type ra: " << typeid(ra).name() << endl;
30     cout << "type pa: " << typeid(pa).name() << endl;
31 }
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) // i是普通类型
16         i *= i;
17 }
18 
19 template<typename T>
20 void square2(T &x) {
21     for(auto &i: x)  // i是引用类型
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

 

任务五 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(int 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);    // 构造动态int数组对象x1,包含n个元素,不对元素初始化
20     for(int i = 0; i < n; ++i)
21         x1.at(i) = i*i;
22     cout << "vectorInt对象x1: ";
23     output(x1);
24 
25     vectorInt x2(n, 42); // 构造动态int数组对象x1,包含n个元素,每个元素初始值为42
26     cout << "vectorInt对象x2: ";
27     output(x2);
28     vectorInt x3(x2);    // 使用x2构造x3
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 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 m);
 9     vectorInt(const vectorInt & vc);
10     int get_size() const;
11     ~vectorInt();
12     int& at(int index);
13     int& at(int index) const;
14 private:
15     int size;
16     int *ptr;
17 };
18 vectorInt::vectorInt(int n)
19 {
20     size=n;
21     ptr =new int[n];
22     cout<<"constructor vectorInt(int n) called."<<endl;
23 }
24 vectorInt::vectorInt(int n,int m)
25 {
26     size=n;ptr= new int[n];
27     for(int i=0;i<size;i++)
28         ptr[i]=m;
29     cout<<"constructor vectorInt(int n,int value) called"<<endl;
30     
31 } 
32 vectorInt::vectorInt(const vectorInt & vc)
33 {
34     size=vc.size;
35     ptr=new int[size];
36     for(int i = 0; i < size; ++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 size;
48 }
49 int& vectorInt::at(int index) 
50 {
51     assert(index >= 0 && index < size); // 宏,在测试模式下工作。如果不满足条件,则程序终止
52     return ptr[index];
53 }
54 int& vectorInt::at(int index) const 
55 {
56     assert(index >= 0 && index < size);
57     return ptr[index];
58 }
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 // 输出一个矩阵对象中索引为index对应的行的所有元素值
10 void output(const Matrix &m, int index) {
11     for(int j = 0; j < m.get_cols(); ++j)
12         cout << m.at(index, j) << ", ";
13     cout << "\b\b \n";
14 }
15 
16 void test() {
17 
18 
19     double x[N1*N2] = {1, 2, 3, 4, 5, 6};
20 
21     Matrix m1(N1, N2);      // 创建一个N1×N2矩阵
22     m1.set(x);              // 用一维数组x的值按行为矩阵m1赋值
23     cout << "矩阵对象m1: " << endl;
24     m1.print();             // 打印矩阵m1的值
25     cout << "矩阵对象m1第0行是: " << endl;
26     output(m1, 0);
27     cout << endl;
28 
29     Matrix m2(N2, N1);
30     m2.set(x);
31     cout << "矩阵对象m2: " << endl;
32     m2.print();         
33     cout << "矩阵对象m2第0行是: " << endl;
34     output(m2, 0);
35     cout << endl;
36 
37     Matrix m3(m2);      // 用矩阵m2构造新的矩阵m3
38     m3.set(0, 0, 999);  // 讲矩阵对象m2索引(0,0)元素设为999
39     cout << "矩阵对象m3:" << endl;
40     m3.print();
41     cout << endl;
42 
43     Matrix m4(2);       // 创建一个2*2矩阵对象
44     m4.set(x);          // 用一维数组x的值按行为矩阵m4赋值
45     cout << "矩阵对象m4:" << endl;
46     m4.print();
47 }
48 
49 int main() {
50     test();
51 }
View Code matrix.hpp
 1 #include <iostream>
 2 #include <cassert>
 3 using std::cout;
 4 using std::endl;
 5 // 类Matrix的声明
 6 class Matrix {
 7 public:
 8     Matrix(int n, int m); // 构造函数,构造一个n*m的矩阵
 9     Matrix(int n); // 构造函数,构造一个n*n的矩阵
10     Matrix(const Matrix &x); // 复制构造函数, 使用已有的矩阵X构造
11     ~Matrix();
12     void set(const double *pvalue); // 用pvalue指向的连续内存块数据按行为矩阵赋值
13     void set(int i, int j, double value); // 设置矩阵对象索引(i,j)的元素值为value
14     double& at(int i, int j) const; // 返回矩阵对象索引(i,j)的元素引用
15     double& at(int i, int j); // 返回矩阵对象索引(i,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 Matrix::Matrix(int n,int m) 
25 {
26     lines=n;cols=m;
27     ptr= new double[n*m];
28 }
29 Matrix::Matrix(int n)
30 {
31     lines=n;cols=n;
32     ptr=new double[lines*cols];
33 }
34 Matrix::Matrix(const Matrix &x)
35 {
36     lines=x.lines;
37     cols=x.cols;
38     ptr=new double[cols*lines];
39     for(int i=0;i<lines;i++)
40         for(int j=0;j<cols;j++)
41             ptr[i*cols+j]=x.ptr[i*cols+j];    
42 }
43 Matrix::~Matrix()
44 {
45     delete[] ptr;
46 }
47 void Matrix::set(const double *pvaule)
48 {
49     for(int i=0;i<lines;i++)
50         for(int j=0;j<cols;j++)
51             ptr[i*cols+j]=pvaule[i*cols+j];
52 }
53 void Matrix::set(int i,int j,double value)
54 {
55     ptr[i*cols+j]=value;
56 }
57 double& Matrix::at(int i,int j)
58 {
59     return ptr[i*cols+j];
60 }
61 double& Matrix::at(int i,int j) const
62 {
63     return ptr[i*cols+j];
64 }
65 int Matrix::get_lines() const
66 {
67     return lines;
68 }
69 int Matrix::get_cols() const
70 {
71     return cols;
72 }
73 void Matrix::print() const
74 {
75     for(int i=0;i<lines;i++)
76     {
77         for(int j=0;j<cols;j++)
78         {
79             cout<<ptr[i*cols+j]<<"  ";
80         }
81     cout<<endl;
82     }
83 }
View Code

 

 

标签:index,const,Point,int,实验,vectorPoint,size
From: https://www.cnblogs.com/dmsx/p/17808207.html

相关文章

  • 实验3—C语言函数应用编程
    1、实验任务1源代码1#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN806voidprint_text(intline,intcol,chartext[]);//函数声明7voidprint_spaces(intn);//函数声明8voidprint_b......
  • ensp实验,大一必学网工基础,数通HCIA内容,详细知识点集合
    1:交换机通信:两个pc端在一个网段内,不用单独配置交换机信息,不用设置网关地址,都能ping通2:路由器通信:Pc的网关地址是路由器的ip地址。路由器配置接口和地址Pc3pingpc4即可3:静态路由:静态路由配置好PC的ip地址、子网掩码、网关和路由器接口的地址信息。在R7路由器输入:[R7]iproute-stat......
  • 国产教学实验箱_嵌入式实验箱:3-4 mp3音频解码实验
    一、实验目的了解MP3文件的结构,掌握MP3音频解码的原理,并实现基于SYSBIOS系统,将MP3格式的音频解码为PCM格式。二、实验原理1、MP3音频格式MP3是一种音频压缩技术,其全称是动态影像专家压缩标准音频层面3(MovingPictureExpertsGroupAudioLayerIII),简称为MP3。它被设计用来大幅度地......
  • 实验三
     #include<stdio.h>intfunc(intn,intm);intmain(){intn,m;while(scanf("%d%d",&n,&m)!=EOF)printf("n=%d,m=%d,ans=%d\n",n,m,func(n,m));return0;}intfunc(intn,intm){if(n<m)......
  • JVM 调优实验
    JVM调优理论前言关于性能优化Therealproblemisthatprogrammershavespentfartoomuchtimeworryingaboutefficiencyinthewrongplacesandatthewrongtimes;prematureoptimizationistherootofallevil(oratleastmostofit)inprogramming.—D......
  • Linux 实验
    知识补充框架设计Linux操作可以分为本地操作和网络操作,操作对象主要是文件和目录,根据上述分类设计框架如下本地操作基础指令:系统一般内置目录目录切换:cd增删改查mkdirrmdir权限管理:chmodchmod命令的具体用法如下:chmod[选项]模式文件其中,模式是......
  • 操作系统实验——进程管理的算法实现
    前言笔者在大学下属的事业单位上班,最近去帮着带下操作系统的实验课,这里随手水点参考代码,欢迎各位领导老师莅临指正实验目标编写一个简单的进程调度器实验内容进程控制块(PCB)的定义与管理进程调度算法的实现进程创建、销毁和切换给定一批进程对比3-4种调度算法的时间(自选算......
  • 【单片机】I/O口实验
    要求:拨动开关,让所亮小灯位置左移或者右移#include<STC8.H>#include<intrins.h>voiddelay(){ inti,j; for(i=0;i<1000;i++){ for(j=0;j<1000;j++); }}charmove_left(charvalue,intnumber){ value=_crol_(value,number); returnvalue; }charmov......
  • 0. 实验工具准备以及后续实验目的
    该系列课程需要使用到RISC-V(极简指令集)版本的四个工具:1.QEMU5.1+2.GDB8.3+3.GCC4.Binutils下面逐个介绍这四个工具。GCCGNUCompilerCollections,GNU系统中的编译器套件。GDBGNUDebugger,GNU系统中的调试器。QEMU开源的硬件虚拟化仿真器(Emulator)。是一个托管的虚......
  • 实验3 类与数组、指针
    实验任务1Point.hpp源码1#pragmaonce23#include<iostream>4usingstd::cout;5usingstd::endl;67classPoint{8public:9Point(intx0=0,inty0=0);10~Point()=default;1112intget_x()const;13intget_y()c......