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

实验三 类与数组、指针

时间:2023-11-05 20:01:16浏览次数:25  
标签:index const vectorPoint Point int 实验 数组 include 指针

任务一

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;
void output(const vector<Point> &v) {
for(auto &t: v)
t.show();
}
void test() {
int n;
cin >> n;
vector<Point> x(n);
cout << "x" << endl;
output(x);
vector<Point> y(x);
cout << "\nx " << endl;
output(y);
cout << "\nupdate x" << endl;
x.at(0).move(30, 50);
x.push_back(Point(2, 2));
cout << "\nx total:" << endl;
output(x);
cout << "\ny total: " << endl;
output(y);
}
int main() {
test();
}
View Code

运行结果

 

任务二

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);
Point& at(int 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>
void output(const vectorPoint &v) {
for(auto i = 0; i < v.get_size(); ++i)
v.at(i).show();
}
void test() {
using namespace std;
int n;
cout << "vectorPoint yuansugeshu: ";
cin >> n;
vectorPoint x(n);
cout << "x information: " << endl;
output(x);
vectorPoint y(x);
cout << "\ny information: " << endl;
output(y);
cout << "\nupdate x information......" << endl;
x.at(0).move(30, 50);
x.at(1).move(-1, -1);
cout << "x information: " << endl;
output(x);
cout << "\ny information: " << endl;
output(y);
}
int main() {
test();
}
View Code

运行结果

 

任务三

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);
Point& at(int 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>
void output(const vectorPoint &v) {
for(auto i = 0; i < v.get_size(); ++i)
v.at(i).show();
}
void test() {
using namespace std;
int n;
cout << "input vectorPoint how much number: ";
cin >> n;
vectorPoint x(n);
cout << "x information: " << endl;
output(x);
vectorPoint y(x);
cout << "\ny information: " << endl;
output(y);
cout << "\nupdate x information......" << endl;
x.at(0).move(30, 50);
x.at(1).move(-1, -1);
cout << "x information: " << endl;
output(x);
cout << "\ny information: " << endl;
output(y);
}
int main() {
test();
}
View Code

运行结果

 

任务四

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;

    cout << "&a = " << hex << &a << endl;
    cout << "&ra = " << hex << &ra << endl;
    cout << "&pa = " << hex << &pa << "\n\n";
    
    cout << "a = " << a << endl;
    cout << "ra = " << a << endl;
    cout << "pa = " << hex << pa << endl;
    
    cout << "*pa = " << *pa << "\n\n";

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

template<typename T>
void square2(T &x) {
    for(auto &i: x) 
        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

运行结果

 

任务五

task5.cpp

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

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

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

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

    vectorInt x1(n);
    for(auto i = 0; i < n; ++i)
        x1.at(i) = i*i;
    cout << "vectorInt对象x1: ";
    output(x1);

    vectorInt x2(n, 42);
    cout << "vectorInt对象x2: ";
    output(x2);
    vectorInt x3(x2);
    cout << "vectorInt对象x3: ";
    output(x3);

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

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

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

vectorInt.hpp

#include<iostream>
#include<cassert>
using namespace std;
class vectorInt
{
public:
    vectorInt(int n);
    vectorInt(int n,int m);
    vectorInt(const vectorInt & vc);
    int get_size() const;
    ~vectorInt();
    int& at(int index);
    int& at(int index) const;
private:
    int size;
    int *ptr;
};
vectorInt::vectorInt(int n)
{
    size=n;
    ptr =new int[n];
    cout<<"constructor vectorInt(int n) called."<<endl;
}
vectorInt::vectorInt(int n,int m)
{
    size=n;ptr= new int[n];
    for(int i=0;i<size;i++)
        ptr[i]=m;
    cout<<"constructor vectorInt(int n,int value) called"<<endl;

}
vectorInt::vectorInt(const vectorInt & vc)
{
    size=vc.size;
    ptr=new int[size];
    for(int i = 0; i < size; ++i)
        ptr[i] = vc.ptr[i];
    cout<<"copy constructor called"<<endl;
}
vectorInt::~vectorInt()
{
    delete[] ptr;
    cout<<"destructor called"<<endl;
}
int vectorInt::get_size() const
{
    return size;
}
int& vectorInt::at(int index)
{
    assert(index >= 0 && index < size); 
    return ptr[index];
}
int& vectorInt::at(int index) const
{
    assert(index >= 0 && index < size);
    return ptr[index];
}
View Code

运行结果

 

任务六

task6.cpp

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

using namespace std;

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

void output(const Matrix &m, int index) {
    for(int 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);
    m1.set(x);
    cout << "矩阵对象m1: " << endl;
    m1.print();
    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);
    m3.set(0, 0, 999);
    cout << "矩阵对象m3:" << endl;
    m3.print();
    cout << endl;

    Matrix m4(2);
    m4.set(x);
    cout << "矩阵对象m4:" << endl;
    m4.print();
}

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

matrix.hpp

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

class Matrix {
public:
    Matrix(int n, int m);
    Matrix(int n); 
    Matrix(const Matrix &x); 
    ~Matrix();
    void set(const double *pvalue); 
    void set(int i, int j, double value); 
    double& at(int i, int j) const;
    double& at(int i, int j); 
    int get_lines() const; 
    int get_cols() const;
    void print() const; 
private:
    int lines;
    int cols; 
    double *ptr;
};
Matrix::Matrix(int n,int m)
{
    lines=n;cols=m;
    ptr= new double[n*m];
}
Matrix::Matrix(int n)
{
    lines=n;cols=n;
    ptr=new double[lines*cols];
}
Matrix::Matrix(const Matrix &x)
{
    lines=x.lines;
    cols=x.cols;
    ptr=new double[cols*lines];
    for(int i=0;i<lines;i++)
        for(int j=0;j<cols;j++)
            ptr[i*cols+j]=x.ptr[i*cols+j];
}
Matrix::~Matrix()
{
    delete[] ptr;
}
void Matrix::set(const double *pvaule)
{
    for(int i=0;i<lines;i++)
        for(int j=0;j<cols;j++)
            ptr[i*cols+j]=pvaule[i*cols+j];
}
void Matrix::set(int i,int j,double value)
{
    ptr[i*cols+j]=value;
}
double& Matrix::at(int i,int j)
{
    return ptr[i*cols+j];
}
double& Matrix::at(int i,int j) const
{
    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++)
        {
            cout<<ptr[i*cols+j]<<"  ";
        }
    cout<<endl;
    }
}
View Code

运行结果

 

标签:index,const,vectorPoint,Point,int,实验,数组,include,指针
From: https://www.cnblogs.com/NFurioso/p/17811033.html

相关文章

  • 实验三
    实验一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);1011......
  • 2023年11月第一周题解-------数组
    1.问题A:LY学长的随机数解题思路第一种思路是先去重后排序第二种思路是先排序再去重解题方法暴力遍历#define_CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>#include<time.h>#defineN10voidquickSort......
  • 【单片机】初次实验:Keil51的使用
    哔哩哔哩/CSDN/博客园:萌狼蓝天延时器delay(intcount){ inti,j; for(i=0;i<count;i++){ for(j=0;j<1000;j++); }}瞧一瞧题目要求:P0口接八个发光二极管,先让后面四个灯亮,再让前面四个灯亮,循坏#include<REGX51.H>delay(intcount){ inti,j; for(i=0;i<count;i+......
  • Go语言使用range修改值,需要使用切片的指针 &slice[index]
    由于Value是值拷贝的,并非引用传递,所以直接改Value是达不到更改原切片值的目的的,需要通过&slice[index]获取真实的地址packagemainimport("fmt")funcmain(){ slice:=[]int{10,20,30,40} forindex,value:=rangeslice{ fmt.Printf("Value=%d,value-addr......
  • 实验3
    1.Point.h#pragmaonce#include<iostream>usingstd::cout;usingstd::endl;classPoint{public:Point(intx0=0,inty0=0);~Point()=default;intget_x()const;intget_y()const;voidshow()const;voidmove(intn......
  • 实验三
    #include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprint_speace(intn);voidprint_blank_lines(intn);intmain(){intline,col,i;......
  • 基础排序——数组和链表实现(C)
    一、数组实现①选择排序从index=0开始,每一轮找到一个最小的元素,然后交换num[index]和num[min]的位置,直至数组遍历完。得到一个升序数组。voidselectSort(int*num,intn){for(inti=0;i<n;i++){intmin=i;for(intj=i+1;j<n;j++){......
  • 查找数组中元素
    作业要求输入一个固定长度的数组,并输入一个要查找的数,给出能不能检索到的伪代码并测试。伪代码Setfirstto0Setlasttolength-1SetfoundtoFALSEWHILE(first<=lastANDNOTfound)Setmiddleto(first+last)/2IF(itemequalsdat......
  • 实验三
    1..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;voidmove(intn......
  • 实验二
    #include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1374#defineN2465intmain(){intnumber;inti;srand(time(0));for(i=0;i<N;++i){number=rand()%(N2-1+1)+N1;printf("......