C++一个总结初中物理的程序,全面总结,可以修改

当然,请看下面的代码:

#include 
#include 
#include 
#include 

// 物理概念抽象基类
class PhysicsConcept {
protected:
    std::string name;
    std::string description;

public:
    PhysicsConcept(const std::string& n, const std::string& d) : name(n), description(d) {}

    virtual void display() const = 0;
    virtual void saveToFile(std::ofstream& file) const = 0;
};

// 力和运动概念派生类
class ForceAndMotionConcept : public PhysicsConcept {
private:
    std::string formula;

public:
    ForceAndMotionConcept(const std::string& n, const std::string& d, const std::string& f)
        : PhysicsConcept(n, d), formula(f) {}

    void display() const override {
        std::cout << "概念: " << name << std::endl;
        std::cout << "描述: " << description << std::endl;
        std::cout << "公式: " << formula << std::endl;
        std::cout << std::endl;
    }

    void saveToFile(std::ofstream& file) const override {
        file << "ForceAndMotion" << std::endl;
        file << name << std::endl;
        file << description << std::endl;
        file << formula << std::endl;
    }
};

// 机械能概念派生类
class MechanicalEnergyConcept : public PhysicsConcept {
private:
    double potentialEnergy;
    double kineticEnergy;

public:
    MechanicalEnergyConcept(const std::string& n, const std::string& d, double pe, double ke)
        : PhysicsConcept(n, d), potentialEnergy(pe), kineticEnergy(ke) {}

    void display() const override {
        std::cout << "概念: " << name << std::endl;
        std::cout << "描述: " << description << std::endl;
        std::cout << "势能: " << potentialEnergy << " J" << std::endl;
        std::cout << "动能: " << kineticEnergy << " J" << std::endl;
        std::cout << std::endl;
    }

    void saveToFile(std::ofstream& file) const override {
        file << "MechanicalEnergy" << std::endl;
        file << name << std::endl;
        file << description << std::endl;
        file << potentialEnergy << std::endl;
        file << kineticEnergy << std::endl;
    }
};

// 初中物理知识总结类
class PhysicsSummary {
private:
    std::string topic;
    std::vector<PhysicsConcept*> concepts;

public:
    PhysicsSummary(const std::string& t) : topic(t) {}

    void addConcept(PhysicsConcept* concept) {
        concepts.push_back(concept);
    }

    void displaySummary() const {
        std::cout << "物理知识总结 - " << topic << std::endl;
        std::cout << "------------------------" << std::endl;

        if (concepts.empty()) {
            std::cout << "暂无总结内容。" << std::endl;
        } else {
            for (const auto& concept : concepts) {
                concept->display();
            }
        }
    }

    void saveToFile(const std::string& filename) const {
        std::ofstream file(filename);

        if (file.is_open()) {
            for (const auto& concept : concepts) {
                concept->saveToFile(file);
            }

            file.close();
            std::cout << "保存成功!" << std::endl;
        } else {
            std::cout << "保存文件失败。" << std::endl;
        }
    }

    ~PhysicsSummary() {
        for (const auto& concept : concepts) {
            delete concept;
        }
    }
};

int main() {
    // 创建一个物理知识总结对象
    PhysicsSummary physics("初中物理");

    // 添加力和运动概念
    PhysicsConcept* concept1 = new ForceAndMotionConcept("力和运动", "描述力和运动关系", "F = m * a");
    physics.addConcept(concept1);

    // 添加机械能概念
    PhysicsConcept* concept2 = new MechanicalEnergyConcept("机械能", "描述机械能概念", 100, 50);
    physics.addConcept(concept2);

    // 显示物理知识总结
    physics.displaySummary();

    // 将物理知识总结保存到文件
    physics.saveToFile("physics_summary.txt");

   

你可能感兴趣的:(C++,简介,牛逼程序,c++,开发语言,算法,学习,小程序)