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

实验3 类与数组、指针

时间:2023-11-05 13:12:17浏览次数:35  
标签:const 数组 Point int void 实验 vectorPoint include 指针

实验任务1

point.hpp

#pragma once

#include <iostream>
using std::cout;
using std::endl;

class Point {
public:
    Point(int x0 = 0, int y0 = 0);
    ~Point() = default;

    int get_x() const;
    int get_y() const;
    void show() const;
    void move(int new_x, int new_y);

private:
    int x, y;
};

Point::Point(int x0, int y0): x{x0}, y{y0} {   
}

int Point::get_x() const {
    return x;
}

int Point::get_y() const {
    return y;
}

void Point::show() const {
    cout << "(" << x << ", " << y << ")" << endl;
}

void Point::move(int new_x, int new_y) {
    x = new_x;
    y = new_y;
}
View Code

task1.cpp

#include <iostream>
#include "point.hpp"
#include <vector>

using std::vector;
using std::cin;

// 输出vector<Point>对象内所有点的坐标
void output(const vector<Point> &v) {
    for(auto &t: v)
        t.show();
}

void test() {
    int n;
    cout << "输入动态Point数组类对象中元素个数: ";
    cin >> n;

    vector<Point> x(n);
    cout << "x对象中所有点坐标信息: " << endl;
    output(x); 

    vector<Point> y(x);  // 基于vector<Point>对象x构建对象y
    cout << "\nx对象中所有点坐标信息: " << endl;
    output(y);
    
    cout << "\n更新x对象......" << endl;
    x.at(0).move(30, 50);       // 更新对象x内索引为0的点对象坐标
    x.push_back(Point(2, 2));   // 向x对象末尾添加一个点对象

    cout << "\nx对象中所有点坐标信息: " << endl;
    output(x);          
    cout << "\ny对象中所有点坐标信息: " << endl; 
    output(y);           
}

int main() {
    test();
}
View Code

运行结果

实验任务2

point.hpp

#pragma once

#include <iostream>
using std::cout;
using std::endl;

class Point {
public:
    Point(int x0 = 0, int y0 = 0);
    ~Point() = default;

    int get_x() const;
    int get_y() const;
    void show() const;
    void move(int new_x, int new_y);

private:
    int x, y;
};

Point::Point(int x0, int y0): x{x0}, y{y0} {   
}

int Point::get_x() const {
    return x;
}

int Point::get_y() const {
    return y;
}

void Point::show() const {
    cout << "(" << x << ", " << y << ")" << endl;
}

void Point::move(int new_x, int new_y) {
    x = new_x;
    y = new_y;
}
View Code

vectorPoint.hpp

#pragma once

#include "point.hpp"
#include <cassert>
#include <iostream>

class vectorPoint{
public:
    vectorPoint(int n);
    ~vectorPoint();
    
    int get_size() const;           // 获得当前动态数组内元素个数
    Point& at(int index);           // 返回下标为index的元素引用
    Point& at(int index) const;     // 返回下标为index的元素const引用

private:
    int size; // 动态数组的大小
    Point *ptr;
};

vectorPoint::vectorPoint(int n) : size{n} {
    ptr = new Point[n];
}

vectorPoint::~vectorPoint() {
    delete[] ptr;
}

int vectorPoint::get_size() const {
    return size;
}

Point& vectorPoint::at(int index) {
    assert(index >= 0 && index < size);  // 宏,在测试模式下工作。如果不满足条件,则程序终止
    return ptr[index];
}

Point& vectorPoint::at(int index) const {
    assert(index >= 0 && index < size);  
    return ptr[index];
}
View Code

task2.cpp

#include "vectorPoint.hpp"
#include <iostream>

// 输出vectorPoint对象内的所有数据
void output(const vectorPoint &v) {
    for(auto i = 0; i < v.get_size(); ++i)
        v.at(i).show();
}

// 测试vectorPoint类:构造对象、复制构造对象
void test() {
    using namespace std;

    int n;
    cout << "输入vectorPoint对象中元素个数: ";
    cin >> n;

    vectorPoint x(n);
    cout << "x对象中所有点坐标信息: " << endl;
    output(x); 

    vectorPoint y(x);
    cout << "\ny对象中所有点坐标信息: " << endl;
    output(y); 

    cout << "\n更新x对象中点坐标信息......" << endl;
    x.at(0).move(30, 50);
    x.at(1).move(-1, -1);

    cout << "x对象中所有点坐标信息: " << endl;
    output(x); 

    cout << "\ny对象中所有点坐标信息: " << endl;
    output(y); 
}

int main() {
    test();
}
View Code

运行结果

实验任务3

point.hpp

#pragma once

#include <iostream>
using std::cout;
using std::endl;

class Point {
public:
    Point(int x0 = 0, int y0 = 0);
    ~Point() = default;

    int get_x() const;
    int get_y() const;
    void show() const;
    void move(int new_x, int new_y);

private:
    int x, y;
};

Point::Point(int x0, int y0): x{x0}, y{y0} {   
}

int Point::get_x() const {
    return x;
}

int Point::get_y() const {
    return y;
}

void Point::show() const {
    cout << "(" << x << ", " << y << ")" << endl;
}

void Point::move(int new_x, int new_y) {
    x = new_x;
    y = new_y;
}
View Code

vectorPoint.hpp

#pragma once

#include "point.hpp"
#include <cassert>
#include <iostream>

class vectorPoint{
public:
    vectorPoint(int n);
    vectorPoint(const vectorPoint &vp);
    ~vectorPoint();
    
    int get_size() const;           // 获得当前动态数组内元素个数
    Point& at(int index);           // 返回下标为index的元素引用
    Point& at(int index) const;     // 返回下标为index的元素const引用

private:
    int size; // 动态数组的大小
    Point *ptr;
};

vectorPoint::vectorPoint(int n) : size{n} {
    ptr = new Point[n];
}

vectorPoint::vectorPoint(const vectorPoint &vp): size{vp.size}, ptr{new Point[size]} {
    for(auto i = 0; i < size; ++i)
        ptr[i] = vp.ptr[i];
}

vectorPoint::~vectorPoint() {
    delete[] ptr;
}

int vectorPoint::get_size() const {
    return size;
}

Point& vectorPoint::at(int index) {
    assert(index >= 0 && index < size);  // 宏,在测试模式下工作。如果不满足条件,则程序终止
    return ptr[index];
}

Point& vectorPoint::at(int index) const {
    assert(index >= 0 && index < size);  
    return ptr[index];
}
View Code

task3.cpp

#include "vectorPoint.hpp"
#include <iostream>

// 输出vectorPoint对象内的所有数据
void output(const vectorPoint &v) {
    for(auto i = 0; i < v.get_size(); ++i)
        v.at(i).show();
}

// 测试vectorPoint类:构造对象、复制构造对象
void test() {
    using namespace std;

    int n;
    cout << "输入vectorPoint对象中元素个数: ";
    cin >> n;

    vectorPoint x(n);
    cout << "x对象中所有点坐标信息: " << endl;
    output(x); 

    vectorPoint y(x);
    cout << "\ny对象中所有点坐标信息: " << endl;
    output(y); 

    cout << "\n更新x对象中点坐标信息......" << endl;
    x.at(0).move(30, 50);
    x.at(1).move(-1, -1);

    cout << "x对象中所有点坐标信息: " << endl;
    output(x); 

    cout << "\ny对象中所有点坐标信息: " << endl;
    output(y); 
}

int main() {
    test();
}
View Code

运行结果

实验任务4

task4_1.cpp

#include <iostream>
using namespace std;

// 函数声明
void swap1(int &rx, int &ry);    // 引用作为形参
void swap2(int *px, int *py);    // 指针作为形参
void print(int x, int y);        // 普通变量作为形参

// 测试代码
void test() {
    int x = 3, y = 4;

    print(x, y);
    swap1(x, y);        // 函数调用,注意:引用作为形参时,实参形式
    print(x, y);

    cout << endl;

    x = 3, y = 4;
    print(x, y);
    swap2(&x, &y);        // 函数调用,注意:指针作为形参时,实参形式
    print(x, y);
}

int main() {
    test();
}

// 函数定义:交换两个变量(引用变量作为形参)
void swap1(int &rx, int &ry) {
    int t;

    t = rx;  rx = ry;  ry = t;
}

// 函数定义:交换两个变量(指针变量作为形参)
void swap2(int *px, int *py) {
    int t;

    t = *px;  *px = *py;  *py = t;
}

// 函数定义:输出两个变量(普通变量作为形参)
void print(int x, int y) {
    std::cout << "x = " << x << ", y = " << y << "\n";
}
View Code

task4_2.cpp

#include <iostream>
#include <typeinfo>
using namespace std;

int main() {
    int a;
    
    int &ra = a;
    ra = 4;

    int *pa = &a;
    *pa = 5;

    // 以十六进制形式输出普通变量a, 引用变量ra,指针变量pa的地址
    cout << "&a = " << hex << &a << endl;
    cout << "&ra = " << hex << &ra << endl;
    cout << "&pa = " << hex << &pa << "\n\n";
    
    // 输出普通变量a, 引用变量ra,指针变量pa的值
    cout << "a = " << a << endl;
    cout << "ra = " << a << endl;
    cout << "pa = " << hex << pa << endl;
    
    // 输出指针变量pa指向的变量的值
    cout << "*pa = " << *pa << "\n\n";

    // 输出普通变量a,引用变量ra, 指针变量pa的类型信息
    cout << "type a: " << typeid(a).name() << endl;
    cout << "type ra: " << typeid(ra).name() << endl;
    cout << "type pa: " << typeid(pa).name() << endl;
}
View Code

task4_3.cpp

#include <iostream>
#include <vector>

using namespace std;

template<typename T>
void output(const T &x) {
    for(auto i: x)  
        std::cout << i << ", ";
    std::cout << "\b\b \n";
}

template<typename T>
void square1(T &x) {
    for(auto i: x) // i是普通类型
        i *= i;
}

template<typename T>
void square2(T &x) {
    for(auto &i: x)  // i是引用类型
        i *= i;
}

void test1() {
    vector<int> x {1, 2, 3, 4, 5};

    cout << "动态int型数组对象x内的元素值: ";
    output(x);

    cout << "调用函数square1()......" << endl;
    square1(x);

    cout << "动态int型数组对象x内的元素值: ";
    output(x);
}

void test2() {
    vector<int> x {1, 2, 3, 4, 5};

    cout << "动态int型数组对象x内的元素值: ";
    output(x);

    cout << "调用函数square2()......" << endl;
    square2(x);

    cout << "动态int型数组对象x内的元素值: ";
    output(x);
}

int main() {
    cout << "测试1: " << endl;
    test1();

    cout << "\n测试2: " << endl;
    test2();
}
View Code

实验任务5

#ifndef VECTORINT_HPP
#define VECTORINT_HPP

#include <iostream>

class vectorInt {
private:
    int *data;
    int size;

public:
    vectorInt(int s);
    vectorInt(int s, int value);
    vectorInt(const vectorInt &other); 
    ~vectorInt();

    int& at(int index);
    int get_size() const;

    friend void output(const vectorInt &vi);
};

#endif 
vectorint.hpp
#include "vectorInt.hpp"
#include <iostream>

void output(const vectorInt &vi);

void test() {
    int n;
    std::cout << "输入vectorInt对象中元素个数: ";
    std::cin >> n;

    vectorInt x1(n);    // 构造动态int数组对象x1,包含n个元素,不对元素初始化
    for (int i = 0; i < n; ++i)
        x1.at(i) = i * i;
    std::cout << "vectorInt对象x1: ";
    output(x1);

    vectorInt x2(n, 42); // 构造动态int数组对象x1,包含n个元素,每个元素初始值为42
    std::cout << "vectorInt对象x2: ";
    output(x2);
    vectorInt x3(x2);    // 使用x2构造x3
    std::cout << "vectorInt对象x3: ";
    output(x3);

    std::cout << "更新vectorInt对象x2......\n";
    x2.at(0) = 77;
    x2.at(1) = -999;

    std::cout << "vectorInt对象x2: ";
    output(x2);
    std::cout << "vectorInt对象x3: ";
    output(x3);
}

int main() {
    test();
}
task5.cpp
#include "vectorInt.hpp"

vectorInt::vectorInt(int s) : size(s) {
    data = new int[size];
    std::cout << "Constructed vectorInt with size " << size << std::endl;
}

vectorInt::vectorInt(int s, int value) : size(s) {
    data = new int[size];
    for (int i = 0; i < size; ++i) {
        data[i] = value;
    }
    std::cout << "Constructed vectorInt with size " << size << " and initialized values to " << value << std::endl;
}

vectorInt::vectorInt(const vectorInt &other) : size(other.size) {
    data = new int[size];
    for (int i = 0; i < size; ++i) {
        data[i] = other.data[i];
    }
    std::cout << "Constructed vectorInt by deep copying another vectorInt" << std::endl;
}

vectorInt::~vectorInt() {
    delete[] data;
    std::cout << "Destroyed vectorInt" << std::endl;
}

int& vectorInt::at(int index) {
    if (index < 0 || index >= size) {
        throw std::out_of_range("Index out of range");
    }
    return data[index];
}

int vectorInt::get_size() const {
    return size;
}

void output(const vectorInt &vi) {
    for (int i = 0; i < vi.get_size(); ++i)
        std::cout << vi.at(i) << ", ";
    std::cout << "\b\b \n";
}
View Code

运行结果

实验任务6

#include <iostream>
#include "matrix.hpp"

// ��Matrix�Ķ���
Matrix::Matrix(int n, int m) : lines(n), cols(m) {
    ptr = new double[lines * cols](); // Initialize with zeros
}

Matrix::Matrix(int n) : lines(n), cols(n) {
    ptr = new double[lines * cols](); // Initialize with zeros
}

Matrix::Matrix(const Matrix &x) : lines(x.lines), cols(x.cols) {
    ptr = new double[lines * cols];
    for (int i = 0; i < lines * cols; ++i) {
        ptr[i] = x.ptr[i];
    }
}

Matrix::~Matrix() {
    delete[] ptr;
}

void Matrix::set(const double *pvalue) {
    for (int i = 0; i < lines * cols; ++i) {
        ptr[i] = pvalue[i];
    }
}

void Matrix::set(int i, int j, double value) {
    assert(i >= 0 && i < lines && j >= 0 && j < cols);
    ptr[i * cols + j] = value;
}

double& Matrix::at(int i, int j) const {
    assert(i >= 0 && i < lines && j >= 0 && j < cols);
    return ptr[i * cols + j];
}

double& Matrix::at(int i, int j) {
    assert(i >= 0 && i < lines && j >= 0 && j < cols);
    return ptr[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) {
            std::cout << ptr[i * cols + j] << " ";
        }
        std::cout << std::endl;
    }
}

// Test code (task6.cpp remains unchanged)
// ...
matrix.cpp
#pragma once

#include <iostream>
#include <cassert>

using std::cout;
using std::endl;

// 类Matrix的声明
class Matrix {
public:
    Matrix(int n, int m);       // 构造函数,构造一个n*m的矩阵
    Matrix(int n);              // 构造函数,构造一个n*n的矩阵
    Matrix(const Matrix &x);    // 复制构造函数, 使用已有的矩阵X构造
    ~Matrix();

    void set(const double *pvalue);         // 用pvalue指向的连续内存块数据按行为矩阵赋值
    void set(int i, int j, double value);   // 设置矩阵对象索引(i,j)的元素值为value
    
    double& at(int i, int j) const;         // 返回矩阵对象索引(i,j)的元素引用
    double& at(int i, int j);               // 返回矩阵对象索引(i,j)的元素引用
    
    int get_lines() const;                  // 返回矩阵对象行数
    int get_cols() const;                   // 返回矩阵对象列数

    void print() const;                     // 按行打印输出矩阵对象元素值

private:
    int lines;      // 矩阵对象内元素行数
    int cols;       // 矩阵对象内元素列数
    double *ptr;
};

// 类Matrix的实现:待补足
// xxx
matrix.hpp
#include <iostream>
#include "matrix.hpp"

using namespace std;

const int N1 = 3;
const int N2 = 2;

// 输出一个矩阵对象中索引为index对应的行的所有元素值
void output(const Matrix &m, int index) {
    for(auto j = 0; j < m.get_cols(); ++j)
        cout << m.at(index, j) << ", ";
    cout << "\b\b \n";
}

void test() {


    double x[N1*N2] = {1, 2, 3, 4, 5, 6};

    Matrix m1(N1, N2);      // 创建一个N1×N2矩阵
    m1.set(x);              // 用一维数组x的值按行为矩阵m1赋值
    cout << "矩阵对象m1: " << endl;
    m1.print();             // 打印矩阵m1的值
    cout << "矩阵对象m1第0行是: " << endl;
    output(m1, 0);
    cout << endl;

    Matrix m2(N2, N1);
    m2.set(x);
    cout << "矩阵对象m2: " << endl;
    m2.print();         
    cout << "矩阵对象m2第0行是: " << endl;
    output(m2, 0);
    cout << endl;

    Matrix m3(m2);      // 用矩阵m2构造新的矩阵m3
    m3.set(0, 0, 999);  // 讲矩阵对象m2索引(0,0)元素设为999
    cout << "矩阵对象m3:" << endl;
    m3.print();
    cout << endl;

    Matrix m4(2);       // 创建一个2*2矩阵对象
    m4.set(x);          // 用一维数组x的值按行为矩阵m4赋值
    cout << "矩阵对象m4:" << endl;
    m4.print();
}

int main() {
    test();
}
task6.cpp

运行结果

 

标签:const,数组,Point,int,void,实验,vectorPoint,include,指针
From: https://www.cnblogs.com/le-sh/p/17797438.html

相关文章

  • 实验三-类与数组、指针
    point.hpp1#pragmaonce23#include<iostream>4usingstd::cout;5usingstd::endl;67classPoint{8public:9Point(intx0=0,inty0=0);10~Point()=default;1112intget_x()const;13intget_y()const;1......
  • 实验2 类和对象_基础编程2
    实验任务1demo1.dev方法一#ifndefT_H#defineT_H#include<iostream>#include<string>usingnamespacestd;//类T的声明classT{public:T(intx=0,inty=0);//带有默认形值的构造函数T(constT&t);//复制构造函数T(T&&......
  • 查找数组中元素
    查找数组中元素任务详情输入一个固定长度的数组,并输入一个要查找的数,给出能不能检索到的伪代码并测试伪代码fact赋值为0输入长度为8的数组num输入想检索的数searchi赋值为0如果i不超过7{判断num[i]是否等于search等于则fact赋值为1并结束循环i赋值为i+1}如果fact为1......
  • 面试必刷TOP101:21、旋转数组的最小数字
    题目题解二分法:importjava.util.ArrayList;publicclassSolution{publicintminNumberInRotateArray(int[]array){//特殊情况判断if(array.length==0){return0;}//左右指针ijinti=0,j=array.......
  • 实验3
    实验1源代码1#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN8067voidprint_text(intline,intcol,chartext[]);8voidprint_spaces(intn);9voidprint_blank_lines(intn);10......
  • Java小白学习记录--------常见的一维数组遍历方法
    一维数组:for循环遍历:int[]myArray={1,2,3,4,5};for(inti=0;i<myArray.length;i++){System.out.println("myArray["+i+"]="+myArray[i]);//输出数组中的每个元素} for-each循环遍历数组(增强for循环遍历)int[]myArray={1,2,3,4,5};......
  • linux shell中 创建数组及数组的基本操作
     001、创建数组a、方法1[root@pc1test01]#ay1=(100200"abc""xyz")##创建数组 b、方法2[root@pc1test01]#ay2[0]=100[root@pc1test01]#ay2[1]=200[root@pc1test01]#ay2[3]="mn" 002、访问数组的全部元素[root@pc1test01]#ay1=(100......
  • 实验3 类与数组、指针
    实验任务1point.hpp#pragmaonce#include<iostream>usingstd::cout;usingstd::endl;classPoint{public:Point(intx0=0,inty0=0);~Point()=default;intget_x()const;intget_y()const;voidshow()const;voidmov......
  • 【教3妹学编程-算法题】使数组变美的最小增量运算数
    2哥 :3妹,脸上的豆豆好了没呢。3妹:好啦,现在已经没啦2哥 :跟你说很快就会消下去的,还不信~既然你的容颜和心情都如此美丽,那我们就再做一道关于美丽的题吧。3妹:切,2哥就会取笑我,伤心时让我做题,开心时也让我做题! 1题目: 给你一个下标从0开始、长度为n的整数数组nums,和一个整......
  • 数组的练习专项(接上节的数组的详解)
     练习1:求和需求:定义一个数组,存储1,2,3,4,5遍历数组得到每一个元素,求数组里面所有的数据和代码示例:/*定义一个数组,存储1,2,3,4,5遍历数组得到每一个元素,求数组里面所有的数据和*///分析://1.定义一个数组,并添加数据1,2,3,4,5int[]arr={1,2,3,4,5};//求和变量int......