资源描述
程序设计所涉及二叉树解释
任意一棵树或一个森林都能唯一地对应一棵二叉树,由此而编写本程序.
本程序采用类二叉树为整体结构,二叉树类下定义节点类,每一条支路均为树的一个节点,支路所有的参数均作为节点的属性,并给节点加入属性“支路编号",并以支路编号为依据构建二叉树,这就要求提前根据二叉树结构给每一个支路编号。
支路编号原则:左子树上所有编号均小于其双亲的编号,右子树上所有编号均大于其双亲的编号,为了便于查看,本程序在节点较少时编号从1开始,逐个递加至支路数;当支路较多时,可不必拘泥于逐个递加,只要满足支路编号原则即可.
例如习题3-4:
程序二叉树结构示意图:
本二叉树中节点1即支路1为题目中节点1与节点2之间的部分;
本二叉树中节点2即支路2为题目中节点2与节点3之间的部分;
对于习题3-3:
程序二叉树结构示意图:
本二叉树中节点2即支路2为题目中节点1与节点2之间的部分;
本二叉树中节点1即支路1为题目中节点2与节点3之间的部分;
本二叉树中节点3即支路3为题目中节点2与节点4之间的部分.
拓展:
如下多支路网络:
对于三节点网络需先进行以下网络处理转化为标准二叉树,而后进行计算;
图中三角形表明该项阻抗为零,为纯导线,并进行相应参数补充进行计算。
程序说明文档
*******************************************************************************************************************************************************
本程序测试使用方法:在E盘根目录下建立输入文件:
输入文件名:input。txt;
将所附算例对应输入文件内容复制粘贴至上述文件中,在VC++6。0环境下运行cpp文件得出结果。
输出文件在E盘根目录下
输出文件名为:data。txt;
*******************************************************************************************************************************************************
程序功能说明:本程序可以计算任意长度线型开始网络潮流;
支持多电压等级下的计算;
可在除供电节点外任意节点引出负载;
*******************************************************************************************************************************************************
输入格式说明:以支路为基本单位,按潮流方向输入数据:
以下例示意:
2 //支路个数
1,110,118,8。5,20。5,0.000564,0,0,1,0,0 //线路等效的支路
2,11,110,1.22,20.2,0,40,30,10,0.17,1。7 //变压器等效的支路
. //按此方式知道输入所有的支路
. //输入从上到下的顺序为潮流在
。 //线型开式网络中的流动方向
/*
第一行输入支路个数,回车
第二行至后输入各个支路参数,回车分隔不同支路;
各行输入的支路参数顺序是:
支路编号,末端电压,始端电压,线路等效电阻,线路等效感抗,线路等效容纳,末端输入有功,末端输入无功,变比,变压器有功励磁损耗,变压器无功励磁损耗
对于线路等效电路:变压器有功/无功损耗输入零,变比输入1;
对于变压器等效电路:所有的参数均归算至高压侧,Rt,Xt对应输入线路等效电阻/感抗的位置,线路等效容抗为零;
对于个节点的引出负荷:输入至以此节点为末节点的支路的末端输入有功/无功部分;
*/
********************************************************************************************************************************************************
输出文件格式说明:输出的内容包括
(1)支路信息:每个支路元件的始端有功、无功和末端有功、无功;有功损耗无功损耗;电压损耗;
(2)全网信息:全网的总电源有功、总负荷有功、有功损耗、网损率;
(3)迭代信息:每次完整迭代后的所有内容;
具体在输出文件中都明确标出.
*********************************************************************************************************************************************************
程序中变量定义说明:
类中定义的变量
class line_part{ //定义支路类
double U[2]; //支路电压降落:U[0]电压降落横分量,U[1]电压降落纵分量
double k; //变压器变比
double val; //支路排序
double U_end; //支路末端电压
double U_begin; //支路首段电压
double X[3]; //支路等效阻抗: X[0]电阻,X[1]感抗,X[2]容纳
double S_end[2]; //支路末端功率:S_end[0]有功,S_end[1]无功
double S_begin[2]; //支路首段功率:S_begin[0]有功,S_begin[1]无功
double S0[2]; //变压器励磁损耗:S0[0]有功,S0[1]无功
double S_org[2]; //支路末端负载:S_org[0]有功,S_org[1]无功
line_part *lchild,*rchild; //支路的后继两个支路
}
class BinTree{ //定义树类
void PreOrder(){PreOrder(root);}; //树的先序遍历修改电压
void PostOrder(){PostOrder(root);}; //树的后序遍历修改潮流
void display(){display(root);}; //树的先序遍历显示数据
line_part *root; //树的根,是一个支路类
};
主函数中定义的数据
ofstream outfile; //输出数据流定
ifstream infile; //输入数据流定
const int M(a); //支路个数常量
主程序(复制粘贴到C++就能用)
**************************************************************************************************************************************************************
#include<fstream。h>
#include〈iostream。h〉
#include〈math.h>
double p_cost_all=0;
double sqr(double x){ //平方计算函数
return x*x;};
class line_part{ //定义支路类
private:
double val; //支路排序
double U_end; //支路末端电压
double U_begin; //支路首段电压
double X[3]; //支路等效阻抗: X[0]电阻,X[1]感抗,X[2]容纳
double S_end[2]; //支路末端功率:S_end[0]有功,S_end[1]无功
double S_begin[2]; //支路首段功率:S_begin[0]有功,S_begin[1]无功
double S0[2]; //变压器励磁损耗:S0[0]有功,S0[1]无功
double S_org[2]; //支路末端负载:S_org[0]有功,S_org[1]无功
line_part *lchild,*rchild;
public:
double U[2]; //支路电压降落:U[0]电压降落横分量,U[1]电压降落纵分量
double k; //变压器变比
public:
line_part(){
val=0;
U_end=0;U_begin=0;X[0]=0;X[1]=0;
X[2]=0;S_end[0]=S_org[0]=0;S_end[1]=S_org[1]=0;
S_begin[0]=0;S_begin[1]=0;k=1;
S0[0]=0;S0[1]=0;U[0]=0;U[1]=0;
lchild = rchild= NULL;
}
line_part(double vall,double u_end=0,double u_begin=0,double r=0,
double x=0,double b=0,double Pe=0,double Xe=0,double K=0,double P0=0,double Q0=0){
val=vall;
U_end=u_end;
U_begin=u_begin;
X[0]=r;
X[1]=x;
X[2]=b;
S_end[0]=S_org[0]=Pe;
S_end[1]=S_org[1]=Xe;
S_begin[0]=0;
S_begin[1]=0;
k=K;
S0[0]=P0;
S0[1]=Q0;
U[0]=0;
U[1]=0;
lchild = rchild= NULL;
};
friend class BinTree;
friend void pass_U(line_part*a,line_part*b,line_part*c); //电压传递函数
friend void pass_w(line_part*a,line_part*b,line_part*c); //功率传递函数
friend void pass_U2(line_part*a,line_part*b); //电压传递函数
friend void pass_w2(line_part*a,line_part*b); //功率传递函数
void Sbegin(){ //支路首段功率计算函数
double Uend,I2;
Uend=k*U_end;
I2=(sqr(S_end[0])+sqr(S_end[1]—sqr(U_end)*X[2]/2))/sqr(Uend);
S_begin[0]=S_end[0]+I2*X[0]+S0[0];
S_begin[1]=S_end[1]+I2*X[1]+S0[1]—sqr(U_begin)*X[2]/2—sqr(U_end)*X[2]/2;
};
void Uend(){ //支路末端电压计算函数
double U_heng(0),U_zong(0);
double p_begin,q_begin;
p_begin=S_begin[0]—S0[0];
q_begin=S_begin[1]+sqr(U_begin)*X[2]/2—S0[1];
U_heng=(p_begin*X[0]+q_begin*X[1])/U_begin; //U_heng即是△u2
U_zong=(p_begin*X[1]—q_begin*X[0])/U_begin; //U_zong即是δu2
U_end=sqrt(sqr(U_begin—U_heng)+sqr(U_zong))/k;
U[0]=U_heng;
U[1]=U_zong;
};
double get_val(){ //返回支路编号
if(this==0){return —1;}else{
if(val〉0&&val<100) {return val;}
else return —1;
}
};
double get_Uend(){ //返回支路末端电压
return U_end;
};
double get_Ubegin(){ //返回支路首段电压
return U_begin;};
double get_Pbegin(){ //返回支路首段有功
return S_begin[0];};
double get_Pend(){ //返回支路末端有功
return S_end[0];};
double get_Qbegin(){ //返回支路首段无功
return S_begin[1];};
double get_Qend(){ //返回支路末端无功
return S_end[1];};
double get_Pcost(){ //返回支路有功损耗
return S_begin[0]-S_end[0];};
double get_Qcost(){ //返回支路无功损耗
return S_begin[1]-S_end[1];};
line_part *get_lchild(){ //返回支路无功损耗
return lchild;};
line_part *get_rchild(){ //返回支路无功损耗
return rchild;};
};
void pass_U(line_part *a,line_part *b,line_part *c){
(*c)。U_begin=(*a)。U_end;
(*b).U_begin=(*a).U_end;
};
void pass_w(line_part *a,line_part *b,line_part *c){
(*a).S_end[0]=(*b).S_begin[0]+(*c)。S_begin[0]+(*a).S_org[0];
(*a)。S_end[1]=(*b).S_begin[1]+(*c).S_begin[1]+(*a).S_org[1];
};
void pass_U2(line_part *a,line_part *b){
(*b)。U_begin=(*a)。U_end;
};
void pass_w2(line_part *a,line_part *b){
(*a)。S_end[0]=(*b)。S_begin[0]+(*a)。S_org[0];
(*a)。S_end[1]=(*b).S_begin[1]+(*a).S_org[1];
};
class BinTree{
public:
friend void pass_U(line_part*,line_part*,line_part*); //电压传递函数
friend void pass_w(line_part*,line_part*,line_part*); //功率传递函数
BinTree(){line_part *aa=new line_part(0,0,0,0,0,0,0,0,0,0,0); root = aa; }
line_part *Getroot(){return root;}
void insertline_part(double vall,double u_end=0,double u_begin=0,double r=0,
double x=0,double b=0,double Pe=0,double Xe=0,
double K=0,double P0=0,double Q0=0){
insertline_part(root,vall,u_end,u_begin,r,x,b,Pe,Xe,K,P0,Q0);
}
void PreOrder(){PreOrder(root);};
void PostOrder(){PostOrder(root);};
void display(){display(root);};
private:
line_part *root;
void insertline_part(line_part *&t,double vall,double u_end,double u_begin,double r,
double x,double b,double Pe,double Xe,
double K,double P0,double Q0);
void PreOrder(line_part *&t);
void PostOrder(line_part *t);
void display(line_part *&t);
};
void BinTree::insertline_part(line_part *&t, double vall,double u_end=0,double u_begin=0,double r=0,
double x=0,double b=0,double Pe=0,double Xe=0,
double K=0,double P0=0,double Q0=0){ //插入节点
double ass=t—>get_val();
if(t==0||t-〉get_val()〈=0) {
t=new line_part (vall,u_end,u_begin,r,x,b,Pe,Xe,K,P0,Q0);}
else if(vall<t—〉get_val()) {
insertline_part(t-〉lchild, vall,u_end,u_begin,r,x,b,Pe,Xe,K,P0,Q0);}
else {
insertline_part(t-〉rchild, vall,u_end,u_begin,r,x,b,Pe,Xe,K,P0,Q0);}
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
void BinTree::display(line_part *&t){
if(t—〉get_val()>0&&t—>get_val()〈100)
{
display(t—>lchild);
display(t—〉rchild);
ofstream outfile1;
outfile1.open(”e:\data。txt”,ios::ate);
p_cost_all +=t—〉get_Pcost();
double U; // 计算并存放各个节点的电压相角(始端为零)
U=atan2(t—〉U[1],(t—>get_Ubegin()-t-〉U[0]))/3.1415926*180;
outfile1<〈”支路"〈〈t->get_val()〈<”电压相角"<〈” "〈〈U〈<endl;
outfile1〈<" "〈<"首端电压"〈<" ”<<t—>get_Ubegin()〈〈endl; //计算并存放各个支路的首端电压
outfile1<<” "<<”末端电压"〈<" ”〈<t—>get_Uend()〈<endl; //计算并存放各个支路的末端电压
double U_cost; //计算并存放各个支路的电压损耗
U_cost=t—〉get_Ubegin()—t—>get_Uend();
outfile1〈<" "〈<"电压损耗”〈<” ”〈〈U_cost〈〈endl;
outfile1<〈” ”<〈”始端有功"〈〈” "〈〈t—〉get_Pbegin()<〈endl; //计算并存放各个支路的首端电压
outfile1<〈” "〈<”始端无功”〈〈” ”〈〈t->get_Qbegin()<<endl; //计算并存放各个支路的末端电压
outfile1〈〈" "<<"末端有功"〈〈” ”<〈t—〉get_Pend()〈〈endl; //计算并存放各个支路的首端电压
outfile1<<” ”〈〈"末端无功”〈<” ”<〈t->get_Qend()〈〈endl; //计算并存放各个支路的末端电压
double P_cost; //计算并存放各个支路的有功损耗
P_cost=t—〉get_Pcost();
outfile1〈<" ”<〈”有功损耗”<〈” "〈<P_cost〈〈endl;
double Q_cost; //计算并存放各个支路的无功损耗
Q_cost=t-〉get_Qcost();
outfile1〈〈" "<<”无功损耗”<〈" ”<<Q_cost〈〈endl;
outfile1。close();
}
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void BinTree::PreOrder(line_part *&t){ //先序遍历二叉树修改电压
if(t-〉get_val()>0&&t-〉get_val()<100)
{
if(t->get_lchild()->get_val()>0&&t—〉get_lchild()->get_val()〈100)
{
if(t—〉get_rchild()—〉get_val()>0&&t—〉get_rchild()—〉get_val()〈100)
{
t—>Uend();
pass_U(t,t—>lchild,t->rchild);
t->lchild-〉Uend();
t-〉rchild-〉Uend();
}
else{
t—>Uend();
pass_U2(t,t->lchild);
t->lchild-〉Uend();}
}
else
{
if(t-〉get_rchild()—〉get_val()>0&&t—>get_rchild()-〉get_val()<100)
{
t-〉Uend();
pass_U2(t,t->rchild);
t->rchild-〉Uend();
}
else{}
}
PreOrder(t—〉lchild);
PreOrder(t->rchild);
}
};
void BinTree::PostOrder(line_part *t){ //后序遍历二叉树修改潮流
if(t—>get_val()>0&&t—〉get_val()〈100)
{
PostOrder(t—〉lchild);
PostOrder(t—>rchild);
if(t->get_lchild()—〉get_val()〉0&&t->get_lchild()-〉get_val()〈100)
{
if(t—〉get_rchild()->get_val()>0&&t—〉get_rchild()-〉get_val()<100)
{
t->lchild->Sbegin();
t—〉rchild—>Sbegin();
pass_w(t,t->lchild,t-〉rchild);
t—〉Sbegin();
}
else{
t->lchild—>Sbegin();
pass_w2(t,t—>lchild);
t-〉Sbegin();}
}
else
{
if(t-〉get_rchild()—>get_val()〉0&&t—>get_rchild()—>get_val()<100)
{
t->rchild->Sbegin();
pass_w2(t,t->rchild);
t-〉Sbegin();
}
else{}
}
}
};
void main(){
ofstream outfile; //输出数据流定义
outfile.open("e:\data。txt”);
outfile。clear();
outfile。close();
ifstream infile;
infile。open("e:\input。txt"); //输入数据流定义
int a;
int l(0);
char b;
infile〉>a;
cout<<”节点个数 "〈〈a<〈endl;
const int M(a); //支路节点数常量
double *A=new double[10*M]; //输入流输入各支路数据数组
while(l<11*M){
infile〉〉A[l];
infile.get(b);
l++;
};
outfile.open(”e:\data。txt”,ios::ate);
outfile<<”节点个数 ”<〈a<〈endl;
outfile〈<endl;
outfile.close();
BinTree elec;
for(int i=0;i〈M;i++){ //添加节点
elec。insertline_part(A[11*i+0],A[11*i+1],A[11*i+2],A[11*i+3],A[11*i+4],
A[11*i+5],A[11*i+6],A[11*i+7],A[11*i+8],A[11*i+9],A[11*i+10]);
}
for(i = 0;i〈5;i++){
outfile.open(”e:\data.txt”,ios::ate);
outfile<〈endl;
outfile〈<"第"〈〈i+1〈〈”次迭代结果如下”<<endl;
outfile<〈endl;
outfile.close();
elec。PostOrder();
elec.PreOrder();
elec.display();
outfile.open("e:\data。txt",ios::ate);
outfile<〈endl;
line_part *ee=new line_part();
ee=elec。Getroot();
double aa =ee—>get_Pbegin();
outfile<〈"全网的总电源有功”〈<aa〈<endl;
outfile。close();
outfile.open("e:\data。txt”,ios::ate);
double bb ;
bb =aa - p_cost_all;
outfile〈〈”全网的总负荷有功”〈〈bb〈〈endl;
outfile<<”全网的总有功损耗”〈<p_cost_all<〈endl;
double cc;
cc = p_cost_all/aa*100;
outfile<〈"全网的网损率”〈<cc〈〈”%”〈〈endl;
outfile.close();
p_cost_all=0.0;
}
};
**************************************************
展开阅读全文