数据结构与算法实验课表安排.docx

上传人:b****5 文档编号:11965773 上传时间:2023-04-16 格式:DOCX 页数:31 大小:106.18KB
下载 相关 举报
数据结构与算法实验课表安排.docx_第1页
第1页 / 共31页
数据结构与算法实验课表安排.docx_第2页
第2页 / 共31页
数据结构与算法实验课表安排.docx_第3页
第3页 / 共31页
数据结构与算法实验课表安排.docx_第4页
第4页 / 共31页
数据结构与算法实验课表安排.docx_第5页
第5页 / 共31页
点击查看更多>>
下载资源
资源描述

数据结构与算法实验课表安排.docx

《数据结构与算法实验课表安排.docx》由会员分享,可在线阅读,更多相关《数据结构与算法实验课表安排.docx(31页珍藏版)》请在冰豆网上搜索。

数据结构与算法实验课表安排.docx

数据结构与算法实验课表安排

数据结构与算法分析课程设计报告

课题名称:

拓扑排序——

打印输出计算机本科专业4年每学期的课表

 

提交文档学生姓名:

提交文档学生学号:

同组成员名单:

指导教师姓名:

指导教师评阅成绩:

指导教师评阅意见:

提交报告时间:

2010年12月日

1.实验题目:

拓扑排序——打印输出计算机本科专业4年每学期的课表

2.实验的目的和要求:

(1)采用C++实现;

(2)熟练掌握图的应用;

(3)熟练掌握图的邻接矩阵存储结构以级拓扑排序的基本思想;

(4)上机调试程序,掌握查错,排错使程序能正确运行。

3.实验的环境:

(1)硬件环境:

联想G450笔记本电脑

(2)软件环境:

WindowsXP,MicrosoftVisualC++6.0

4.算法描述:

程序流程图

 

●类的层次结构,每个类的设计,包括数据成员和成员函数组成.

●classGraph

●{

●public:

●virtualintn()=0;

●virtualinte()=0;

●virtualintfirst(int)=0;

●virtualintnext(int,int)=0;

●virtualvoidsetEdge(int,int,int)=0;

●virtualvoiddelEdge(int,int)=0;

●virtualintweight(int,int)=0;

●virtualintgetMark(int)=0;

●virtualvoidsetMark(int,int)=0;

●};

●classGraphm:

publicGraph

●{

●public:

●Graphm(intnumVert);

●~Graphm();

●intn();

●inte();

●intfirst(int);

●intnext(int,int);

●voidsetEdge(int,int,int);

●voiddelEdge(int,int);

●intweight(int,int);

●intgetMark(int);

●voidsetMark(int,int);

●private:

●intnumVertex,numEdge;

●int**matrix;

●int*mark;

●};

●classLink

●{

●public:

●Link();

●Link(stringcode,stringname,intperiod,intsemester,stringprecondition);

●stringgetCode();

●voidsetCode(stringstr);

●stringgetName();

●voidsetName(stringstr);

●intgetPeriod();

●voidsetPeriod(intp);

●intgetSemaster();

●voidsetSemester(ints);

●stringgetPrecondition();

●voidsetPredition(stringstr);

●Link*getNext();

●voidsetNext(Link*);

●private:

●stringcode;

●stringname;

●intperiod;

●intsemester;

●stringprecondition;

●Link*next;

●};

●classList

●{

●public:

●List();

●voidaddCourse(Link*course);

●voidinsertCourse(Link*course);

●Link*getHead();

●Link*getTail();

●voidprintlist();

●private:

●Link*head;

●Link*tail;

●};

●测试程序说明

●intmain()

●{

●readFromFile();//从文件读取课程信息

●Graphmgraph(38);

●buildGraph(list,&graph);//做课程信息图

●topsort(&graph);//对图进行拓扑排序

●sortedlist.printlist();//打印排序后的结构

●cout<

●courseArrange();//安排课表

printSchoolTimeTable();//打印课表,并将其存入文件

●return0;

●}

5.源程序清单:

●添加必要的注释

●staticListlist;//存放初始课程信息

●staticListsortedlist;//存放拓扑排序后的课程信息(顺序)

●staticintarraynum[8];//保存每个学期要开设的课程数目

●staticintindex;//共八个学期

●staticstringcourName;//从文件读取课程编码

●staticstringcourCode;//从文件读取课程名称

●staticstringcourPrecondition;//从文件读取课程的先决条件

●staticintcourperiod;//从文件读取对应课程的开设学时数

●staticintcoursemester;//从文件读取对应课程的开课学期

●//读取文件数据,创建list

●voidreadFromFile()

●{

●fstreaminput;

●input.open("course_inf.txt");

●charch;

●ch=input.get();

●while(!

(ch>='0'&&ch<='9'))//开始出现数字时,开始读取arraynum[]

●ch=input.get();

●if(ch>='0'&&ch<='9'&&(index<8))

●{

●while(ch!

='\n')

●{

●if(ch>='0'&&ch<='9'&&(index<8))

●arraynum[index++]=ch-48;//将读入的字符转换成对应数字

●ch=input.get();

●}

●}

●while(ch!

='c')

●ch=input.get();

●while(ch!

='\n')

●ch=input.get();//跳过所有注释行

●while(!

input.eof())

●{

●if(ch=='c')

●{

●while(ch!

=9)

●{

●courCode.insert(courCode.size(),1,ch);//读取courCode

●ch=input.get();

●}

●while(ch==9)

●ch=input.get();

●while(ch!

=9)

●{

●courName.insert(courName.size(),1,ch);//读取courName

●ch=input.get();

●}

●while(ch==9)

●ch=input.get();

●courperiod=ch-48;//读取courperiod

●ch=input.get();

●while(ch==9)

●ch=input.get();

●coursemester=ch-48;//读取coursemester

●ch=input.get();

●while(ch==9)

●ch=input.get();

●if(ch=='c')

●while(ch!

='\n')//读取courPrecondition

●{

●courPrecondition.insert(courPrecondition.size(),1,ch);

●ch=input.get();

●}

●}//if

●ch=input.get();

●if(courCode.size()>0)

●{

●Link*courselink=newLink(courCode,courName,courperiod,

●coursemester,courPrecondition);//读取一次完整的信息即可将它生成一个Link节点

●list.addCourse(courselink);//将Link节点加入List

●}

●courCode.erase();//清除string中的内容,用于下一行次读取文件

●courName.erase();

●courPrecondition.erase();

●}//while

●input.close();

●}

●//建图,添加有先决条件的结点之间的边

●voidbuildGraph(List&courseList,Graph*courseGraph)

●{

●Link*courselink=courseList.getHead();

●intv1=0;

●while(courselink!

=NULL)

●{

●stringstr=courselink->getPrecondition();

●for(inti=0;str[i]!

='\0';)

●{

●if(str[i]=='c')//课程以c开头,由此分辨先决条件

●{

●charch1=str[++i];

●charch2=str[++i];

●intv2=10*(ch1-48)+(ch2-48)-1;//将课程号转换为整型数据,图的下标用int表示的

●courseGraph->setEdge(v2,v1,1);

●}

●else

●i++;

●}

●v1++;

●courselink=courselink->getNext();

●}

●}

●voidtophelp(Graph*G,intv)//Processv

●{

●G->setMark(v,0);

●for(intw=G->first(v);wn();w=G->next(v,w))

●if(G->getMark(w)==1)

●tophelp(G,w);

●Link*courselink=list.getHead();

●for(inti=0;i

●{

●courselink=courselink->getNext();

●}

●sortedlist.insertCourse(courselink);//将拓扑排序的正序存入sortedlist中,用于课程的安排

●}

●voidtopsort(Graph*G)//Topologicalsort

●{

●inti;

●for(i=0;in();i++)//Initialize

●G->setMark(i,1);

●for(i=0;in();i++)//Dovertices

●if(G->getMark(i)==1)

●tophelp(G,i);//Callhelper

●}

●voidcourseArrange()//安排课表

●{

●Link*temp=sortedlist.getHead();

●intcount[8];

●for(inti=0;i<8;i++)

●count[i]=0;

●for(;count[0]<7&&temp!

=NULL;temp=temp->getNext())//优先安排已经预设学期的课程

●{

●if(temp->getSemaster()==1)

●count[0]++;

●elseif(temp->getSemaster()==2)

●count[1]++;

●elseif(temp->getSemaster()==3)

●count[2]++;

●elseif(temp->getSemaster()==4)

●count[3]++;

●elseif(temp->getSemaster()==5)

●count[4]++;

●elseif(temp->getSemaster()==6)

●count[5]++;

●elseif(temp->getSemaster()==7)

●count[6]++;

●elseif(temp->getSemaster()==8)

●count[7]++;

●}

●temp=sortedlist.getHead();//再按学期顺序安排已经安排学期的课程,srtedlist中的先后顺序对应了学期的先后顺序

●for(;temp!

=NULL;temp=temp->getNext())

●{

●if(count[0]

●{

●if(temp->getSemaster()==0)

●{

●temp->setSemester

(1);

●count[0]++;

●}

●}

●elseif(count[1]

●{

●if(temp->getSemaster()==0)

●{

●temp->setSemester

(2);

●count[1]++;

●}

●}

●elseif(count[2]

●{

●if(temp->getSemaster()==0)

●{

●temp->setSemester(3);

●count[2]++;

●}

●}

●elseif(count[3]

●{

●if(temp->getSemaster()==0)

●{

●temp->setSemester(4);

●count[3]++;

●}

●}

●elseif(count[4]

●{

●if(temp->getSemaster()==0)

●{

●temp->setSemester(5);

●count[4]++;

●}

●}

●elseif(count[5]

●{

●if(temp->getSemaster()==0)

●{

●temp->setSemester(6);

●count[5]++;

●}

●}

●elseif(count[6]

●{

●if(temp->getSemaster()==0)

●{

●temp->setSemester(7);

●count[6]++;

●}

●}

●else//semter8

●{

●if(temp->getSemaster()==0)

●{

●temp->setSemester(8);

●count[7]++;

●}

●}

●}

●}

voidprintSchoolTimeTable()//打印课表

{

Link*temp=sortedlist.getHead();//semter1

cout<<"coursesofsemester1:

"<

fstreamoutput;

output.open("course_table.txt");

output<<"第一学期课程:

"<

for(;temp!

=NULL;temp=temp->getNext())

{

if(temp->getSemaster()==1)

{

output<getCode()<<""<getName()<

cout<getCode()<<""<getName()<

}

}

temp=sortedlist.getHead();//semter2

cout<<"coursesofsemester2:

"<

output<<"第二学期课程:

"<

for(;temp!

=NULL;temp=temp->getNext())

{

if(temp->getSemaster()==2)

{

output<getCode()<<""<getName()<

cout<getCode()<<""<getName()<

}

}

temp=sortedlist.getHead();//semter3

cout<<"coursesofsemester3:

"<

output<<"第三学期课程:

"<

for(;temp!

=NULL;temp=temp->getNext())

{

if(temp->getSemaster()==3)

{

output<getCode()<<""<getName()<

cout<getCode()<<""<getName()<

}

}

temp=sortedlist.getHead();//semter4

cout<<"coursesofsemester4:

"<

output<<"第四学期课程:

"<

for(;temp!

=NULL;temp=temp->getNext())

{

if(temp->getSemaster()==4)

{

output<getCode()<<""<getName()<

cout<getCode()<<""<getName()<

}

}

temp=sortedlist.getHead();//semter5

cout<<"coursesofsemester5:

"<

output<<"第五学期课程:

"<

for(;temp!

=NULL;temp=temp->getNext())

{

if(temp->getSemaster()==5)

{

output<getCode()<<""<getName()<

cout<getCode()<<""<getName()<

}

}

temp=sortedlist.getHead();//semter6

cout<<"coursesofsemester6:

"<

output<<"第六学期课程:

"<

for(;temp!

=NULL;temp=temp->getNext())

{

if(temp->getSemaster()==6)

{

output<getCode()<<""<getName()<

cout<getCode()<<""<getName()<

}

}

temp=sortedlist.getHead();//semter7

cout<<"coursesofsemester7:

"<

output<<"第七学期课程:

"<

for(;temp!

=NULL;temp=temp->getNext())

{

if(temp->getSemaster()==7)

{

output<getCode()<<""<getName()<

cout<getCode()<<""<getName()<

}

}

temp=sortedlist.getHead();//semter8

cout<<"coursesofsemester8:

"<

output<<"第八学期课程:

"<

for(;temp!

=NULL;temp=temp->getNext())

{

if(temp->getSemaster()==8)

{

output<getCode()<<""<getName()<

cout<getCode()<<""<getName()<

}

}

output.close();

}

6.运行结果:

●测试数据选择

●(课程编码,课程名称,开课学时,预设开课学期,先决条件)

("c01","程序设计基础",5,0,"")("c02","离散数学",6,0,"c01")

●("c03","数据结构",4,0,"c01c02")("c04","汇编语言",5,0,"c01")

●("c05","算法设计",4,0,"c03c04")("c06","计算机组成原理",6,0,"")

●("c07","微机原理",4,0,"c03")("c08","单片机应用",3,0,"c03")

●("c09","编译原理",5,0,"c03")("c10","操作系统原理",4,0,"c03")

●("c11","数据库原理",5,0,"c03")("c12","高等数学",6,0,"")

●("c13","线性代数",6,0,"")("c14","数值分析",6,0,"c12")

●("c15","普通物理",6,0,"c12")("c16","计算机文化",3,0,"")

●("c17","计算机系统结构",6,0,"c06")("c18","

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 企业管理

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1