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

View File

@@ -0,0 +1,5 @@
#include "func.h"
int f1(int num) {
return g1(num * 2);
}

View File

@@ -0,0 +1,8 @@
#include "func.h"
int g1(int num) {
return num * num;
}
int g2(int num) {
return f1(num * 3);
}

View File

@@ -0,0 +1,7 @@
#include <iostream>
#include "func.h"
using namespace std;
int main(int argc, char * argv[]) {
cout << g2(10) << endl;
return 0;
}

9
oop_hw1/oop_hw1_1/func.h Normal file
View File

@@ -0,0 +1,9 @@
#pragma once
#ifndef FUNC_H
#define FUNC_H
int f1(int num);
int g1(int num);
int g2(int num);
#endif

View File

@@ -0,0 +1,14 @@
#include <iostream>
#include "print_this.h"
#include "print_that.h"
extern int print_count;
int main() {
#ifdef DO_THIS
print_this("Hello");
#else
print_that("Hello");
#endif
std :: cout << "Print called " << print_count << " times." << std::endl;
return 0;
}

View File

@@ -0,0 +1,7 @@
#include <iostream>
#include "print.h"
int print_count = 0;
void print(const std::string& s) {
++print_count;
std::cout << s << std::endl;
}

View File

@@ -0,0 +1,6 @@
#pragma once
#include <string>
#ifndef PRINT_H
#define PRINT_H
void print(const std::string& s);
#endif

View File

@@ -0,0 +1,5 @@
#include "print.h"
#include "print_that.h"
void print_that(const std::string& s) {
print("That: " + s);
}

View File

@@ -0,0 +1,7 @@
#pragma once
#include <string>
#ifndef PRINT_THAT_H
#define PRINT_THAT_H
void print_that(const std::string& s);
#endif

View File

@@ -0,0 +1,5 @@
#include "print.h"
#include "print_this.h"
void print_this(const std::string& s) {
print("This: " + s);
}

View File

@@ -0,0 +1,6 @@
#pragma once
#include <string>
#ifndef PRINT_THIS_H
#define PRINT_THIS_H
void print_this(const std::string& s);
#endif

View File

@@ -0,0 +1,17 @@
#include <iostream>
double average(int arr[], int size) {
int sum = 0;
for (int i = 0; i < size; ++i) sum += arr[i];
return (double)sum / size;
}
using namespace std;
int main() {
int n;
cin >> n;
int * a = new int[n];
for (int i = 0;i < n;i++) {
cin >> a[i];
}
cout << average(a, n);
return 0;
}

View File

@@ -0,0 +1,11 @@
#include <iostream>
#include "t_area.h"
using namespace std;
int main() {
double a, b, c;
cin >> a >> b >> c;
cout << triangle_area(a,b,c) << endl;
return 0;
}

View File

@@ -0,0 +1,5 @@
#include <cmath>
double triangle_area(double a, double b, double c) {
double s = (a + b + c) / 2;
return sqrt(s * (s - a) * (s - b) * (s - c));
}

View File

@@ -0,0 +1,8 @@
#pragma once
#ifndef tarea
#define tarea
double triangle_area(double a, double b, double c);
#endif // !tarea

View File

@@ -0,0 +1,7 @@
#pragma once
#ifndef testchar
#define testchar
bool is_digit(char ch);
#endif // !testchar

View File

@@ -0,0 +1,13 @@
#include <iostream>
#include "check_num.h"
using namespace std;
int main()
{
char ch;
for (int i = 0;i < 10;i++) {
cin >> ch;
cout << is_digit(ch)<<endl;
}
return 0;
}

View File

@@ -0,0 +1,3 @@
bool is_digit(char ch) {
return ch >= '0' && ch <= '9';
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 193 KiB

View File

@@ -0,0 +1,16 @@
#include <iostream>
#include "sort.h"
using namespace std;
int main() {
int d, e;
cin >> d >> e;
sort_two(d, e);
int a, b, c;
cin >> a >> b >> c;
sort_three(a, b, c);
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" << endl;
cout << d << e <<endl;
cout << a << b << c;
return 0;
}

View File

@@ -0,0 +1,9 @@
#include <algorithm>
void sort_two(int& a, int& b) {
if (a < b) std::swap(a, b);
}
void sort_three(int& a, int& b, int& c) {
if (a < b) std::swap(a, b);
if (a < c) std::swap(a, c);
if (b < c) std::swap(b, c);
}

7
oop_hw1/oop_hw1_6/sort.h Normal file
View File

@@ -0,0 +1,7 @@
#pragma once
#ifndef sort
#define sort
void sort_three(int& a, int& b, int& c);
void sort_two(int& d, int& e);
#endif // !sort

Binary file not shown.

After

Width:  |  Height:  |  Size: 220 KiB

14
oop_hw1/oop_hw1_7/fib.cpp Normal file
View File

@@ -0,0 +1,14 @@
#include <iostream>
long& smaller(long& a, long& b) { return (a < b) ? a : b; }
long& bigger(long& a, long& b) { return (a >= b) ? a : b; }
void fibe(int count) {
long a = 1, b = 1;
for (int i = 0; i < count; i++) {
long& min = smaller(a, b);
long& max = bigger(a, b);
min = a + b;
std::cout << max << " ";
}
std::cout << std::endl;
}

9
oop_hw1/oop_hw1_7/fib.h Normal file
View File

@@ -0,0 +1,9 @@
#pragma once
#ifndef fib
#define fib
long& smaller(long& a, long& b);
long& bigger(long& a, long& b);
void fibe(int count);
#endif // !fib

View File

@@ -0,0 +1,10 @@
#include <iostream>
#include "fib.h"
using namespace std;
int main() {
int n;
cin >> n;
fibe(n);
return 0;
}

View File

@@ -0,0 +1,43 @@
#include <iostream>
#include <string>
#include <vector>
class Pen {
public:
void write() {}
};
class FountainPen : public Pen {
public:
void refill() {}
};
class File {
public:
std::string name;
};
class Directory {
public:
std::vector<File> files;
};
class Printer {
public:
void print(const std::string& doc) {}
};
class Monitor {
public:
void display(const std::string& img) {}
};
class Sun {
public:
void shine() {}
};
class Moon {
public:
void reflect() {}
};

Binary file not shown.