-
Bio
#include <iostream> #include <fstream> #include <string> #include <vector> #include <list> #include <map> #include <set> #include <algorithm> #include <memory> #include <stdexcept> #include <functional> #include <thread> #include <mutex> #include <condition_variable> #include <chrono> #include <future> #include <random> #include <complex> #include <cmath> #include <ctime> #include <cctype> #include <cstring>
// 前置声明 class Shape; class Circle; class Rectangle; class Triangle;
// 1. 基础类和继承体系 // 抽象基类:形状 class Shape { protected: std::string color; public: Shape(const std::string& c) : color(c) {} virtual ~Shape() = default;
// 纯虚函数,使Shape成为抽象类 virtual double area() const = 0; virtual double perimeter() const = 0; virtual void draw() const = 0; // 虚函数 virtual std::string getType() const { return "Shape"; } // 普通成员函数 std::string getColor() const { return color; } // 静态函数 static void printMessage(const std::string& msg) { std::cout << "Shape Message: " << msg << std::endl; }
};
// 派生类:圆形 class Circle : public Shape { private: double radius; public: Circle(const std::string& c, double r) : Shape(c), radius(r) { if (radius <= 0) { throw std::invalid_argument("Radius must be positive"); } }
double area() const override { return M_PI * radius * radius; } double perimeter() const override { return 2 * M_PI * radius; } void draw() const override { std::cout << "Drawing a circle with radius " << radius << " and color " << color << std::endl; } std::string getType() const override { return "Circle"; } double getRadius() const { return radius; }
};
// 派生类:矩形 class Rectangle : public Shape { private: double width; double height; public: Rectangle(const std::string& c, double w, double h) : Shape(c), width(w), height(h) { if (width <= 0 || height <= 0) { throw std::invalid_argument("Width and height must be positive"); } }
double area() const override { return width * height; } double perimeter() const override { return 2 * (width + height); } void draw() const override { std::cout << "Drawing a rectangle with width " << width << ", height " << height << " and color " << color << std::endl; } std::string getType() const override { return "Rectangle"; } double getWidth() const { return width; } double getHeight() const { return height; }
};
// 派生类:三角形(多重继承示例) class Triangle : public Shape { private: double a, b, c; // 三条边 public: Triangle(const std::string& c, double side1, double side2, double side3) : Shape(c), a(side1), b(side2), c(side3) { // 验证是否能构成三角形 if (a <= 0 || b <= 0 || c <= 0 || a + b <= c || a + c <= b || b + c <= a) { throw std::invalid_argument("Invalid triangle sides"); } }
double area() const override { // 使用海伦公式 double s = (a + b + c) / 2; return std::sqrt(s * (s - a) * (s - b) * (s - c)); } double perimeter() const override { return a + b + c; } void draw() const override { std::cout << "Drawing a triangle with sides " << a << ", " << b << ", " << c << " and color " << color << std::endl; } std::string getType() const override { return "Triangle"; }
};
// 2. 模板类示例:动态数组 template<typename T> class DynamicArray { private: T* data; size_t size; size_t capacity;
// 扩容函数 void resize() { capacity *= 2; T* newData = new T[capacity]; for (size_t i = 0; i < size; ++i) { newData[i] = data[i]; } delete[] data; data = newData; }
public: DynamicArray() : data(nullptr), size(0), capacity(10) { data = new T[capacity]; }
~DynamicArray() { delete[] data; } // 拷贝构造函数 DynamicArray(const DynamicArray& other) : size(other.size), capacity(other.capacity) { data = new T[capacity]; for (size_t i = 0; i < size; ++i) { data[i] = other.data[i]; } } // 移动构造函数 DynamicArray(DynamicArray&& other) noexcept : data(other.data), size(other.size), capacity(other.capacity) { other.data = nullptr; other.size = 0; other.capacity = 0; } // 拷贝赋值运算符 DynamicArray& operator=(const DynamicArray& other) { if (this != &other) { delete[] data; size = other.size; capacity = other.capacity; data = new T[capacity]; for (size_t i = 0; i < size; ++i) { data[i] = other.data[i]; } } return *this; } // 移动赋值运算符 DynamicArray& operator=(DynamicArray&& other) noexcept { if (this != &other) { delete[] data; data = other.data; size = other.size; capacity = other.capacity; other.data = nullptr; other.size = 0; other.capacity = 0; } return *this; } // 添加元素 void add(const T& value) { if (size >= capacity) { resize(); } data[size++] = value; } // 访问元素 T& operator[](size_t index) { if (index >= size) { throw std::out_of_range("Index out of range"); } return data[index]; } const T& operator[](size_t index) const { if (index >= size) { throw std::out_of_range("Index out of range"); } return data[index]; } // 获取大小 size_t getSize() const { return size; } // 迭代器支持 T* begin() { return data; } const T* begin() const { return data; } T* end() { return data + size; } const T* end() const { return data + size; }
};
// 3. 函数模板示例:排序和查找 template<typename T> void bubbleSort(DynamicArray<T>& arr) { size_t n = arr.getSize(); for (size_t i = 0; i < n - 1; ++i) { for (size_t j = 0; j < n - i - 1; ++j) { if (arr[j] > arr[j + 1]) { std::swap(arr[j], arr[j + 1]); } } } }
template<typename T> int linearSearch(const DynamicArray<T>& arr, const T& value) { for (size_t i = 0; i < arr.getSize(); ++i) { if (arr[i] == value) { return static_cast<int>(i); } } return -1; }
// 4. 文件操作类 class FileHandler { private: std::string filename; std::fstream file; public: FileHandler(const std::string& fn) : filename(fn) {}
~FileHandler() { if (file.is_open()) { file.close(); } } bool openRead() { file.open(filename, std::ios::in); return file.is_open(); } bool openWrite() { file.open(filename, std::ios::out | std::ios::trunc); return file.is_open(); } bool writeLine(const std::string& line) { if (!file.is_open() || !file.good()) { return false; } file << line << std::endl; return true; } bool readLine(std::string& line) { if (!file.is_open() || !file.good()) { return false; } return static_cast<bool>(std::getline(file, line)); } void close() { if (file.is_open()) { file.close(); } }
};
// 5. 线程安全队列 template<typename T> class ThreadSafeQueue { private: std::list<T> queue; mutable std::mutex mtx; std::condition_variable cv; public: void push(const T& value) { std::lock_guardstd::mutex lock(mtx); queue.push_back(value); cv.notify_one(); }
void push(T&& value) { std::lock_guard<std::mutex> lock(mtx); queue.push_back(std::move(value)); cv.notify_one(); } bool pop(T& value) { std::unique_lock<std::mutex> lock(mtx); cv.wait(lock, [this]{ return !queue.empty(); }); value = std::move(queue.front()); queue.pop_front(); return true; } bool tryPop(T& value) { std::lock_guard<std::mutex> lock(mtx); if (queue.empty()) { return false; } value = std::move(queue.front()); queue.pop_front(); return true; } size_t size() const { std::lock_guard<std::mutex> lock(mtx); return queue.size(); } bool empty() const { std::lock_guard<std::mutex> lock(mtx); return queue.empty(); }
};
// 6. 异常类示例 class CustomException : public std::exception { private: std::string message; public: explicit CustomException(const std::string& msg) : message(msg) {}
const char* what() const noexcept override { return message.c_str(); }
};
// 7. 智能指针示例 class Resource { public: Resource() { std::cout << "Resource acquired" << std::endl; } ~Resource() { std::cout << "Resource released" << std::endl; } void use() const { std::cout << "Resource in use" << std::endl; } };
// 8. 数学工具类 class MathUtils { public: // 模板元编程:编译期计算阶乘 template<unsigned int N> struct Factorial { static constexpr unsigned long long value = N * Factorial<N-1>::value; };
template<> struct Factorial<0> { static constexpr unsigned long long value = 1; }; // 复数运算 static std::complex<double> addComplex(const std::complex<double>& a, const std::complex<double>& b) { return a + b; } // 生成随机数 static int generateRandom(int min, int max) { static std::random_device rd; static std::mt19937 gen(rd()); std::uniform_int_distribution<> dis(min, max); return dis(gen); }
};
// 9. 主函数 int main() { try { // 1. 测试形状类 std::cout << "=== Testing Shapes ===" << std::endl; std::vectorstd::unique_ptr> shapes; shapes.push_back(std::make_unique<Circle>("Red", 5.0)); shapes.push_back(std::make_unique<Rectangle>("Blue", 4.0, 6.0)); shapes.push_back(std::make_unique<Triangle>("Green", 3.0, 4.0, 5.0));
for (const auto& shape : shapes) { std::cout << shape->getType() << ": " << "Area=" << shape->area() << ", " << "Perimeter=" << shape->perimeter() << std::endl; shape->draw(); } // 2. 测试动态数组 std::cout << "\n=== Testing DynamicArray ===" << std::endl; DynamicArray<int> intArray; for (int i = 10; i >= 1; --i) { intArray.add(i); } std::cout << "Original array: "; for (int num : intArray) { std::cout << num << " "; } std::cout << std::endl; bubbleSort(intArray); std::cout << "Sorted array: "; for (int num : intArray) { std::cout << num << " "; } std::cout << std::endl; int searchValue = 5; int index = linearSearch(intArray, searchValue); if (index != -1) { std::cout << "Value " << searchValue << " found at index " << index << std::endl; } else { std::cout << "Value " << searchValue << " not found" << std::endl; } // 3. 测试文件操作 std::cout << "\n=== Testing File Handling ===" << std::endl; FileHandler file("test.txt"); if (file.openWrite()) { file.writeLine("Hello, World!"); file.writeLine("This is a test file."); file.close(); } else { std::cerr << "Failed to open file for writing!" << std::endl; } if (file.openRead()) { std::string line; while (file.readLine(line)) { std::cout << "Read: " << line << std::endl; } file.close(); } else { std::cerr << "Failed to open file for reading!" << std::endl; } // 4. 测试线程安全队列 std::cout << "\n=== Testing ThreadSafeQueue ===" << std::endl; ThreadSafeQueue<int> tsQueue; // 生产者线程 auto producer = [&tsQueue]() { for (int i = 0; i < 5; ++i) { tsQueue.push(i); std::cout << "Produced: " << i << std::endl; std::this_thread::sleep_for(std::chrono::milliseconds(200)); } }; // 消费者线程 auto consumer = [&tsQueue]() { int value; for (int i = 0; i < 5; ++i) { tsQueue.pop(value); std::cout << "Consumed: " << value << std::endl; } }; std::thread t1(producer); std::thread t2(consumer); t1.join(); t2.join(); // 5. 测试智能指针 std::cout << "\n=== Testing Smart Pointers ===" << std::endl; { std::shared_ptr<Resource> sharedRes = std::make_shared<Resource>(); sharedRes->use(); std::shared_ptr<Resource> sharedRes2 = sharedRes; std::cout << "Shared count: " << sharedRes.use_count() << std::endl; } // 资源自动释放 // 6. 测试数学工具类 std::cout << "\n=== Testing MathUtils ===" << std::endl; std::cout << "5! = " << MathUtils::Factorial<5>::value << std::endl; std::complex<double> c1(3.0, 4.0); std::complex<double> c2(1.0, 2.0); std::complex<double> sum = MathUtils::addComplex(c1, c2); std::cout << "Complex sum: " << sum << std::endl; std::cout << "Random number between 1 and 100: " << MathUtils::generateRandom(1, 100) << std::endl; // 7. 测试异常处理 std::cout << "\n=== Testing Exceptions ===" << std::endl; try { Circle invalidCircle("Purple", -1.0); } catch (const std::invalid_argument& e) { std::cerr << "Exception caught: " << e.what() << std::endl; } try { throw CustomException("This is a custom exception"); } catch (const CustomException& e) { std::cerr << "Custom exception caught: " << e.what() << std::endl; } catch (const std::exception& e) { std::cerr << "Standard exception caught: " << e.what() << std::endl; } catch (...) { std::cerr << "Unknown exception caught" << std::endl; } } catch (const std::exception& e) { std::cerr << "Fatal error: " << e.what() << std::endl; return 1; } catch (...) { std::cerr << "Fatal unknown error" << std::endl; return 1; } return 0;
}
-
Accepted Problems
- Y1
- Y10
- Y100
- Y101
- P5
- Y104
- Y108
- Y11
- Y111
- Y114
- Y117
- Y12
- Y122
- Y13
- Y132
- Y137
- Y138
- Y14
- Y141
- Y146
- Y147
- Y15
- Y151
- Y153
- Y154
- Y157
- Y159
- Y16
- Y160
- Y161
- Y162
- Y163
- Y167
- Y168
- Y17
- Y170
- Y173
- Y174
- Y178
- Y18
- Y185
- Y19
- Y192
- Y193
- Y194
- Y198
- Y2
- Y20
- Y203
- P122
- Y209
- Y21
- Y215
- Y216
- Y22
- Y220
- Y223
- Y227
- Y23
- Y234
- Y238
- Y239
- Y24
- Y240
- Y241
- Y80
- Y83
- Y9
- Y92
- Y98
- Y99
- Z2019A
- Z0204
- Z0311
- Z0312
- P878
- Z0834
- Z0210
- Z0321
- Z0322
- Z0325
- Z0328
- Z0331
- Z0333
- Z0334
- Z0408
- Z1029
- Z0314
- Z1919
- Z0107
- Z5001
- S0002
- P950
- Z0102
- P952
- Z0099
- Z0105
- Z0201
- Z0202
- Z0412
- Z0512
- Z0513
- Z0514
- P1064
- P1065
- Z221017
- P1268
- P1272
- P1277
- P1291
- P1388
- P1393
- P1395
- P1501
- P1502
- P1504
- P1505
- P1506
- P1507
- P1508
- P1510
- P1511
- Y1391
- Y500
- P1980
- P2031
- P2050
- P2051
- P2072
- P2093
- P2154
- P2185
- P2211
- P2318
- P2141
- P1137
- P1138
- P1139
- P1140
- P1145
- P1147
- GC0101
- P2404
- P2492
- GC0123
- GC01
- GC04
- GC07
- T1092
- GC16
- GC19
- GC02
- GC03
- GC05
- GC06
- GC08
- GC11
- GC12
- GC14
- GC17
- GC18
- GC20
- GC21
- GC0110
- GC0107
- GC22
- GC23
- GC24
- GC0104
- GC0105
- GC0106
- GC0108
- GC0109
- GC0111
- GC0112
- GC0114
- GC0115
- GC0117
- GC0118
- GC0120
- GC0121
- A0432
- A0556
- P2590
- A0462
- A0461
- A0431
- A0439
- P2687
- A0584
- A0588
- A0617
- A0621
- P2721
- P2765
-
Recent Activities
- 李牧易暑假训练 Assignment
- AShui0709堂训 Assignment
- AShui0708堂训 Assignment
- AShui0707堂训 Assignment
- AShui0706堂训 Assignment
- AShui0705堂训 Assignment
- 周周【周六9:00】6月21日 二维数组 Assignment
- 李牧易-6月14日作业 Assignment
- 周周【周六9:00】数据结构练习 Assignment
- 周周【周六9:00】5月17日 递归 Assignment
- 周周【周六9:00】5月10日 结构体2 Assignment
- 周周 【周六9:00】 4月26日 结构体 Assignment
- 周周 【周六9:00】 4月19日 排序三刷 Assignment
- 周周【周六9:00】4月5日 Assignment
- 周周【周六9:00】3月29日 Assignment
- 周周【周六9:00】3月22日 Assignment
- 周周【周六9:00】3月15日 Assignment
- 周周【周六9点】3月8日 Assignment
- 周周【周六9点】 3月1日作业 Assignment
- 【周六9:00】 12月28日作业 Assignment
- 【周六9:00】 12月14日作业 Assignment
- c++240824【周天10:30】 12月8日作业 【三刷】 Assignment
- 排序 Assignment
- c++240615班(11月测试) OI
- 【周六9:00】 11月23日 Assignment
- 【周六9:00】 11月16日 Assignment
- 【周六9:00】课堂小测 ACM/ICPC
- c++240615【周六9:00】11月2日作业 Assignment
- 【周六9:00】10月26日 Assignment
- 【周六9:00】10月19日作业 Assignment
- 【周天10:30】10月13日 Assignment
- 【周天10:30】 10月6日 Assignment
- 9月21日作业(周六早C++班袁) Assignment
- 【周六9:00】 9月21日作业 Assignment
- 【周六9:00】 9月16日作业 Assignment
- 【周天10:30】 9月22日作业 Assignment
- 【周天10:30】 9月8日作业 Assignment
- 【周六9:00】 9月7日作业 Assignment
- 【周六9:00】 8月31日作业 Assignment
- 【周六9:00】 8月24日作业 Assignment
- 【周六9:00】暑假作业 Assignment
- 【周六9:00】 team2 ACM/ICPC
- 【周六9:00】 team1 ACM/ICPC
- 【周六9:00】 ACM/ICPC
- 【周六9:00】 7月6日作业 Assignment
- 7月7日1340王熙然 Assignment
- 【周六9:00】 6月29日作业 Assignment
- 【周六9:00】 day2 Assignment
Problem Tags
- 一本通编程启蒙
- 133
- GESPC++ 二级
- 20
- 一维数组
- 8
- 递归
- 8
- 四年级题目-双重循环练习题
- 8
- 语法周赛
- 7
- T1
- 7
- 结构体
- 6
- 课课通
- 5
- 循环嵌套
- 4
- 客观题
- 4
- T2
- 4
- CSPJ模拟赛
- 4
- while循环
- 3
- 数学函数
- 3
- sort排序
- 3
- 条件分支
- 3
- 模拟
- 3
- 排序
- 3
- 入门必做-语言过关
- 3