实验4 类与数组、指针

实验任务1

程序源码

point.hpp

#pragma once

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

class Point {
public:
    Point(): x{0}, y{0} {}
    Point(int x0, int y0): x{x0}, y{y0} {} 
    ~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;
};

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;
}

task1.cpp

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

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


// 测试动态数组类接口
void test1() {
    int n;
    cin >> n;

    vector<Point> x(n); // 创建一个vector<Point>对象,指定包含n个元素
    x.at(0).show();
    x.at(0).move(30, 50);
    x.at(0).show();
}

// 测试复制构造
void test2() {
    int n;
    cin >> n;

    vector<Point> x(n);
    x.at(0).show();

    vector<Point> y(x);  // 基于vector<Point>对象x构建对象y
    y.at(0).show();
    
    x.at(0).move(30, 50);

    x.at(0).show();
    y.at(0).show();
}

int main() {
    cout << "测试动态数组类接口...\n";
    test1();

    cout << "测试复制构造...\n";
    test2();
}

测试截图

实验任务2

程序源码

point.hpp

#pragma once

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

class Point {
public:
    Point(): x{0}, y{0} { cout << "Point: default constructor called.\n"; }
    Point(int x0, int y0): x{x0}, y{y0} { cout << "Point: constructor called.\n"; }
    ~Point() { cout << "Point: destructor called.\n"; }

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

private:
    int x, y;
};

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;
}

 

vectorPoint.hpp

#pragma once

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

class vectorPoint{
public:
    vectorPoint(int n);
    ~vectorPoint();
    
    Point &at(int index); // 返回下标为index的元素引用

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

vectorPoint::vectorPoint(int n) : size{n} {
    cout << "dynamic create array..." << endl;
    p = new Point[n];
}

vectorPoint::~vectorPoint() {
    cout << "dynamic delete array..." << endl;
    delete[] p;
}

Point &vectorPoint::at(int index) {
    assert(index >= 0 && index < size);  // 宏,通常测试用。如果不满足条件,终止程序
    return p[index];
}

 

task2.cpp

测试截图

注:后续无停止输出Point: destructor called.

修改vectorPoint.hpp

#pragma once

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

class vectorPoint{
public:
    vectorPoint(int n);
    vectorPoint(const vectorPoint &vp); 
    ~vectorPoint();
    
    Point &at(int index); // 返回下标为index的元素引用

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

vectorPoint::vectorPoint(int n) : size{n} {
    cout << "dynamic create array..." << endl;
    p = new Point[n];
}

// 复制构造函数
vectorPoint::vectorPoint(const vectorPoint &vp): size{ vp.size } {
cout << "copy array..." << endl;
p = new Point[size];
for(auto i = 0; i < size; ++i)
p[i] = vp.p[i];
}

vectorPoint::~vectorPoint() {
    cout << "dynamic delete array..." << endl;
    delete[] p;
}

Point &vectorPoint::at(int index) {
    assert(index >= 0 && index < size);  // 宏,通常测试用。如果不满足条件,终止程序
    return p[index];
}

修改后测试截图

实验任务3

程序源码

task3_1.cpp

#include <iostream>

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

int main() {
    using namespace std;

    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);
}


// 函数定义
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";
}

测试截图

程序源码

task3_2

#include <typeinfo>
#include <iostream>

int main() {
    using namespace std;

    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;
}

 

测试截图

 

程序源码

task3_3

#include <iostream>
#include <vector>

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;
}

int main() {
    using namespace std;

    vector<int> x{1,2,3,4,5};
    vector<int> y{x};

    output(x);
    square1(x);
    output(x);

    output(y);
    square2(y);
    output(y);
}

 

测试截图

实验任务4

程序源码

ball.hpp

#pragma once

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

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

const int SIZE_X = 50; // 小球x轴移动范围0 ~ SIZE_X - 1
const int SIZE_Y = 50; // 小球y轴移动范围0 ~ SIZE_Y - 1

class Ball {
public:
    Ball(int x = 0, int y = 0); // 在坐标(x,y)处构造一个小球
    void left(int step = 1);    // 左移step
    void right(int step = 1);   // 右移step
    void up(int step = 1);      // 上移step
    void down(int step = 1);    // 下移step
private:
    int x; // x坐标
    int y; // y坐标
};

Ball::Ball(int x0, int y0) : x{x0}, y{y0} {
    print_ball(x, y);
}

void Ball::left(int step) {
    x -= step;

    if (x <= 0)
        x = 0;

    // 在更新后的坐标(x,y)处打印小球
    print_ball(x, y);
}

void Ball::right(int step) {
    x += step;

    if (x >= SIZE_X)
        x = SIZE_X;

    // 在更新后的坐标(x,y)处打印小球
    print_ball(x, y);
}

void Ball::up(int step) {
    y -= step;
    if (y <= 0)
        y = 0;

    // 在更新后的坐标(x,y)处打印小球
    print_ball(x, y);
}

void Ball::down(int step) {
    y += step;

    if (y >= SIZE_Y)
        y = SIZE_Y;

    // 在更新后的坐标(x,y)处打印小球
    print_ball(x, y);
}

utils.hpp

#pragma once

#include <iostream>
#include <string>
#include <cstdlib>
using std::cout;
using std::string;

// 函数声明
void print_blank_lines(int n);        // 打印n行空白行
void print_spaces(int n);             // 打印n个空格
void print_ball(int x, int y);        // 在坐标(x,y)处打印小球
void set_color(string bg, string fg); // 设置屏幕背景色、前景色

// 函数定义
// 打印n行空行
void print_blank_lines(int n) {
    for (int line = 1; line <= n; ++line)
        cout << "\n";
}

// 打印n个空格
void print_spaces(int n) {
    for (int i = 1; i <= n; ++i)
        cout << " ";
}

// 在坐标(x,y)处打印小球
void print_ball(int x, int y) {
    // 清屏
    system("cls");

    // 打印y-1行空行
    print_blank_lines(y - 1);

    // 打印x-1个空格
    print_spaces(x - 1);

    // 打印小球
    cout << "O" << "\n";
}

// 设置屏幕背景色、前景色
void set_color(string bg, string fg) {
    string color = "color ";

    color += bg;
    color += fg;
    system( color.c_str() );
}

 

canvas.hpp

#pragma once

#include <string>
#include "utils.hpp"
using std::string;

class Canvas
{
public:
    Canvas(string bg0 = "0", string fg0 = "A");
    void change_bg(string bg_new);                   // 改变背景色
    void change_fg(string fg_new);                   // 改变前景色
    void change_color(string bg_new, string fg_new); // 同时改变前景色和背景色

private:
    string bg; // 背景色(background color)
    string fg; // 前景色(foreground color)
};

Canvas::Canvas(string bg0, string fg0) : bg{bg0}, fg{fg0} {
    set_color(bg, fg);
}

void Canvas::change_bg(string new_bg) {
    bg = new_bg;
    set_color(bg, fg);
}

void Canvas::change_fg(string new_fg) {
    fg = new_fg;
    set_color(bg, fg);
}

void Canvas::change_color(string new_bg, string new_fg) {
    bg = new_bg;
    fg = new_fg;

    set_color(bg, fg);
}

 

task4.cpp

#include <iostream>
#include "canvas.hpp"
#include "ball.hpp"

void test() {
    Canvas canvas;
    
    Ball ball1(10, 10);
    system("Pause");

    ball1.left(5);
    system("Pause");

    ball1.up(20);
    system("Pause");

    canvas.change_fg("E");      // 更新画布前景色
    system("Pause");

    canvas.change_bg("D");      // 更新画布背景色
    system("Pause");
}

int main() {
    test();
}

 

实验任务5

程序源码

vectorInt.hpp

#pragma once

#include<iostream>
#include <cassert>

using namespace std;

class vectorInt{
public:
    vectorInt(int n);
    vectorInt(int n,int x);
    vectorInt(const vectorInt& y);
    ~vectorInt();
    
    int &at(int index);
    int get_size();
    friend void output(vectorInt& x){
        int i;
        for(i=0;i<x.size-1;i++)
        cout<<x.p[i]<<" ";
        cout<<x.p[i]<<endl;
    };
private:
    int size;    
    int x;
    int *p;
};

vectorInt::vectorInt(int n):size{n}{
    p=new int[n];
    for(int i=0;i<n;i++)
    p[i]=x;
    cout << "constructor 1 called." << endl;
}

vectorInt::vectorInt(int n,int x):size{n},x{x}{
    p=new int[n];
    for(int i=0;i<n;i++)
    p[i]=x;
    cout << "constructor 2 called." << endl;
}

vectorInt::vectorInt(const vectorInt& y){
    size=y.size;
    x=y.x;
    p=new int[size];
    for(int i=0;i<size;i++)
    p[i]=y.p[i];
    cout<<"copy constructor called."<<endl;
}

vectorInt::~vectorInt() {
    cout << "destructor called." << endl;
    delete[] p;
}

int &vectorInt::at(int index){
    assert(index>=0&&index<size);
    return p[index];
}

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

task5.cpp

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

void test() {
    using namespace std;

    int n;
    cin >> n;
    
    vectorInt x1(n);
    for(auto i = 0; i < n; ++i)
        x1.at(i) = i*i;

    output(x1);

    vectorInt x2(n, 42);
    vectorInt x3(x2);

    output(x2);
    output(x3);

    x2.at(0) = 77;

    output(x2);
    output(x3);
}

int main() {
    test();
}

测试截图

实验任务6

程序源码

Matrix.hpp

#pragma once

#include <iostream>


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

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

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

private:
    int lines; // 矩阵行数
    int cols;  // 矩阵列数
    double *p; // 指向存放矩阵数据的内存块的首地址
};

Matrix::Matrix(int n):lines{n},cols{n}{
    p=new double [n*n];
}

Matrix::Matrix(int n,int m):lines{n},cols{m}{
    p=new double [n*m];
}

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

Matrix::~Matrix()
{
    for(int i=0;i<lines*cols;i++)
        delete[] p;
}

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

void Matrix::set(int i, int j, int value){
    p[i*cols+j]=value;
}

double &Matrix::at(int i, int j){
    return p[i*cols+j];
}

double Matrix::at(int i, int j) const{
    return p[i*cols+j];
}

int Matrix::get_lines() const{
    return lines;
}

int Matrix::get_cols() const{
    return cols;
}

void Matrix::print() const{
    int i,j;
    for ( i = 0; i < lines; i++){
        for ( j = 0; j < cols-1; j++)
        {
            cout<<p[i*cols+j]<<",";
        }
        cout<<p[i*cols+j]<<endl;
    }
}

task6.cpp

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

void test() {
    using namespace std;

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

    Matrix m1(3, 2);    // 创建一个3×2的矩阵
    m1.set(x);          // 用一维数组x的值按行为矩阵m1赋值
    m1.print();         // 打印矩阵m1的值
    cout << "the first line is: " << endl;
    cout << m1.at(0, 0) << " " << m1.at(0, 1) << endl;  // 输出矩阵m1第1行两个元素的值
    cout << endl;

    Matrix m2(2, 3);
    m2.set(x);
    m2.print();
    cout << "the first line is: " << endl;
    cout << m2.at(0, 0) << " " << m2.at(0, 1) << " " << m2.at(0, 2) << endl;
    cout << endl;

    Matrix m3(m2);      // 用矩阵m2构造新的矩阵m3
    m3.set(0, 0, 999);  // 将矩阵m3第0行第0列元素值设为999
    m3.print();
}

int main() {
    test();
}

测试截图

 

posted @ 2022-11-02 19:03  阑夜风华  阅读(21)  评论(0编辑  收藏  举报