楼主: 好汉歌3355
392 0

[休闲其它] 王道2024C++训练营62期|价值2万 [推广有奖]

  • 0关注
  • 0粉丝

学前班

90%

还不是VIP/贵宾

-

威望
0
论坛币
0 个
通用积分
0.0168
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
50 点
帖子
2
精华
0
在线时间
2 小时
注册时间
2025-3-12
最后登录
2025-3-16

楼主
好汉歌3355 发表于 2025-3-13 11:32:28 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
王道2024C++训练营62期|价值2万

以下为你提供一系列 2024 年较为实用的 C++ 训练内容,涵盖基础知识巩固、高级特性掌握、项目实践等多个方面。

基础知识巩固变量与数据类型
  • 练习目标:熟悉 C++ 中各种基本数据类型(如int、float、double、char等)的使用,掌握变量的声明、初始化和赋值操作。
  • 练习内容编写程序,输入两个整数,计算它们的和、差、积、商并输出。定义一个字符变量,将其初始化为大写字母 'A',然后将其转换为小写字母并输出。

cpp

#include int main() {    int num1, num2;    std::cout << : std::cin>> num1 >> num2;    std::cout << "和: " << num1 + num2 << std::endl;    std::cout << "差: " << num1 - num2 << std::endl;    std::cout << "积: " << num1 * num2 << std::endl;    if (num2 != 0) {        std::cout << "商: " << static_cast(num1) / num2 << std::endl;    } else {        std::cout << "除数不能为零。" << std::endl;    }    char ch = 'A';    ch = ch + 32;    std::cout << "转换后的小写字母: " << ch << std::endl;    return 0;}控制结构
  • 练习目标:掌握if-else、switch、for、while、do-while等控制结构的使用。
  • 练习内容编写一个程序,判断一个数是否为偶数。使用for循环输出 1 到 100 之间的所有奇数。

cpp

#include int main() {    int num;    std::cout << : std::cin>> num;    if (num % 2 == 0) {        std::cout << num << " 是偶数。" << std::endl;    } else {        std::cout << num << " 是奇数。" << std::endl;    }    std::cout << "1到100之间的奇数: ";    for (int i = 1; i <= 100; i += 2) {        std::cout << i << " ";    }    std::cout << std::endl;    return 0;}高级特性掌握面向对象编程
  • 练习目标:理解类、对象、继承、多态等面向对象编程的核心概念,掌握类的定义、成员函数的实现、构造函数和析构函数的使用。
  • 练习内容定义一个Rectangle类,包含长和宽两个属性,以及计算面积和周长的成员函数。从Rectangle类派生出一个Square类,并重写计算面积和周长的成员函数。

cpp

#include class Rectangle {protected:    double length;    double width;public:    Rectangle(double l, double w) : length(l), width(w) {}    double area() {        return length * width;    }    double perimeter() {        return 2 * (length + width);    }};class Square : public Rectangle {public:    Square(double side) : Rectangle(side, side) {}    double area() {        return length * length;    }    double perimeter() {        return 4 * length;    }};int main() {    Rectangle rect(5, 3);    std::cout << "矩形面积: " << rect.area() << std::endl;    std::cout << "矩形周长: " << rect.perimeter() << std::endl;    Square sq(4);    std::cout << "正方形面积: " << sq.area() << std::endl;    std::cout << "正方形周长: " << sq.perimeter() << std::endl;    return 0;}模板编程
  • 练习目标:掌握函数模板和类模板的定义和使用,理解模板的实例化过程。
  • 练习内容编写一个函数模板,实现两个数的交换。定义一个类模板Stack,实现栈的基本操作(如入栈、出栈、判断栈是否为空等)。

cpp

#include // 函数模板:交换两个数template void swap(T& a, T& b) {    T temp = a;    a = b;    b = temp;}// 类模板:栈template class Stack {private:    T data[size];    int top;public:    Stack() : top(-1) {}    bool isEmpty() {        return top == -1;    }    bool isFull() {        return top == size - 1;    }    void push(T value) {        if (!isFull()) {            data[++top] = value;        }    }    T pop() {        if (!isEmpty()) {            return data[top--];        }        return T();    }};int main() {    int a = 10, b = 20;    std::cout << "交换前: a = " << a << ", b = " << b << std::endl;    swap(a, b);    std::cout << "交换后: a = " << a << ", b = " << b << std::endl;    Stack<int, 5> stack;    stack.push(1);    stack.push(2);    stack.push(3);    while (!stack.isEmpty()) {        std::cout << stack.pop() << " ";    }    std::cout << std::endl;    return 0;}项目实践简单的图书管理系统
  • 项目目标:综合运用 C++ 的面向对象编程、文件操作等知识,实现一个简单的图书管理系统,包括图书的添加、删除、查询和显示等功能。
  • 项目步骤定义Book类,包含图书的基本信息(如书名、作者、ISBN 等)。定义Library类,负责图书的管理,包括添加、删除、查询和显示等操作。实现文件操作,将图书信息保存到文件中,并在程序启动时从文件中读取图书信息。

cpp

#include #include #include #include class Book {public:    std::string title;    std::string author;    std::string isbn;    Book(const std::string& t, const std::string& a, const std::string& i)        : title(t), author(a), isbn(i) {}};class Library {private:    std::vector books;    const std::string filename = "books.txt";    void saveToFile() {        std::ofstream file(filename);        if (file.is_open()) {            for (const auto& book : books) {                file << book.title << "," << book.author << "," << book.isbn << std::endl;            }            file.close();        }    }    void loadFromFile() {        std::ifstream file(filename);        if (file.is_open()) {            std::string line;            while (std::getline(file, line)) {                size_t pos1 = line.find(',');                size_t pos2 = line.find(',', pos1 + 1);                std::string title = line.substr(0, pos1);                std::string author = line.substr(pos1 + 1, pos2 - pos1 - 1);                std::string isbn = line.substr(pos2 + 1);                books.emplace_back(title, author, isbn);            }            file.close();        }    }public:    Library() {        loadFromFile();    }    void addBook(const Book& book) {        books.push_back(book);        saveToFile();    }    void displayBooks() {        for (const auto& book : books) {            std::cout << "书名: " << book.title << ", 作者: " << book.author << ", ISBN: " << book.isbn << std::endl;        }    }};int main() {    Library library;    Book book("C++ Primer", "Stanley Lippman", "978-0321714114");    library.addBook(book);    library.displayBooks();    return 0;}

这些训练内容从基础到高级,逐步深入,通过练习可以全面提升你对 C++ 的掌握程度。在练习过程中,要注重代码的可读性、可维护性和性能优化。


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:训练营 include double float 基础知识

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
扫码
拉您进交流群
GMT+8, 2026-1-28 03:55