数据结构课程设计——电力设备管理系统

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

//////////////////////Power Equipment Management System///////
//////////////////////ver1.0//////////////////////////////////
/////////////////////2011.06.27///////////////////////////////
#include
#include
#include

#define STRING 50
#define MAX_VEX_NUM 100
#define MAX_INT (1<<31-1)

typedef struct VEX
{
int id;
char name[STRING];
char area[STRING];
char type[STRING];//bus,trans,circuit,switcher,load
bool state;
float volt;
}vexdata;

///////////////Graph////////////
typedef struct ARCNODE
{
int adjvex;
struct ARCNODE *nextarc;
// int info;
}Arcnode;

typedef struct VEXNODE
{
vexdata data;
Arcnode *firstarc;
}Vexnode;

typedef struct
{
Vexnode vextices[MAX_VEX_NUM];
int vexnum,arcnum;
}ALGraph;
///////////////////////////////////////////
/////////////////FUNCTION LIST/////////////
int FindIndex(ALGraph gb,char *name);
int _type(char *name);
char *findtype(int e);
char *findarea(int e);
void DFS(ALGraph gb,char *name,bool visited[],int load[],int &count);
void DFSTravel(ALGraph gb,char *name,int load[],int &count);
void Build_Net(ALGraph &gb);
void _print_type(ALGraph gb,char type[]);
void _print_area(ALGraph gb,char area[]);
void display(ALGraph gb);
void insret(ALGraph &gb);
void _modify(ALGraph &gb);
void check(ALGraph gb);
void menu(int &choice);
///////////////////////////////////////////

int FindIndex(ALGraph gb,char *name)//未考虑输入ID的情况
{
for(int i=0;i{
if(strcmp(gb.vextices[i],name)==0)
return i;
}

return -1;
}

int _type(char *name)
{
if(strcmp(name,"bus")==0)
return 1;
else if(strcmp(name,"trans")==0)
return 2;
else if(strcmp(name,"circuit")==0)
return 3;
else if(strcmp(name,"switcher")==0)
return 4;
else if(strcmp(name,"load")==0)
return 5;
else
return -1;
}

char *findtype(int e)
{
if(e==0)
return "bus";
else if(e==1)
return "trans";
else if(e==2)
return "circuit";
else if(e==3)
return "switcher";
else if(e==4)
return "load";
else
return "None";
}

char *findarea(int e)
{
if(e==0)
return "zhuganquyu";//主干区域
else if(e==1)
return "zhuxinzhuang";//朱辛庄
else if(e==2)
return "huilongguan";//回龙观
else
return "None";
}

///////////////////////////遍历
void DFS(ALGraph gb,char *name,bool visited[],int load[],int &count)
{
Arcnode *p;
int start=FindIndex(gb,name);
visited[start]=true;
if(_type(gb.vextices[start].data.type)==5)
load[count++]=start;
p=gb.vextices[start].firstarc;
while(p!=NULL)
{
if(visited[p->adjvex]==false&&gb.vextices[p->adjvex].data.state==true)//通路才可遍历
{
DFS(gb,gb.vextices[p->adjvex],visited,load,count);
}
p=p->nextarc;
}
}

void DFSTravel(ALGraph gb,char *name,int load[],int &count)
{
bool visited[MAX_VEX_NUM];
for(int i=0;ivisited[i]=false;
DFS(gb,name,visite

d,load,count);
}



void Build_Net(ALGraph &gb)
{
int n1,n2,ln;
char ch1[STRING],ch2[STRING],line[STRING];
Arcnode *p;
FILE *fp;
int count=0;//vexnum,arcnum
if((fp=fopen("vexdata.dat","r+"))==NULL)
{
printf("The file error!");
exit(0);
}
while(!feof(fp))
{
fscanf(fp,"%d%s%s%s",&gb.vextices[count].data.id,gb.vextices[count],gb.vextices[count].data.type,gb.vextices[count].data.area);//id,name,type,area
gb.vextices[count].firstarc=NULL;


if(_type(gb.vextices[count].data.type)==4)
gb.vextices[count].data.state=false;
else
gb.vextices[count].data.state=true;

if(strcmp(gb.vextices[count].data.area,"zhuganquyu")&&_type(gb.vextices[count].data.type)==1)
fscanf(fp,"%f",&gb.vextices[count].data.volt);
else
gb.vextices[count].data.volt=-1;
count++;
}
gb.vexnum=count;
fclose(fp);

count =0;
if((fp=fopen("arcdata.dat","r+"))==NULL)
{
printf("The file error!");
exit(0);
}

while(!feof(fp))
{
fscanf(fp,"%s%s%s",ch1,line,ch2);
n1=FindIndex(gb,ch1);n2=FindIndex(gb,ch2);ln=FindIndex(gb,line);
p=new Arcnode;
p->adjvex=ln;
p->nextarc=gb.vextices[n1].firstarc;
gb.vextices[n1].firstarc=p;
p=new Arcnode;
p->adjvex=n1;
p->nextarc=gb.vextices[ln].firstarc;
gb.vextices[ln].firstarc=p;

p=new Arcnode;
p->adjvex=n2;
p->nextarc=gb.vextices[ln].firstarc;
gb.vextices[ln].firstarc=p;
p=new Arcnode;
p->adjvex=ln;
p->nextarc=gb.vextices[n2].firstarc;
gb.vextices[n2].firstarc=p;
count++;

if(_type(gb.vextices[n2].data.type)==5)
gb.vextices[n2].firstarc=NULL;
}
gb.arcnum=count;

fclose(fp);
}
//打印函数type
void _print_type(ALGraph gb,char type[])
{
Arcnode *p;
int i;
bool visited[MAX_VEX_NUM];
for(i=0;ivisited[i]=false;
printf("Id\t名称 \t类型\t所在区域\t电压(若可用)\n");
for(i=0;i{
//先判断第一个
if(strcmp(gb.vextices[i].data.type,type)==0&&visited[i]==false)
{
visited[i]=true;
if(_type(type)==1&&strcmp(gb.vextices[i].data.area,"zhuganquyu"))
printf("%d\t%s\t%s\t%s\t%f\n",gb.vextices[i].data.id,gb.vextices[i],gb.vextices[i].data.type,gb.vextices[i].data.area,gb.vextices[i].data.volt);
else
printf("%d\t%s\t%s\t%s\n",gb.vextices[i].data.id,gb.vextices[i],gb.vextices[i].data.type,gb.vextices[i].data.area);

}

p=gb.vextices[i].firstarc;
while(p)
{
if(strcmp(gb.vextices[p->adjvex].data.type,type)==0&&visited[p->adjvex]==false)
{
visited[p->adjvex]=true;
if(_type(type)==1&&strcmp(gb.vextices[p->adjvex].data.area,"zhuganquyu"))
printf("%d\t%s\t%s\t%s\t%f\n",gb.vextices[p->adjvex].data.id,gb.vextices[p->adjvex],gb.vextices[p->adjvex].data.type,gb.vextices[p->adjvex].data.area,gb.vextices[p->adjvex].data.volt);
else
printf("%d\t%s\t%s\t%s\n",gb.vextices[p->adjvex].data.id,gb.vextices[p->adjvex],gb.vextices

[p->adjvex].data.type,gb.vextices[p->adjvex].data.area);

}
p=p->nextarc;
}
}
}
//打印函数area
void _print_area(ALGraph gb,char area[])
{
Arcnode *p;
int i;
bool visited[MAX_VEX_NUM];
for(i=0;ivisited[i]=false;

printf("Id\t名称\t类型\t所在区域\t电压(若可用)\n");
for(i=0;i{
//先判断第一个
if(strcmp(gb.vextices[i].data.area,area)==0&&visited[i]==false)
{
visited[i]=true;
if(strcmp(area,"zhuganquyu")&&_type(gb.vextices[i].data.type)==1)
printf("%d\t%s\t%s\t%s\t%f\n",gb.vextices[i].data.id,gb.vextices[i],gb.vextices[i].data.type,gb.vextices[i].data.area,gb.vextices[i].data.volt);
else
printf("%d\t%s\t%s\t%s\n",gb.vextices[i].data.id,gb.vextices[i],gb.vextices[i].data.type,gb.vextices[i].data.area);

}

p=gb.vextices[i].firstarc;
while(p)
{
if(strcmp(gb.vextices[p->adjvex].data.area,area)==0&&visited[p->adjvex]==false)
{
visited[p->adjvex]=true;
if(strcmp(area,"zhuganquyu")&&_type(gb.vextices[p->adjvex].data.type)==1)
printf("%d\t%s\t%s\t%s\t%f\n",gb.vextices[p->adjvex].data.id,gb.vextices[p->adjvex],gb.vextices[p->adjvex].data.type,gb.vextices[p->adjvex].data.area,gb.vextices[p->adjvex].data.volt);
else
printf("%d\t%s\t%s\t%s\n",gb.vextices[p->adjvex].data.id,gb.vextices[p->adjvex],gb.vextices[p->adjvex].data.type,gb.vextices[p->adjvex].data.area);

}
p=p->nextarc;
}
}
}

void display(ALGraph gb)//只考虑了现有type area
{

int choice;
int i=1;
char name[STRING];
printf("请选择显示方式:\n");
printf("1.按类型\t2.按区域\n");
printf("选择:");
scanf("%d",&choice);
getchar();
switch(choice)
{
case 1:
while(i)
{
strcpy(name,findtype(i-1));
if(strcmp(name,"None")==0)
break;

printf("\n");
printf("%s\n",name);
_print_type(gb,name);
i++;
}
case 2:
while(i)
{
strcpy(name,findarea(i-1));
if(strcmp(name,"None")==0)
break;

printf("\n");
printf("%s\n",name);
_print_area(gb,name);
i++;

}
}
printf("\n");
}

void insert(ALGraph &gb)
{
FILE *fp;
char ch1[STRING],ch2[STRING],line[STRING];
int count;
printf("请输入要插入元件的数量:");
scanf("%d",&count);
getchar();
printf("请输入元件信息:\n");
if(count<1)
{
printf("\n输入数量不合法,系统将退出插入!");
return;
}
for(int i=0;i{
//vex
printf("%d、",i+1);
printf("Id\t名称\t类型\t所在区域 \n");
scanf("%d%s%s%s",&gb.vextices[gb.vexnum].data.id,gb.vextices[gb.vexnum],gb.vextices[gb.vexnum].data.type,gb.vextices[gb.vexnum].data.area);
gb.vextices[count].firstarc=NULL;

//Input in vexdata
if((fp=fopen("vexdata.dat","a+"))==NULL)
{
printf("The file error!");
exit(0);
}
fprintf(fp,"%d\t%s\t%s\t%s",gb.vextices[gb.

vexnum].data.id,gb.vextices[gb.vexnum],gb.vextices[gb.vexnum].data.type,gb.vextices[gb.vexnum].data.area);
if(strcmp(gb.vextices[gb.vexnum].data.area,"zhuganquyu")&&_type(gb.vextices[gb.vexnum].data.type)==1)
{
printf("经检测此元件不在主干电路上,请输其支路电压:");
scanf("%f",&gb.vextices[gb.vexnum].data.volt);
fprintf(fp,"\t%f\n",gb.vextices[gb.vexnum].data.volt);
printf("\n");
}
else
{
gb.vextices[gb.vexnum].data.volt=-1;
fprintf(fp,"\n");
}
fclose(fp);


if(_type(gb.vextices[gb.vexnum].data.type)==4)
gb.vextices[gb.vexnum].data.state=false;
else
gb.vextices[gb.vexnum].data.state=true;


//arc
if(_type(gb.vextices[gb.vexnum].data.type)!=5)//!load
{
printf("请输入连接情况(例如:元件1<--->元件2<--->元件3):\n");
printf("(示意:元件1 元件2 元件3)\n");
scanf("%s%s%s",ch1,line,ch2);

if((fp=fopen("arcdata.dat","a+"))==NULL)
{
printf("The file error!");
exit(0);
}
fprintf(fp,"%s\t%s\t%s\n",ch1,line,ch2);//if load,should be the last!
fclose(fp);
}
else//type==load
{
printf("系统检测到所插入的元件是负载\n");
printf("请输入连接情况(例如:上级线路<--->所插线路<--->负载):\n");
printf("(示意:上级线路\t所插线路)\n");
scanf("%s%s",ch1,line);

if((fp=fopen("arcdata.dat","a+"))==NULL)
{
printf("The file error!");
exit(0);
}
fprintf(fp,"%s\t%s\t%s\n",ch1,line,gb.vextices[gb.vexnum]);//if load,should be the last!
fclose(fp);
}
}
Build_Net(gb);
printf("插入成功!");
getchar();
}

void _modify(ALGraph &gb)
{
char name[STRING];
int index;
int choice;
char alter[STRING];
char type[STRING];
FILE *fp;
//goto
loop:
printf("请输入元件的名称:");
scanf("%s",name);
index=FindIndex(gb,name);
printf("**********可选修改项目**********\n");
printf("1. ID 2. 名称\n");
printf("3. 所属区域 4. 类型\n");
printf("0. 退出修改\n");
printf("请选择要修改的项目:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("该元件原ID为:");
printf("%d\n",gb.vextices[index].data.id);
printf("请输入修改后的ID:");
scanf("%d",&gb.vextices[index].data.id);
break;
case 2:
printf("该元件原名称为:");
printf("%s\n",gb.vextices[index]);
printf("请输入修改后的名称:");
scanf("%s",gb.vextices[index]);
break;
case 3:
printf("该元件原所属区域为:");
printf("%s\n",gb.vextices[index].data.area);
printf("请输入修改后的所属区域:");
scanf("%s",gb.vextices[index].data.area);
if(strcmp(gb.vextices[index].data.area,"zhuganquyu")&&_type(gb.vextices[index].data.type)==1)//volt
{
printf("原电压值为%f",gb.vextices[index].data.volt);
printf("是否改变此电压值?(Y/N)");

scanf("%s",alter);
if(strcmp(alter,"Y")==0||strcmp(alter,"y")==0)
{
printf("请输入新的电压值:");
scanf("%f",&gb.vextices[index].data.volt);
}
else
break;
}
break;
case 4:
printf("该元件原类型为:");
printf("%s\n",gb.vextices[index].data.type);
printf("请输入修改后的类型:");
scanf("%s",type);
//load
if(_type(type)==5)
{
if(gb.vextices[index].firstarc!=NULL)
{
printf("注意!有其他元件和该元件相连,修改为负载后,其他元件将有可能与它失去连接!\n");
printf("是否确定修改?(Y/N)");
scanf("%s",alter);
if(strcmp(alter,"Y")==0||strcmp(alter,"y")==0)
strcpy(gb.vextices[index].data.type,type);
else break;
}
else
strcpy(gb.vextices[index].data.type,type);
}
else
strcpy(gb.vextices[index].data.type,type);
//volt&&主干区域
if(strcmp(gb.vextices[index].data.area,"zhuganquyu")&&_type(gb.vextices[index].data.type)==1)
{
printf("请输入该支路上的电压值:");
scanf("%f",&gb.vextices[index].data.volt);
}
//switch
if(_type(gb.vextices[index].data.type)==4)
{
printf("系统检测到新的类型为“刀闸”,是否将刀闸设为断开?(Y/N)");
scanf("%s",alter);
if(strcmp(alter,"Y")==0||strcmp(alter,"y")==0)
gb.vextices[index].data.state=false;
}
case 0:
break;
default:
break;
}
printf("是否继续修改?(Y/N)");
scanf("%s",alter);
if(strcmp(alter,"Y")==0||strcmp(alter,"y")==0)
goto loop;

//Inpot into file
if((fp=fopen("vexdata.dat","w+"))==NULL)
{
printf("The file error!");
exit(0);
}
for(int i=0;i{
//volt
if(strcmp(gb.vextices[i].data.area,"zhuganquyu")&&_type(gb.vextices[i].data.type)==1)
fprintf(fp,"%d\t%s\t%s\t%s\t%f\n",gb.vextices[i].data.id,gb.vextices[i],gb.vextices[i].data.type,gb.vextices[i].data.area,gb.vextices[i].data.volt);
else
fprintf(fp,"%d\t%s\t%s\t%s\n",gb.vextices[i].data.id,gb.vextices[i],gb.vextices[i].data.type,gb.vextices[i].data.area);
}
fclose(fp);
}

void check(ALGraph gb)
{
int load[MAX_VEX_NUM];//记录负载
int slect_load[MAX_VEX_NUM];//记录断后节点后的负载
int count_load=0;//记录负载个数
int count_slect_load=0;//记录断后节点后的负载数
char name[STRING];
int locate;
int i,j;
printf("电路已接通!\n");
//联通开关
for(i=0;i{
if(_type(gb.vextices[i].data.type)==4)
gb.vextices[i].data.state=true;
}


//确定断点
printf("\n请输入发生故障的元件或欲断开刀闸的名称:\n");
scanf("%s",name);
locate=FindIndex(gb,name);

gb.vextices[locate].data.state=false;

DFSTravel(gb,gb.vextices[0],load,count_load);//处中断区域外的负载
DFSTravel(gb,gb.vextices[locate],slect_load,count_slect_load);//中断区域内的负载
//找出无法工作

的负载
for(i=0;ifor(j=0;j{
if(slect_load[j]==load[i])
slect_load[j]=-1;
}
printf("无法工作的负载有:\n");
printf("ID 名称 所在区域\n");
for(j=0;jif(slect_load[j]!=-1)
printf("%d %s %s\n",gb.vextices[slect_load[j]].data.id,gb.vextices[slect_load[j]],gb.vextices[slect_load[j]].data.area);
//开关恢复初始状态0
for(i=0;i{
if(_type(gb.vextices[i].data.type)==4)
gb.vextices[i].data.state=false;
}
}

void menu(int &choice)
{
printf("****************Power Equipment Management System*******************\n\n");
printf("********************************菜单********************************\n\n");
printf("\t1.初始化数据\t\t2.显示元件\n");
printf("\t3.插入元件\t\t4.修改元件信息\n");
printf("\t5.检验电路\t\t6.程序信息\n");
printf("\t0.退出系统\n\n");
printf("*********************注意:第一次运行请先初始化*********************\n");
printf("请选择功能:");
scanf("%d",&choice);
getchar();
}

void main(void)
{
ALGraph gb;
int n=1;
int choice;
while(n)
{
//system("cls");
menu(choice);
switch(choice)
{
case 1:
Build_Net(gb);
printf("初始化成功!按任意键返回");
getchar();
system("cls");
break;
case 2:
display(gb);
printf("按任意键返回");
getchar();
system("cls");
break;
case 3:
insert(gb);
getchar();
system("cls");
break;
case 4:
_modify(gb);
getchar();
break;
case 5:
check(gb);
getchar();
break;
case 6:
printf("*******************************Ver. 1.0*****************************\n\n");
printf("\t\t\tBy Huang BW\n\n");
printf("*******************************2011.06.29***************************\n");
printf("按任意键返回");
getchar();
system("cls");
break;
case 0:
n=0;
break;
default:
printf("请输入合法的数字!");
getchar();
system("cls");
break;
}
}
}

相关文档
最新文档