实验二

family_relationship.pro

#-------------------------------------------------
#
# Project created by QtCreator 2015-04-22T19:27:00
#
#-------------------------------------------------

QT       += core

QT       -= gui

TARGET = family_relationship
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app


SOURCES += main.cpp \
    father.cpp \
    mother.cpp \
    child.cpp

HEADERS += \
    father.h \
    mother.h \
    child.h


child.h

#ifndef CHILD_H
#define CHILD_H
#include <string>
using namespace std;
class Child
{
public:
    Child();
    string name;
    void answer();
    void callFather();
};
#endif // CHILD_H

father.h

#ifndef FATHER_H
#define FATHER_H
#include <string>
#include "child.h"
using namespace std;
class Father
{
public:
    Father();
    string name;
    Child child;
    void callChild();
    void answer();
};
#endif // FATHER_H

mother.h

#ifndef MOTHER_H
#define MOTHER_H
class Mother
{
public:
    Mother();
};
#endif // MOTHER_H

child.cpp

#include "child.h"
#include "iostream"
Child::Child():name("xiao Hua")
{
}
void Child::answer(){
    cout<<endl<<name<<" is here!";
}
void Child::callFather(){
    cout<<endl<<"I am calling my father!";
    cout<<endl<<"Father is not here!";
}

father.cpp

#include "father.h"
#include <iostream>
Father::Father()
    :name("Lao Hua")
{
}
void Father::callChild(){
    cout<<endl<<"I am calling my child!";
    child.answer();
}
void Father::answer(){
    cout<<endl<<name<<" is here waiting for you!";
}

mother.cpp

#include "mother.h"
Mother::Mother()
{
}

main.cpp

#include <QCoreApplication>
#include "father.h"
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    Father baba;
    baba.callChild();
    cout<<endl;
    return a.exec();
}

1、通过C++课本复习2.5节(自定义数据类型)、4.2,3,4节(类和对象基本语法、类的数据成员和成员函数、构造和析构函数、类的组合)、6.6节(字符串)、7.1,3,4节(类的继承相关核心)内容,通过在Qt环境新建non-Qt项目实现这些章节中的代码;如果没有带C++课本,可在网上搜索相关知识,通过在Qt环境新建non-Qt项目写出练习代码并运行通过,记录过程中出现的各种编译器报错和运行期错误。

 

2、上网搜索资料,研究#include语句中,引用头文件时使用<和"的区别;在网上搜索C++的namespace关键字的相关资料,归纳namespace关键字的用法,写示例代码运行。

注意:一定要运行通过,给出运行结果,而不能只是在网上找到了相关知识贴在下面。

 

  #include<> 引用的是编译器的类库路径里面的头文件。

  #include"" 引用的是你程序目录的相对路径中的头文件.

命名空间是用来阻止和重用代码的,因为不能排除变量名即标识符相同的情况,对于库来说,这个问题尤为严重。为了解决这个问题,引入了命名空间这个概念。这样当对象来自不同的地方但是名字相同的时候就不会含糊不清了,是一种将程序库名称封装起来的方法,它就像在各个程序中立起来一道道围墙。

    由于namespace的概念,使用C++标准程序的任何标识符时,可以有三种选择:1.直接指定标识符。2使用using关键字。3.使用using namespace std;

3、上网搜索“前置声明”,写练习代码,总结C++前置声明的语法、使用时的注意事项(包括为什么)、用途。

类A中用到了类B,而类B的声明出现在类A的后面。如果没有类B的前置说明,下面的程序将不同通过编译,编译器将会给出类似“缺少类型说明符”这样的出错提示

#include <iostream>

using namespace std;

 

class B;// 这是前置声明(Forward declaration)

class A

{

private:

        B* b;

public:

        A(B* b):b(b)

        {

        }

        …

};

class B

{

        …

};

 

// Main.cpp

#include "ForwardDeclaration.h"

int main(int argc, char** argv)

{

        B* b = new B();

        A* a = new A(b);

 

        delete a;

              delete b;

 

        return 0;

}

 

 

4、参考附件family_relationship项目,完成:

(1)为Child类增添代码,时Child也能呼叫Father。尝试若不使用前置声明,或者不使用指针数据成员,或者不初始化对象中的指针成员时,可能出现的各种编译器报错或运行期错误,根据课本第46页3.1.3节“程序调试”,找到运行期错误的发生位置和原因。在网上搜索复习构造函数相关知识,在构造函数中写实验代码,用实验结果回答一个问题:如果没有执行任何初始化,那么,对象中的成员变量会被默认初始化为怎样的值?

 

       如果不使用前置声明则会出现如下错误提示:显示Father类是一个没有被定义的类型,而且Child类内部没有father这一成员变量

       如果不使用指针数据成员则会显示如下错误提示:提示Father是一个不完整的类型,从而使得主函数调用Child类时找不到father这个成员变量

       如果不初始化对象中的指针成员时会显示如下出错信息:当程序执行到Child类的callfather()函数的father-〉answer()语句时因为主函数里面没有初始化对象中的指针成员则找不到father对象,因此就会出现如下错误。

       如果没有只执行任何初始化,那么对象中的成员变量会默认为空字符串

 

 

(2)写Mother类代码,探索是否只能够采用前置声明和指针数据成员相结合的编程技术,尝试用两种不同编程技术实现Mother类和Child类。

 

Mother.h:

#ifndef MOTHER_H

#define MOTHER_H

#include <string>

#include "child.h"

using namespace std;

class Mother

{

public:

   Mother();

   string name;

   Child child;

   void callchild();

   void answer();

};

#endif // MOTHER_H

Child.h:

#ifndef CHILD_H

#define CHILD_H

#include <string>

using namespace std;

class Father;

class Mother;

class Child

{

public:

   Child();

   Father * father;

   Mother * mother;

   string name;

   void answer();

   void callFather();

   void callmother();

};

#endif // CHILD_H

Main.h:

#include <QCoreApplication>

#include "father.h"

#include"mother.h"

#include <iostream>

#include "child.h"

using namespace std;

int main(int argc, char *argv[])

{

    QCoreApplicationa(argc, argv);

   Father * baba = new Father();

   Mother * mama = new Mother();

   Child kid = baba->child;

   Child kid1= mama->child;

   kid.father = baba;

   kid1.mother = mama;

   baba->callChild();

   kid.callFather();

    mama->callchild();

   kid1.callmother();

   cout<<endl;

   return a.exec();

}

 

 

 

 

 

 

 

(3)为Father、Mother、Child类编写合适的构造函数,使他们能够互相呼叫,记录可能出现的各种编译器报错或运行期错误。

部分代码:

#include "father.h"

#include <iostream>

#include "child.h"

#include "mother.h"

Father::Father()

   :name("Lao Hua")

{

   child=new Child(this,0);

   mother=new Mother(this,child);

   child->mother=mother;

}

 

void Father::callChild(){

   cout<<endl<<"I am calling my child!";

   child->answer();

}

 

void Father::answer(){

   cout<<endl<<name<<" is here waiting foryou!";

}

void Father::callWife(){

   cout<<endl<<"I am calling my wife!";

   mother->answer();

}

(4)尝试写一个Person类,使得Father、Mother和Child均继承自该类,记录过程中出现的各种编译器报错和运行期错误。

 

//person.h

#ifndef PERSON_H

#define PERSON_H

#include <iostream>

#include <string>

using namespace std;

class Person

{

public:

   string name;

   virtual void answer()=0;

};

#endif // PERSON_H

 

//child.h

#ifndef CHILD_H

#define CHILD_H

#include "person.h"

#include <string>

using namespace std;

class Father;

class Mother;

class Child : public person

{

public:

   Child();

   Father *father;

   Mother *mother;

   void answer();

   void callFather();

   void callMother();

};

#endif

 

//mother.h

#ifndef MOTHER_H

#define MOTHER_H

#include "person.h"

#include <string>

using namespace std;

class Child;

class Mother :public person

{

public:

   Mother();

Mother(Child *child);

Child *child;

   void callChild();

   void answer();

};

#endif

 

//father.h

#ifndef FATHER_H

#define FATHER_H

#include "person.h"

#include <string>

using namespace std;

class Child;

class Father :public person

{

public:

   Father();

   Father(Child *child);

   Child *child;

   void callChild();

   void answer();

};

#endif

 

//child.cpp

#include "child.h"

#include "mother.h"

#include "father.h"

 

#include <iostream>

using namespace std;

Child::Child()

{

   name="XIao Hua";

   father=new Father(this);

   mother=new Mother(this);

 

   father->child=this;

   mother->child=this;

}

void Child::answer(){

   cout<<endl<<name<<" is here!"<<endl;

}

void Child::callFather(){

   cout<<endl<<"I am calling my father!";

   father->answer();

}

void Child::callMother(){

   cout<<endl<<"I am calling my mother!";

   mother->answer();

}

 

//mother.cpp

#include "mother.h"

#include "child.h"

#include <iostream>

using namespace std;

Mother::Mother()

{

   name="Xiao Hua\'s Mother";

   child=new Child();

   child->mother=this;

   child->father=NULL;

}

Mother::Mother(Child *child)

{

   name="Xiao Hua\'s Mother";

   this->child=child;

}

void Mother::callChild(){

   cout<<endl<<name<<" say:I am calling mychild!";

   child->answer();

}

void Mother::answer(){

   cout<<endl<<"I\'m "<<name;

}

 

//father.cpp

#include "father.h"

#include "child.h"

#include <iostream>

using namespace std;

Father::Father()

{

   name="Xiao Hua\'s Father";

   child=new Child();

   child->father=this;

   child->mother=NULL;

}

void Father::callChild(){

   cout<<endl<<name<<" say:I am calling mychild!";

   child->answer();

}

 

Father::Father(Child *child)

{

   name="Xiao Hua\'s Father";

   this->child=child;

}

void Father::answer(){

   cout<<endl<<"I\'m "<<name;

}

 

//main.cpp

#include "father.h"

#include "mother.h"

#include "child.h"

#include <iostream>

using namespace std;

int main()

{

   Child xiaohua;

   xiaohua.answer();

   xiaohua.callFather();

   xiaohua.callMother();

   cout<<endl<<endl;

   Mother xiaoma;

   xiaoma.answer();

   xiaoma.callChild();

   Father xiaoba;

   xiaoba.answer();

   xiaoba.callChild();

   return 0;

}



                
【电力系统】单机无穷大电力系统短路故障暂态稳定Simulink仿真(带说明文档)内容概要:本文档围绕“单机无穷大电力系统短路故障暂态稳定Simulink仿真”展开,提供了完整的仿真模型与说明文档,重点研究电力系统在发生短路故障后的暂态稳定性问题。通过Simulink搭建单机无穷大系统模型,模拟不同类型的短路故障(如三相短路),分析系统在故障期间及切除后的动态响应,包括发电机转子角度、转速、电压功率等关键参数的变化,进而评估系统的暂态稳定能力。该仿真有助于理解电力系统稳定性机理,掌握暂态过程分析方法。; 适合人群:电气工程及相关专业的本科生、研究生,以及从事电力系统分析、运行与控制工作的科研人员工程师。; 使用场景及目标:①学习电力系统暂态稳定的基本概念与分析方法;②掌握利用Simulink进行电力系统建模与仿真的技能;③研究短路故障对系统稳定性的影响及提高稳定性的措施(如故障清除时间优化);④辅助课程设计、毕业设计或科研项目中的系统仿真验证。; 阅读建议:建议结合电力系统稳定性理论知识进行学习,先理解仿真模型各模块的功能与参数设置,再运行仿真并仔细分析输出结果,尝试改变故障类型或系统参数以观察其对稳定性的影响,从而深化对暂态稳定问题的理解。
本研究聚焦于运用MATLAB平台,将支持向量机(SVM)应用于数据预测任务,并引入粒子群优化(PSO)算法对模型的关键参数进行自动调优。该研究属于机器学习领域的典型实践,其核心在于利用SVM构建分模型,同时借助PSO的全局搜索能力,高效确定SVM的最优超参数配置,从而显著增强模型的整体预测效能。 支持向量机作为种经典的监督学习方法,其基本原理是通过在高维特征空间中构造个具有最大间隔的决策边界,以实现对样本数据的分或回归分析。该算法擅长处理小规模样本集、非线性关系以及高维度特征识别问题,其有效性源于通过核函数将原始数据映射至更高维的空间,使得原本复杂的分问题变得线性可分。 粒子群优化算法是种模拟鸟群社会行为的群体智能优化技术。在该算法框架下,每个潜在解被视作个“粒子”,粒子群在解空间中协同搜索,通过不断迭代更新自身速度与位置,并参考个体历史最优解群体全局最优解的信息,逐步逼近问题的最优解。在本应用中,PSO被专门用于搜寻SVM中影响模型性能的两个关键参数——正则化参数C与核函数参数γ的最优组合。 项目所提供的实现代码涵盖了从数据加载、预处理(如标准化处理)、基础SVM模型构建到PSO优化流程的完整步骤。优化过程会针对不同的核函数(例如线性核、多项式核及径向基函数核等)进行参数寻优,并系统评估优化前后模型性能的差异。性能对比通常基于准确率、精确率、召回率及F1分数等多项分指标展开,从而定量验证PSO算法在提升SVM模型分能力方面的实际效果。 本研究通过个具体的MATLAB实现案例,旨在演示如何将全局优化算法与机器学习模型相结合,以解决模型参数选择这关键问题。通过此实践,研究者不仅能够深入理解SVM的工作原理,还能掌握利用智能优化技术提升模型泛化性能的有效方法,这对于机器学习在实际问题中的应用具有重要的参考价值。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值