OOP HomeWork
This commit is contained in:
74
oop_hw3/hw1/main.cpp
Normal file
74
oop_hw3/hw1/main.cpp
Normal file
@@ -0,0 +1,74 @@
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
using namespace std;
|
||||
class MyString {
|
||||
private:
|
||||
char* mpData;
|
||||
public:
|
||||
MyString(const char* pData = nullptr) {
|
||||
if (pData == nullptr) {
|
||||
mpData = new char[1];
|
||||
mpData[0] = '\0';
|
||||
}
|
||||
else {
|
||||
int len = strlen(pData);
|
||||
mpData = new char[len + 1];
|
||||
strcpy(mpData, pData);
|
||||
}
|
||||
|
||||
}
|
||||
MyString(const MyString& x) {
|
||||
int len = strlen(x.mpData);
|
||||
mpData = new char[len + 1];
|
||||
strcpy(mpData, x.mpData);
|
||||
}
|
||||
~MyString() {
|
||||
delete[] mpData;
|
||||
}
|
||||
MyString& operator=(const MyString& x) {
|
||||
if (this == &x) return *this;
|
||||
delete[] mpData;
|
||||
int len = strlen(x.mpData);
|
||||
mpData = new char[len + 1];
|
||||
strcpy(mpData, x.mpData);
|
||||
return *this;
|
||||
}
|
||||
MyString& operator+=(const MyString& x) {
|
||||
int len = strlen(mpData) + strlen(x.mpData);
|
||||
char* tmp = new char[len + 1];
|
||||
strcpy(tmp, mpData);
|
||||
strcat(tmp, x.mpData);
|
||||
delete[] mpData;
|
||||
mpData = tmp;
|
||||
return *this;
|
||||
}
|
||||
friend const MyString operator+(const MyString& x, const MyString& y) {
|
||||
int len = strlen(x.mpData) + strlen(y.mpData);
|
||||
char* tmp = new char[len + 1];
|
||||
strcpy(tmp, x.mpData);
|
||||
strcat(tmp, y.mpData);
|
||||
MyString r(tmp);
|
||||
delete[] tmp;
|
||||
return r;
|
||||
}
|
||||
friend ostream& operator<<(ostream& os, const MyString& x) {
|
||||
os << x.mpData;
|
||||
return os;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
char p[] = "Hello, world!";
|
||||
char q[] = "What are you doing?";
|
||||
char r[] = "How are you!";
|
||||
MyString a(p), b, c;
|
||||
b = MyString(q);
|
||||
c = b;
|
||||
MyString d;
|
||||
d = a + b;
|
||||
cout << d << endl;
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
oop_hw3/hw1/测试截图1.png
Normal file
BIN
oop_hw3/hw1/测试截图1.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 329 KiB |
91
oop_hw3/hw2/main.cpp
Normal file
91
oop_hw3/hw2/main.cpp
Normal file
@@ -0,0 +1,91 @@
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <iomanip>
|
||||
#define M_PI 3.14159265358979323846
|
||||
using namespace std;
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
class Fraction {
|
||||
private:
|
||||
int* p;
|
||||
public:
|
||||
Fraction():p(nullptr){}
|
||||
//<2F><><EFBFBD><EFBFBD>ֻ<EFBFBD>ܴ<EFBFBD><DCB4><EFBFBD>СΪ30<33><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
Fraction(int* x) {
|
||||
if (p) delete[] p;
|
||||
p = new int[30];
|
||||
for (int i = 0; i < 30 ;i++) {
|
||||
p[i] = x[i];
|
||||
}
|
||||
}
|
||||
Fraction(const Fraction& x) {
|
||||
if(p) delete[] p;
|
||||
p = new int[30];
|
||||
for (int i = 0; i < 30;i++) {
|
||||
p[i] = x.p[i];
|
||||
}
|
||||
}
|
||||
Fraction& operator=(const Fraction& x) {
|
||||
if(p) delete[] p;
|
||||
p = new int[30];
|
||||
for (int i = 0; i < 30;i++) {
|
||||
p[i] = x.p[i];
|
||||
}
|
||||
}
|
||||
friend ostream& operator<<(ostream& os, const Fraction& x) {
|
||||
for (int i = 0;i < 30;i++) {
|
||||
os << x.p[i] << " ";
|
||||
}
|
||||
return os;
|
||||
}
|
||||
~Fraction() {
|
||||
delete[] p;
|
||||
}
|
||||
void DuiYingFenshu(int x) {
|
||||
long long fenzi = 1, fenmu = 0;
|
||||
for (int i = x - 1; i >= 0; --i) {
|
||||
long long temp = fenzi;
|
||||
fenzi = p[i] * fenzi + fenmu;
|
||||
fenmu = temp;
|
||||
}
|
||||
double val = (double)fenzi / fenmu;
|
||||
double cha = M_PI - val;
|
||||
cout << setprecision(16);
|
||||
cout << "<EFBFBD><EFBFBD>" << x << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǣ<EFBFBD>" << fenzi << "/" << fenmu << endl;
|
||||
cout << "<EFBFBD><EFBFBD>" << x << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>С<EFBFBD><EFBFBD><EFBFBD>ǣ<EFBFBD>" << val << endl;
|
||||
cout << "<EFBFBD><EFBFBD> PI <20><><EFBFBD>" << cha << endl;
|
||||
}
|
||||
};
|
||||
//<2F><>һ<EFBFBD><D2BB>С<EFBFBD><D0A1>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ
|
||||
int* out(double x) {
|
||||
double t = x;
|
||||
double temp;
|
||||
int* tmp;
|
||||
tmp = new int[30];
|
||||
tmp[0] = floor(t);
|
||||
t -= tmp[0];
|
||||
for (int i = 1;i < 30;i++) {
|
||||
temp = 1 / (t);
|
||||
tmp[i] = floor(temp);
|
||||
temp -= tmp[i];
|
||||
t = temp;
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
|
||||
int main() {
|
||||
//<2F><><EFBFBD><EFBFBD>out<75><74><EFBFBD><EFBFBD>
|
||||
int* a;
|
||||
a = out(M_PI);
|
||||
for (int i = 0;i < 30;i++) {
|
||||
cout << a[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
Fraction test(a);
|
||||
for (int i = 1;i < 30;i++) {
|
||||
test.DuiYingFenshu(i);
|
||||
cout << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
BIN
oop_hw3/hw2/测试截图2.png
Normal file
BIN
oop_hw3/hw2/测试截图2.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 378 KiB |
33
oop_hw3/hw3/main.cpp
Normal file
33
oop_hw3/hw3/main.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
template <class T>
|
||||
void f(int m, int n) {
|
||||
T** p;
|
||||
p = new T* [m];
|
||||
for (int i = 0;i < m;i++) {
|
||||
p[i] = new T[n];
|
||||
}
|
||||
int cnt = 1;
|
||||
for (int i = 0;i < m;i++) {
|
||||
for (int j = 0;j < n;j++) {
|
||||
p[i][j] = cnt;
|
||||
cout << cnt << " ";
|
||||
cnt++;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
if (p) {
|
||||
for (int i = 0;i < m;i++) {
|
||||
delete[] p[i];
|
||||
}
|
||||
delete[] p;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int a, b;
|
||||
cin >> a >> b;
|
||||
f<int>(a, b);
|
||||
return 0;
|
||||
}
|
||||
BIN
oop_hw3/hw3/测试截图3.png
Normal file
BIN
oop_hw3/hw3/测试截图3.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 334 KiB |
35
oop_hw3/hw4/main.cpp
Normal file
35
oop_hw3/hw4/main.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class A {
|
||||
public:
|
||||
A() {
|
||||
data = -1;
|
||||
}
|
||||
A(int n) :data(n) {
|
||||
|
||||
}
|
||||
int& Data(){
|
||||
return data;
|
||||
}
|
||||
private:
|
||||
int data;
|
||||
};
|
||||
|
||||
auto g = [](int n) {
|
||||
A* p = new A[n];
|
||||
for (int i = 0;i < n;i++) {
|
||||
p[i].Data() = i + 1;
|
||||
}
|
||||
for (int i = n-1;i >= 0;i--) {
|
||||
cout << p[i].Data() << " ";
|
||||
}
|
||||
cout << endl;
|
||||
};
|
||||
|
||||
int main() {
|
||||
int a;
|
||||
cin >> a;
|
||||
g(a);
|
||||
return 0;
|
||||
}
|
||||
BIN
oop_hw3/hw4/测试截图4.png
Normal file
BIN
oop_hw3/hw4/测试截图4.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 287 KiB |
39
oop_hw3/hw5/main.cpp
Normal file
39
oop_hw3/hw5/main.cpp
Normal 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
81
oop_hw3/hw5/vector.h
Normal 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
|
||||
Reference in New Issue
Block a user