OOP HomeWork

This commit is contained in:
e2hang
2025-08-11 00:01:30 +08:00
commit e8a5ca2363
119 changed files with 3187 additions and 0 deletions

39
oop_hw3/hw5/main.cpp Normal file
View File

@@ -0,0 +1,39 @@
#include <iostream>
#include <iomanip>
#include "vector.h"
using namespace std;
int main() {
Vector<int> test;
Vector<string> stest;
int m, n;
cin >> m >> n;
test = Vector<int>(m, n);
stest = Vector<string>(m, n);
for (int i = 0;i < m;i++) {
for (int j = 0;j < n;j++) {
cin >> test[i][j];
}
}
for (int i = 0;i < m;i++) {
for (int j = 0;j < n;j++) {
cin >> stest[i][j];
}
}
test[0][0] = -1;
stest[0][0] = "Hello, world!";
for (int i = 0;i < m;i++) {
for (int j = 0;j < n;j++) {
cout << setw(4) << test[i][j];
}
cout << endl;
}
for (int i = 0;i < m;i++) {
for (int j = 0;j < n;j++) {
cout << setw(20) << right << stest[i][j];
}
cout << endl;
}
return 0;
}

81
oop_hw3/hw5/vector.h Normal file
View File

@@ -0,0 +1,81 @@
#ifndef VECTOR_H
#define VECTOR_H
#include <iostream>
template<class T>
class Vector {
private:
T** p;
int hang;
int lie;
public:
Vector();
Vector(int m, int n);
Vector& operator=(const Vector& x);
T* operator[](int x);
void AlterElement(int m, int n, T x);
~Vector();
};
template<class T>
Vector<T>::Vector() {
p = nullptr;
hang = 0;
lie = 0;
}
template<class T>
Vector<T>::Vector(int m, int n) {
hang = m;
lie = n;
p = new T * [m];
for (int i = 0;i < m;i++) {
p[i] = new T[n];
}
}
template<class T>
Vector<T>::~Vector() {
if (p) {
for (int i = 0; i < hang; i++) {
delete[] p[i];
}
delete[] p;
}
}
template<class T>
Vector<T>& Vector<T>::operator=(const Vector& x) {
if (p) {
for (int i = 0; i < hang; i++) {
delete[] p[i];
}
delete[] p;
}
hang = x.hang;
lie = x.lie;
p = new T * [hang];
for (int i = 0; i < hang; i++) {
p[i] = new T[lie];
for (int j = 0; j < lie; j++) {
p[i][j] = x.p[i][j];
}
}
return *this;
}
template<class T>
T* Vector<T>::operator[](int x)
{
return p[x];
}
template<class T>
inline void Vector<T>::AlterElement(int m, int n, T x)
{
p[m][n] = x;
}
#endif // !VECTOR_H