#include <bits/stdc++.h> using namespace std; class Vector_int { private: int *arrayAd; int length; public: Vector_int(const int &B); Vector_int(const int &n, const int &m); ~Vector_int(); Vector_int(const Vector_int &A) { length = A.length; arrayAd = new int[length]; for (int y = 0; y < length; y++) { arrayAd[y] = A.arrayAd[y]; } } int at(const int &m); void show() { cout << "DATA: "; for (int j = 0; j < length; j++) { cout << arrayAd[j] << " "; } cout << endl; } }; Vector_int::Vector_int(const int &n) { length = n; arrayAd = new int[n]; } Vector_int::Vector_int(const int &n, const int &m) { length = n; arrayAd = new int[n]; for (int i = 0; i < n; i++) { arrayAd[i] = m; } } Vector_int::~Vector_int() { delete[] arrayAd; } int Vector_int::at(const int &m) { cout << "arrayAd[ " << m << " ] = " << arrayAd[m]; cout << endl; }View Code
#include"Vector_int.hpp" #include<bits/stdc++.h> using namespace std; int main() { int f,I,J,K; int v[1000]; cout<<"数组元素个数/初始化: "<<endl; cin>>f>>J; cout<<"索引:"; cin>>K; Vector_int M(f,J); Vector_int N(M); M.at(K); M.show(); N.show(); return 0; }View Code
#ifndef MATRIX_H #define MATRIX_H #include <iostream> #include <cassert> using namespace std; class Matrix { public: Matrix(const int n); Matrix(const int n,const int m); Matrix(const Matrix &X); ~Matrix(); void set(const double *pvalue); void set(const int i,const int j,const int value); double & at(const int i,const int j) { return p[i*cols+j]; } double at(const int i,const int j) const { return p[i*cols+j]; } int get_lines() const; int get_cols() const; void print() const; private: int lines; int cols; int size; double *p; }; Matrix::Matrix(const int n) { lines=n; cols=n; size=n*n; p=new double[size]; } Matrix::Matrix(const int n,const int m) { lines=n; cols=m; size=n*m; p = new double[size]; } Matrix::Matrix(const Matrix &x) { lines=x.lines; cols=x.cols; p = new double[x.size]; for(int i=0;i<x.size;i++) { p[i]=x.p[i]; } } void Matrix::set(const double *pvalue) { for(int i=0;i<size;i++) p[i]=pvalue[i]; } void Matrix::set(const int i,const int j,const int value) { p[i*cols+j]=value; } 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<<p[i*cols+j]<<" "; } cout<<endl; } } Matrix::~Matrix() { delete[] p; } #endifView Code
#include <iostream> #include "Matrix.hpp" int main() { 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; 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(); }View Code