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

实验4 类与数组、指针

时间:2022-11-08 20:24:25浏览次数:52  
标签:const Matrix int double lines cols 实验 数组 指针

实验五

vectorint.h

#pragma once

#include<iostream>

using namespace std;

class vectorInt {
public:
	int s, * p;

	vectorInt(int m)
	{
		p = new int [m];
		s = m;
		cout << "constructor 1 called." << endl;
	}
	vectorInt(int m, int value)
	{
		p = new int [m];
		s = m;
		for (int i = 0;i < m;i++)
			p[i] = value;
		cout << "constructor 2 called." << endl;
	}
	vectorInt(const vectorInt& x)
	{
		s = x.s;
		p = new int[s];
		for (int i = 0;i < s;i++)
			p[i] = x.p[i];
		cout << "copy constructor called." << endl;
	}
	~vectorInt()
	{
		delete[] p;
		cout << "destructor called" << endl;
	}

	int &at(int m)
	{
		return p[m];
	}
	int get_size()
	{
		return s;
	}

	friend void output(const vectorInt& x)
	{
		for (int i = 0;i < x.s;i++)
			cout << x.p[i] << ",";
		cout << "\b" <<" " << endl;
	}
};

  task

#include <iostream>
#include "vectorint.h"
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();
}

  

 

实验六

Matrix.h

#pragma once

#include <iostream>

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

class Matrix {
public:
	Matrix(int n)
	{
		lines = n;
		cols = n;
		p = new double[lines * cols];
	}
	Matrix(int n, int m)
	{
		lines = n;
		cols = m;
		p = new double[lines * cols];
	}
	Matrix(const Matrix& X)
	{
		lines = X.lines;
		cols = X.cols;
		p = new double[lines * cols];
		for (int i = 0;i < lines * cols;i++)
			p[i] = X.p[i];
	}
	~Matrix()
	{
		delete[] p;
	}

	void set(const double* pvalue)
	{
		for (int i = 0;i < lines * cols;i++)
		{
			p[i] = *pvalue;
			pvalue++;
		}
	}
	void set(int i, int j, int value)
	{
		p[i * cols + j] = value;
	}
	double& at(int i, int j)
	{
		return p[i * cols + j];
	}
	double at(int i, int j) const
	{
		return p[i * cols + j];
	}
	int get_lines() const
	{
		return lines;
	}
	int get_cols() const
	{
		return cols;
	}
	void print() const
	{
		for (int i = 0;i < lines * cols;i++)
		{
			cout << p[i] << ",";
			if ((i+1) % cols == 0)
				cout << "\b" << " " << endl;
		}
	}
private:
	int lines; 
	int cols; 
	double* p; 
};

  task

#include <iostream>
#include "Matrix.h"
void test() {
	using namespace std;
	double x[] = { 1, 2, 3, 4, 5, 6 };
	Matrix m1(3, 2);
	m1.set(x); 
	m1.print(); 
	cout << "the first line is: " << endl;
	cout << m1.at(0, 0) << " " << m1.at(0, 1) << endl; 
		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); 
	m3.set(0, 0, 999); 
	m3.print();
} 
int main()
{
	test();
}

  

 

标签:const,Matrix,int,double,lines,cols,实验,数组,指针
From: https://www.cnblogs.com/Samapoketto/p/16871032.html

相关文章

  • 实验3
    #include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_test(intline,intcol,chartest[]);voidprint_spaces(int......
  • 实验4 类与数组、指针
    Task1~4浅复制:inta=10;intb=a;可用于静态内存的复制。对于简单的类,默认的复制构造函数已经够用了,但当类持有其他资源,如动态分配的内存、指针等,就需要用到深复制......
  • 软件工程最大连续子数组和(最大子段和)
     题目一:最大连续子数组和(最大子段和)背景 问题:给定n个整数(可能为负数)组成的序列a[1],a[2],a[3],…,a[n],求该序列如a[i]+a[i+1]+…+a[j]的子段和的最大值。当所给的整......
  • 实验四
    实验任务五cpp#include<iostream>#include"vectorInt.hpp"voidtest(){usingnamespacestd;intn;cin>>n;vectorIntx1(n);for(aut......
  • 第一次实验题目
    1.题目:最大连续子数组和(最大子段和)问题:给定n个整数(可能为负数)组成的序列a[1],a[2],a[3],…,a[n],求该序列如a[i]+a[i+1]+…+a[j]的子段和的最大值。当所给的整数均为负数......
  • 实验四
    //task.5.cpp#include<iostream>#include"vectorInt.hpp"voidtest(){usingnamespacestd;intn;cin>>n;vectorIntx1(n);for(au......
  • 实验四类和对象数组及指针
    11#pragmaonce22#include<iostream>3344usingstd::cout;55usingstd::endl;6677classvectorInt{88public:99//构造......
  • mysql根据json字段内容作为查询条件(包括json数组)检索数据
    最近用到了mysql5.7的json字段的检索查询,发现挺好用的,记录一下笔记我们有一个日志表,里面的data字段是保存不同对象的json数据,遇到想根据里面的json的字段内容作为条件查询......
  • 8种现代数组方法,每个开发人员都应该知道
    英文| https://javascript.plainenglish.io/8-modern-array-methods-that-every-developer-should-know-416855e01757翻译|小爱在用代码执行数组操作时,你是否经想过,关于......
  • 223201062522-软件工程基础Y- 实验一 刘晋
      沈阳航空航天大学软件工程基础实验报告实验名称:实验一实验题目:个人项目完成时间:2022年11月1实验内容及要求1.1教学内容及要求建立个人博客,完......