Matrix & Stack

This commit is contained in:
e2hang
2025-07-21 15:18:52 +08:00
5 changed files with 396 additions and 0 deletions

74
Matrix/main.cpp Normal file
View File

@@ -0,0 +1,74 @@
#include <iostream>
#include "matrix.h"
using namespace std;
/*
TO TEST:
ok matrix(int rows = 0, int cols = 0);
ok ~matrix() { delete[] element; }
ok int rowss() const { return rows; }
ok int columns() const { return cols; }
ok int times(int i, int j, matrix<T>& left, matrix<T>& right) const;//If unused, write it in private
T& operator()(int i, int j) const;
matrix<T>& operator=(const matrix<T>& x);
//matrix<T> operator+() const;
matrix<T> operator+(const matrix<T>& x) const;
//matrix<T> operator-() const;
matrix<T> operator-(const matrix<T>& x) const;
matrix<T> operator*(const matrix<T>& x) const;
matrix<T>& operator+=(const matrix<T>& x);
friend ostream& operator<<(const matrix<T>& x);
*/
int main() {
int l[] = { 1,2,3,4,5,6 };
matrix<int> left(2, 3, l), right(3, 2, l);
cout << "left: " << endl << left << endl;
cout << "right: " << endl << right << endl;
cout << "left(1, 1): " << left(1, 1) << endl;
cout << "right(2, 1): " << right(2, 1) << endl << endl;
int out[] = { 1,2,3,4 };
matrix<int> tmp(2, 2, out);
tmp = left * right;
cout << "times: " << endl << tmp << endl << endl;
//<2F><><EFBFBD><EFBFBD><EFBFBD>еط<D0B5>dz<EFBFBD><C7B3><EFBFBD><EFBFBD><EFBFBD>ˣ<EFBFBD><CBA3><EFBFBD><EFBFBD>Թ<EFBFBD><D4B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ü<EFBFBD><C3BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǿ<EFBFBD><C7BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><ECBAAF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E2A3AC><EFBFBD><EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ã<EFBFBD><C3A3><EFBFBD><E1B4B4><EFBFBD>µĶ<C2B5><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E2A3AC><EFBFBD><EFBFBD>double delete֮<65><D6AE>
cout << "transpose: " << endl << tmp.transpose() << endl;
cout << "transpose left: " << endl << left.transpose() << endl;
cout << "transpose right: " << endl << right.transpose() << endl;
return 0;
}
/*
left:
1 2 3
4 5 6
right:
1 2
3 4
5 6
left(1, 1): 1
right(2, 1): 3
times:
22 28
49 64
transpose:
22 49
28 64
transpose left:
1 4
2 5
3 6
transpose right:
1 3 5
2 4 6
*/

168
Matrix/matrix.h Normal file
View File

@@ -0,0 +1,168 @@
#pragma once
#include <iostream>
template <class T>
class matrix {
private:
int rows;//<2F><>
int cols;//<2F><>
T* element;//ע<><D7A2><EFBFBD><EFBFBD>T* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>T** <20>ײ<EFBFBD><D7B2><EFBFBD><EFBFBD><EFBFBD>
public:
matrix() = delete;
matrix(int rows = 0, int cols = 0, T* x = nullptr);
~matrix() { delete[] element; }
int rowss() const { return rows; }
int columns() const { return cols; }
int times(int i, int j, const matrix<T>& left, const matrix<T>& right) const;//If unused, write it in private
T& operator()(int i, int j) const;
matrix<T>& operator=(const matrix<T>& x);
//matrix<T> operator+() const;
matrix<T> operator+(const matrix<T>& x) const;
//matrix<T> operator-() const;
matrix<T> operator-(const matrix<T>& x) const;
matrix<T> operator*(const matrix<T>& x) const;
matrix<T>& operator+=(const matrix<T>& x);
matrix<T>& transpose();
template <class T>
friend std::ostream& operator<<(std::ostream& os, const matrix<T>& x);
bool operator==(const matrix<T>& other) const {
if (rows != other.rows || cols != other.cols)
return false;
for (int i = 0; i < rows * cols; ++i)
if (element[i] != other.element[i])
return false;
return true;
}
bool operator!=(const matrix<T>& other) const {
return !(*this == other);
}
};
template <class T>
int matrix<T>::times(int i, int j, const matrix<T>& left, const matrix<T>& right) const {
int tmp = 0;
for (int x = 1; x <= left.cols; x++) {
tmp += left(i, x) * right(x, j);
}
return tmp;
}
template <class T>
matrix<T>::matrix(int rows, int cols, T* x) : rows(rows), cols(cols){
if (rows < 0 || cols < 0) {
throw std::out_of_range("Matrix subscript out of range");
}
int sum = rows * cols;
element = new T[sum];
if (x) {
for (int i = 0; i < sum; i++) element[i] = x[i];
}
else {
for (int i = 0; i < sum; i++) element[i] = T();
}
}
template <class T>
T& matrix<T>::operator()(int i, int j) const {
if (i < 1 || i > rows || j < 1 || j > cols) {
throw std::out_of_range("Matrix subscript out of range");
}
return element[(i - 1) * cols + j - 1];//important, using cols rather than rows
}
template <class T>
matrix<T>& matrix<T>::operator=(const matrix<T>& x) {
if (*this != x) {
delete[] element;
int sum = x.rows * x.cols;
element = new T[sum];
for (int i = 0; i < sum; i++) {
element[i] = x.element[i];
}
}
return *this;
}
template <class T>
matrix<T> matrix<T>::operator+(const matrix<T>& x) const{
if (rows != x.rows || cols != x.cols) {
throw std::invalid_argument("matrix can't add: size mismatch");
}
//std::unique_ptr<matrix<T>> tmp = std::make_unique<matrix<T>>(rows, cols);
matrix<T> tmp(rows, cols);
for (int i = 0; i < rows * cols; i++) {
tmp.element[i] = element[i] + x.element[i];
}
return tmp;
}
template <class T>
matrix<T> matrix<T>::operator-(const matrix<T>& x) const {
if (rows != x.rows || cols != x.cols) {
throw std::invalid_argument("matrix can't minus: size mismatch");
}
//std::unique_ptr<matrix<T>> tmp = std::make_unique<matrix<T>>(rows, cols);
matrix<T> tmp(rows, cols);
for (int i = 0; i < rows * cols; i++) {
tmp.element[i] = element[i] - x.element[i];
}
return tmp;
}
template <class T>
matrix<T> matrix<T>::operator*(const matrix<T>& x) const {
if (cols != x.rows) {
throw std::invalid_argument("matrix can't times: size mismatch");
}
T* t = new T[rows * x.cols];
matrix<T> tmp(rows, x.cols, t);
//* is difficult : <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= x.cols; j++) {
tmp(i, j) = times(i, j, *this, x);
}
}
return tmp;
}
template <class T>
matrix<T>& matrix<T>::operator+=(const matrix<T>& x) {
if (rows != x.rows || cols != x.cols) {
throw std::invalid_argument("matrix += error: size mismatch");
}
for (int i = 0; i < rows * cols; i++) {
element[i] += x.element[i];
}
return *this;
}
template <class T>
std::ostream& operator<<(std::ostream& os, const matrix<T>& x) {
for (int i = 0; i < x.rows; i++) {
for (int j = 0; j < x.cols; j++) {
os << x.element[i * x.cols + j] << " ";
}
os << std::endl;
}
return os;
}
template <class T>
matrix<T>& matrix<T>::transpose() {
T* telement = new T[rows * cols];
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= cols; j++) {
telement[(j - 1) * rows + i - 1] = (*this)(i, j);
}
}
delete[] element;
std::swap(rows, cols);
element = telement;
return *this;
}

View File

@@ -0,0 +1,98 @@
#pragma once
#include <iostream>
#include "stack.h"
template <class T>
class arrayStack : public Stack<T> {
private:
T* stack;
int stackTop;
int length;
public:
arrayStack(int initialCapacity = 10);
virtual ~arrayStack() { delete[] stack; }
virtual bool empty() const override { return stackTop == -1; }
virtual int size() const override { return stackTop + 1; }
virtual T& top() override;
virtual void pop() override;
virtual void push(const T& theElement) override;
virtual void show() const;
arrayStack& operator=(const arrayStack& x);
arrayStack(const arrayStack& x);
};
template <class T>
arrayStack<T>::arrayStack(int initialCapacity) {
length = initialCapacity;
if(length < 0) {
throw std::invalid_argument("Initial Error: Length < 0");
length = 1;
}
stack = new T[length];
stackTop = -1;
}
template <class T>
T& arrayStack<T>::top() {
if (stackTop == -1) throw std::invalid_argument("Top Error: No Element in Stack");
return stack[stackTop];
}
template <class T>
void arrayStack<T>::pop() {
if (stackTop == -1) throw std::invalid_argument("Pop Error: Empty Stack");
if constexpr (!std::is_trivially_destructible<T>::value) {
stack[stackTop].~T(); // ֻ<><D6BB><EFBFBD><EFBFBD> T <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>ŵ<EFBFBD><C5B5><EFBFBD>
}
stackTop--;
}
template <class T>
void arrayStack<T>::push(const T& theElement) {
if(stackTop == length - 1){
T* tmp = new T[length * 2];
for(int i = 0; i < length; i++){
tmp[i] = stack[i];
}
delete[] stack;
stack = tmp;
length *= 2;
}
stackTop++;
stack[stackTop] = theElement;
}
template <class T>
arrayStack<T>::arrayStack(const arrayStack& x) {
length = x.length;
stackTop = x.stackTop;
stack = new T[length];
for (int i = 0; i <= stackTop; ++i) {
stack[i] = x.stack[i];
}
}
template <class T>
arrayStack<T>& arrayStack<T>::operator=(const arrayStack<T>& x) {
if (this != &x) {
delete[] stack;
length = x.length;
stackTop = x.stackTop;
stack = new T[length];
for (int i = 0; i <= stackTop; ++i) {
stack[i] = x.stack[i];
}
}
return *this;
}
template <class T>
void arrayStack<T>::show() const {
for (int i = 0; i <= stackTop; i++) {
std::cout << stack[i] << " ";
}
std::cout << std::endl;
}

43
Stack/arrayStack/main.cpp Normal file
View File

@@ -0,0 +1,43 @@
#include <iostream>
#include "stack.h"
#include "arraystack.h"
using namespace std;
/*
TO TEST:
private:
T* stack;
int stackTop;
int length;
public:
arrayStack(int initialCapacity = 10);
virtual ~arrayStack() { delete[] stack; }
virtual bool empty() const override { return stackTop == -1; }
virtual int size() const override { return stackTop + 1; }
virtual T& top() override;
virtual void pop() override;
virtual void push(const T& theElement) override;
arrayStack& operator=(const arrayStack& x);
arrayStack(const arrayStack& x);
*/
int main(){
arrayStack<char> s(10);
s.push('a');
s.show();
s.push('b');
s.show();
s.push('c');
s.show();
s.pop();
s.show();
return 0;
}
/*
a
a b
a b c
a b
*/

13
Stack/arrayStack/stack.h Normal file
View File

@@ -0,0 +1,13 @@
#pragma once
template <class T>
class Stack {
public:
virtual ~Stack(){}
virtual bool empty() const = 0;
virtual int size() const = 0;
virtual T& top() = 0;
virtual void pop() = 0;
virtual void push(const T& theElement) = 0;
virtual void show() const = 0;
};