2020/12/18第三次上机

博客记录了构造函数和拷贝构造函数的多次调用情况,如多次出现“Constructor called”和“Copy constructor called”,随后记录了多次析构函数调用,出现多个“Destructor”。

T1

#include<iostream>
using namespace std;
class Fraction {   //数据成员,访问控制属性默认是私有
    int m_numerator = 0; //  分子默认为0;  C++11
    int m_denominator = 1; //分母默认为1;
public://公有成员函数
    Fraction(int above = 0, int below = 1) : m_numerator(above), m_denominator(below) {
        cout << "Constructor called" << endl;
    }
    Fraction(const Fraction& rhs) : m_numerator(rhs.m_numerator),      m_denominator(rhs.m_denominator) {
        cout << "Copy constructor called" << endl;
    }
    ~Fraction(){
        cout << "Dead" << endl;
    }
    void divide(){
        int m = gcd(m_numerator, m_denominator);
        m_numerator /= m;
        m_denominator /= m;
    };
    int getnumerator() const {
        return m_numerator;
    };
    int getdenominator() const{
        return m_denominator;
    };
    Fraction operator /(const Fraction&b){
        Fraction f(m_numerator*b.getdenominator(),m_denominator*b.getnumerator());
        f.divide();
        return f;
    }
   ostream& operator << (ostream& os,const Fraction &a){
    return os << a.m_numerator << " / " << a.m_denominator << endl;
}
private:
    int gcd(int a,int b){
        return b == 0?a:gcd(b,a%b);
    }
};

Fraction divide1(const Fraction& divident, const Fraction& divisor) {
        return Fraction(divident.getnumerator() * divisor.getdenominator(),     divident.getdenominator() * divisor.getnumerator());
}
Fraction divide2(Fraction divident, Fraction divisor) {
    Fraction result(divident.getnumerator() * divisor.getdenominator(),      divident.getdenominator() * divisor.getnumerator());
    return result;
}
Fraction makeCommon(const Fraction&a,const Fraction&b){
    Fraction f(a.getnumerator()*b.getdenominator() + a.getdenominator()*b.getnumerator(),a.getdenominator() * b.getdenominator());
    f.divide();
    return f;
}
int main(){
    Fraction f1(2,4);
    cout << f1;
    f1.divide();
    cout << f1;
    Fraction f2(4,5);
    Fraction f = makeCommon(f1, f2);
    cout << f;
    Fraction ff = f1 / f2;
    cout << ff;
    return 0;
}

在这里插入图片描述
Constructor called
Copy constructor called
Constructor called
Constructor called
Constructor called
Constructor called
Copy constructor called
Copy constructor called
Constructor called
Destructor
Destructor
Destructor
Destructor
Destructor
Destructor
Destructor
Destructor
Destructor

T2

#include<iostream>
#include<algorithm>
using namespace std;
int a[5] = { 19,67,24,11,17 }, b[5] = { 2,3,9,17,59 };
int find_k_byorder(int a[],int len,int k){
    for(int i = 0;i < len;++i){
        if(a[i] == k) return i;
    }
    return -1;
}
int binary_search_k(int a[],int len,int k){
    sort(a,a + len);
    int l = 0,r = len;
    while (l <= r) {
        int mid = (l + r) >> 1;
        if(a[mid] == k) return mid;
        else if(a[mid] > k) r = mid-1;
        else l = mid + 1;
    }
    return l;
}
int main(){
    cout << find_k_byorder(a,5,17) << endl;
    cout << find_k_byorder(b,5,17) << endl;
    cout << binary_search_k(a,5,17) << endl;
    cout << binary_search_k(b,5,17) << endl;
    return 0;
}

#include <cmath>
#include <vector>
#include <iostream>
using namespace std;
int a[5] = { 19,67,24,11,17 }, b[5] = { 2,3,9,17,59 };
void select_sort(vector<int>& t)
{
    int len = t.size();
    for (int i = 0;i < len;++i)
    {
        int k = t[i], p = i;
        for (int j = i;j < len;++j)
        {
            if (t[j] < k)
            {
                k = t[j];
                p = j;
            }
        }
        swap(t[i], t[p]);
    }
}
bool isPrime(int k) {
    if (k == 2) return true;
    if ((k & 1) == 0) return false;
    for (int i = 2;i <= sqrt(k);++i) {
        if (k % i == 0) return false;
    }
    return true;
}
int main() {
    vector<int> t;
    for (int i = 0;i < 5;++i) {
        if (isPrime(a[i])) t.push_back(a[i]);
    }
    for (int i = 0;i < 5;++i) {
        if (isPrime(b[i]) && b[i] != 17) t.push_back(b[i]);
    }
    select_sort(t);
    cout << "Increase:";
    for (vector<int>::iterator it = t.begin();it != t.end();++it) {
        cout << *it << " ";
    }
    cout << endl;
    return 0;
}

T3

#include<cmath>
#include<iostream>
using namespace std;
const double PI = 3.1415926;
class Circle;
class Point {
    double m_x = 0, m_y = 0;
    friend class Circle;
    friend double dist(const Point&,const Point&);
public:
    Point(double x=0, double y=0) : m_x(x), m_y(y) {
        cout << "Constructor of Point" << endl;
    }
    Point(const Point &p) :m_x(p.m_x), m_y(p.m_y) {
        cout << "Copy constructor of Point" << endl;
    }
    ~Point() {
        cout << "Destructor of Point" << endl;
    }
    double getMX(){
        return m_x;
    }
    double getMY(){
        return m_y;
    }
};
class Circle {
    Point m_center;
    double m_radius = 1.0;
public:
    Circle(double r=1, const Point &p=Point()) :m_center(p), m_radius(r) {                 cout << "Constructor of Circle" << endl;
    }
    ~Circle() {
        cout << "Destructor of Circle" << endl;
    }
    double getX(){
        return m_center.getMX();
    }
    double getY(){
        return m_center.getMY();
    }
    double area(){
        return PI*m_radius*m_radius;
    }
    double circumference(){
        return 2*PI*m_radius;
    }
};
double dist(const Point&a,const Point&b){
    return sqrt((a.m_x - b.m_x)*(a.m_x - b.m_x) + (a.m_y - b.m_y)*(a.m_y - b.m_y));
}
int main() {
    Circle a(2, Point(1, 3));
    cout << "(" << a.getX() << "," << a.getY() << ")" << endl;
    Point c(1,2);
    Point b(3,5);
    cout << dist(c, b) << endl;
    
    cout << a.area() << " " << a.circumference() <<endl;
    cout << "end" << endl;
    return 0;
}

需求响应动态冰蓄冷系统与需求响应策略的优化研究(Matlab代码实现)内容概要:本文围绕“需求响应动态冰蓄冷系统与需求响应策略的优化研究”展开,基于Matlab代码实现,重点探讨了冰蓄冷系统在电力需求响应背景下的动态建模与优化调度策略。研究结合实际电力负荷与电价信号,构建系统能耗模型,利用优化算法对冰蓄冷系统的运行策略进行求解,旨在降低用电成本、平衡电网负荷,并提升能源利用效率。文中还提及该研究为博士论文复现,涉及系统建模、优化算法应用与仿真验证等关键技术环节,配套提供了完整的Matlab代码资源。; 适合人群:具备一定电力系统、能源管理或优化算法基础,从事科研或工程应用的研究生、高校教师及企业研发人员,尤其适合开展需求响应、综合能源系统优化等相关课题研究的人员。; 使用场景及目标:①复现博士论文中的冰蓄冷系统需求响应优化模型;②学习Matlab在能源系统建模与优化中的具体实现方法;③掌握需求响应策略的设计思路与仿真验证流程,服务于科研项目、论文写作或实际工程方案设计。; 阅读建议:建议结合提供的Matlab代码逐模块分析,重点关注系统建模逻辑与优化算法的实现细节,按文档目录顺序系统学习,并尝试调整参数进行仿真对比,以深入理解不同需求响应策略的效果差异。
综合能源系统零碳优化调度研究(Matlab代码实现)内容概要:本文围绕“综合能源系统零碳优化调度研究”,提供了基于Matlab代码实现的完整解决方案,重点探讨了在高比例可再生能源接入背景下,如何通过优化调度实现零碳排放目标。文中涉及多种先进优化算法(如改进遗传算法、粒子群优化、ADMM等)在综合能源系统中的应用,涵盖风光场景生成、储能配置、需求响应、微电网协同调度等多个关键技术环节,并结合具体案例(如压缩空气储能、光热电站、P2G技术等)进行建模与仿真分析,展示了从问题建模、算法设计到结果验证的全流程实现过程。; 适合人群:具备一定电力系统、能源系统或优化理论基础,熟悉Matlab/Simulink编程,从事新能源、智能电网、综合能源系统等相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①开展综合能源系统低碳/零碳调度的科研建模与算法开发;②复现高水平期刊(如SCI/EI)论文中的优化模型与仿真结果;③学习如何将智能优化算法(如遗传算法、灰狼优化、ADMM等)应用于实际能源系统调度问题;④掌握Matlab在能源系统仿真与优化中的典型应用方法。; 阅读建议:建议结合文中提供的Matlab代码与网盘资源,边学习理论模型边动手调试程序,重点关注不同优化算法在调度模型中的实现细节与参数设置,同时可扩展应用于自身研究课题中,提升科研效率与模型精度。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值