当前位置:   article > 正文

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

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

当然,请看下面的代码:

#include <iostream>
#include <string>
#include <vector>
#include <fstream>

// 物理概念抽象基类
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");

   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/42193
推荐阅读
相关标签
  

闽ICP备14008679号