软基第四次上机实验报告
EX4.1
一、实验流程
1)二叉树结点类型定义为:
typedef struct bnode
{ int data;
struct bnode *lc, *rc;
}bnode_type;
2)编写二叉树的创建函数,可以是排序二叉树的创建思路(见教材),或者以先序遍历为框架。
3)编写中序遍历函数;
4)编写后序遍历函数;
5)编写先序遍历函数;
6)编写main()函数,先调用create函数,建立一颗二叉排序树;然后分别调用中序、后序、先序遍历函数,将二叉树的先序、中序和后序遍历序列输出到屏幕上。
二、程序代码
#include<stdio.h>
#include<malloc.h>
typedef struct bnode
{
int data;
struct bnode *lc, *rc;
}bnode;
void inorder(bnode *t)
{
if(t==NULL)
{
printf("the tree is null\n");
return;
}
else
{
if(t->lc!=NULL)
inorder(t->lc);
printf("%d\n",t->data);
if(t->rc!=NULL)inorder(t->rc);
}
}
void postorder(bnode *t)
{
if(t==NULL)
{
printf("the tree is null!\n");
return;
}
else
{
if(t->lc!=NULL)
postorder(t->lc);
if(t->rc!=NULL)
postorder(t->rc);
printf("%d\n",t->data);
}
}
void preorder(bnode *t)
{
if(t==NULL)
{
printf("the tree is null!\n");
return;
}
else
{
printf("%d\n",t->data);
if(t->lc!=NULL)
preorder(t->lc);
if(t->rc!=NULL)
preorder(t->rc);
}
}
void create(bnode **t)
{
int x,n,i;
bnode *p,*q; *t=NULL;
printf("输入数据元素的个数:\n");
scanf("%d",&n);
printf("分别输入这些元素:\n");
for(i=0;i<n;i++)
{
scanf("%d",&x);
p=(bnode*)malloc(sizeof(bnode));
p->lc=NULL;
p->rc=NULL;
p->data=x;
if(*t==NULL)
*t=p;
else
{
q=*t;
while(q!=NULL)
{
if(q->data>x)
{
if(q->lc!=NULL)
q=q->lc;
else
{
q->lc=p;
q=NULL;
}
}
else
{
if(q->rc!=NULL)q=q->rc;
else
{
q->rc=p;
q=NULL;
}
}
}
}
}
}
void main()
{
bnode **a;
a=(bnode **)malloc(sizeof(bnode*));
create(a);
printf("此树的中序遍历:\n");
inorder(*a);
printf("此树的后序遍历:\n");
postorder(*a);
printf("此树的先序遍历:\n");
preorder(*a);
}
三、测试数据
输入:输入数据元素的个数:8
输入的数据:5 6 9 8 4 3 6 10
输出:
中序遍历:3 4 5 6 6 8 9 10
后序遍历:3 4 6 8 10 9 6 5
先序遍历;:5 4 3 6 9 8 6 10
四、上机遇到的问题:
调用函数出错;解决办法:问同学
五、实际运行结果:
如图所示
六、 小结
掌握不够,还的努力
EX4.2
一、 程序流程
输入一组数(权值),编写算法建立哈夫曼树,输出每个权值对应的二进制编码。
二、程序代码
#include<stdio.h>
#include<iostream.h>
#include<iomanip.h>
const int n=8;
const int m=2*n-1;
struct tree
{
float weight;
int parent;
int lch,rch;
};
struct codetype
{
int bits[n+1];
int start;
char ch;
};
tree hftree[m+1];
codetype code[n+1];
void creathuffmantree() //建立哈夫曼树
{
int i,j,p1,p2;
float s1,s2;
for(i=1;i<=m;i++)
{
hftree[i].parent=0;
hftree[i].lch=0;
hftree[i].rch=0;
hftree[i].weight=0;
}
cout<<"请输入"<<n<<"个权值"<<endl;
for(i=1;i<=n;i++)
cin>>hftree[i].weight;
for(i=n+1;i<=m;i++)
{
p1=p2=0;
s1=s2=32767;
for(j=1;j<=i-1;j++)
if(hftree[j].parent==0)
if(hftree[j].weight<s1)
{
s2=s1;
s1=hftree[j].weight;
p2=p1;
p1=j;
}
else if(hftree[j].weight<s2)
{s2=hftree[j].weight;p2=j;}
hftree[p1].parent=i;
hftree[p2].parent=i;
hftree[i].lch=p1;
hftree[i].rch=p2;
hftree[i].weight=hftree[p1].weight+hftree[p2].weight;
}
}
void huffcode() //哈夫曼编码
{
codetype cd;
int c,p,i;
for(i=1;i<=n;i++)
{
cd.start=n+1;
cd.ch=96+i;//第一个树叶对应字母a,其余依此类推
c=i;
p=hftree[i].parent;
while(p!=0)
{
cd.start--;
if(hftree[p].lch==c)
cd.bits[cd.start]=0;
else
cd.bits[cd.start]=1;
c=p;
p=hftree[p].parent;
}
code[i]=cd;
}
for(i=1;i<=n;i++)
{
cout<<"字符"<<code[i].ch<<"的权值为:"<<hftree[i].weight<<setw(5)<<"编码为:";
for(int j=code[i].start;j<=n;j++)
cout<<code[i].bits[j]<<" ";
cout<<endl;
}
}
void trancode() //哈夫曼译码
{
int i=m;
char b;
cout<<"请输入一串二进制编码(0、1以外的数结束)"<<endl;
cin>>b;
while((b=='0')||(b=='1'))
{
if(b=='0')
i=hftree[i].lch;
else
i=hftree[i].rch;
if(hftree[i].lch==0)
{
cout<<code[i].ch;
i=m;
}
cin>>b;
}
}
void main()
{
creathuffmantree();
huffcode();
trancode();
cout<<endl;
}
三、测试数据
因比较复杂,我们直接看结果吧
四、上机遇到的问题
调试时遇到问题;解决办法:问同学
五、实际运行结果
七、 小结
感觉这部分还不会,要多多练习
学号:
选课号:
姓名:
第二篇:软基实验报告
软件技术基础
实验报告
实验内容:(一)数字校园
(二)情报压缩与传输
姓名:王锐
学院:英才实验学院
学号:2013050108005
实验一、数字校园
(一)题目内容及要求
设计一个数字校园程序,提供校园内主要地点,并能查看任意两地之间的最短路径和长度。
要求:
1.宿舍楼、教学楼、主楼、图书馆、食堂、超市等等作为节点;
2.各节点之间有道路相连,每条道路有长度,通行流量;
3.设计简易数字校园,实现:最优路线规划,高峰期道路推荐;
(二)设计思路
每个地点节点存有相应的编号、名称等信息,由各个地点之间的路径长度构造出整个校园地图(若无路径则设为无穷大),并保存在路径路组中。运用弗洛伊德算法计算各地点之间的最优路径以及路径长度。在用户界面设计一个人机友好的主界面,包括打印出整个校园地图,附上各地点名称,供用户选择地点以及查看任意两地之间的路径长度等操作内容。
(三)程序源代码
#define INFINITY 10000 /*无穷大*/
#define MAX_VERTEX_NUM 40
#define MAX 40
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
typedef struct ArCell
{
int adj; //路径长度
}ArCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct infotype //图中顶点表示校园中主要地点,存放地点的编号、名称等信息,
{
char name[30];
int num;
}infotype;
typedef struct MGraph
{
infotype vexs[MAX_VERTEX_NUM];//地点
AdjMatrix arcs;//路径数组
int vexnum,arcnum;//地点数,路径长度记录
}MGraph;
MGraph b;//全局变量
void cmd(void);//在主函数中用来调用其他应用子函数的函数声明
MGraph InitGraph(void);//初始化,用来构造学校地图的子函数 返回MGraph类型
void Menu(void);//菜单函数;
void Browser(MGraph *G);//调用MGraph类型的地址,进行
void Floyd(MGraph *G);//佛洛伊德算法
void browse_view_distribute();//查看地点分布图
void list(MGraph *G);//地点列表
void print(MGraph *G);//打印输出子函数
/******************************************************/
int main(void)
{
system("color Ff");//设置调试窗口背景和字体颜色
system("mode con: cols=140 lines=130");//设置调试窗口的大小
cmd();//用该函数来调用其他需要用到的函数
}
/******************************************************/
void cmd(void)//用来调用其他需要用到的函数的子函数
{
int i;
b=InitGraph();//构造校园地图
Browser(&b);//Menu();//调用菜单函数
scanf("%d",&i);
while(i!=3)
{
switch(i)
{
case 1:system("cls");/*ShortestPath_DIJ(&b);*/browse_view_distribute();Browser(&b);break;
case 2:system("cls");list(&b);Floyd(&b);Browser(&b);break;
case 3:exit(0);break;
default:break;
}
scanf("%d",&i);
}
printf("欢迎下次继续使用 !\n\n");
}
//*******************************************************************
MGraph InitGraph(void)//构造校园地图
{
MGraph G;
int i,j;
G.vexnum=7;//地点数量
G.arcnum=9;//路径数量
for(i=1;i<=G.vexnum;i++)
G.vexs[i].num=i;//对地点进行对应编号
/*对对应的地点编号进行命名,输入简介*/
strcpy(G.vexs[1].name,"主楼");
strcpy(G.vexs[2].name,"图书馆");
strcpy(G.vexs[3].name,"喷泉");
strcpy(G.vexs[4].name,"超市");
strcpy(G.vexs[5].name,"教学楼");
strcpy(G.vexs[6].name,"宿舍楼");
strcpy(G.vexs[7].name,"食堂");
//对有路的各地点之间的路径长度进行设置,没路的设置为无穷大
for(i=1;i<=G.vexnum;i++)
for(j=1;j<=G.vexnum;j++)
G.arcs[i][j].adj=INFINITY;
G.arcs[1][2].adj=200;
G.arcs[2][3].adj=150;
G.arcs[2][4].adj=500;
G.arcs[3][4].adj=250;
G.arcs[3][5].adj=50;
G.arcs[4][6].adj=200;
G.arcs[5][7].adj=200;
G.arcs[6][7].adj=100;
//无向图的路径是相互的
for(i=1;i<=G.vexnum;i++)
for(j=1;j<=G.vexnum;j++)
G.arcs[j][i].adj=G.arcs[i][j].adj;
return G;
}//InitGraph end
//*******************************************************************
//输出所有地点信息
void Browser(MGraph *G)
{
int v;
printf("┏━━┳━━━━━━┓\n");
printf("┃编号┃景点名称 ┃\n");
for(v=1;v<=13;v++){
switch(v+3){
case 4:printf("┃%-4d┃%-12s┃",G->vexs[v].num,G->vexs[v].name);
printf(" 电子科技大学校园导游图\n");break;
case 5:printf("┃%-4d┃%-12s┃",G->vexs[v].num,G->vexs[v].name);
printf(" ┏━━┳━━━━━━━━━━━━━━━━┓\n");break;
case 6:printf("┃%-4d┃%-12s┃",G->vexs[v].num,G->vexs[v].name);
printf(" ┃编号┃ 实 现 的 功 能 ┃\n");break;
case 7:printf("┃%-4d┃%-12s┃",G->vexs[v].num,G->vexs[v].name);
printf(" ┣━━╋━━━━━━━━━━━━━━━━┫\n");break;
case 8:printf("┃%-4d┃%-12s┃",G->vexs[v].num,G->vexs[v].name);
printf(" ┃ 1 ┃ 查 看 地 点 分 布 图 ┃\n");break;
case 9:printf("┃%-4d┃%-12s┃",G->vexs[v].num,G->vexs[v].name);
printf(" ┣━━╋━━━━━━━━━━━━━━━━┫\n");break;
case 10:printf("┃%-4d┃%-12s┃",G->vexs[v].num,G->vexs[v].name);
printf(" ┃ 2 ┃ 查 找 两 地 点 间 最 短 距 离 ┃\n");break;
case 11:printf("┗━━┻━━━━━━┛");
printf(" ┣━━╋━━━━━━━━━━━━━━━━┫\n");break;
case 12:printf(" ");
printf(" ┃ 3 ┃ 退 出 系 统 ┃\n");break;
case 13:printf(" ");
printf(" ┗━━┻━━━━━━━━━━━━━━━━┛\n");break;
default:break;
}
}
printf("输入你的操作编号:");
}
//*******************************************************************
void Floyd(MGraph *G)
{
int v,u,i,w,k,j,flag=1,p[8][8][8],D[8][8];
for(v=1;v<=G->vexnum;v++)
for(w=1;w<=G->vexnum;w++)
{
D[v][w]=G->arcs[v][w].adj;
for(u=1;u<=G->vexnum;u++)
p[v][w][u]=0;
if(D[v][w]<INFINITY)
{
p[v][w][v]=1;p[v][w][w]=1;
}
}
for(u=1;u<=G->vexnum;u++)
for(v=1;v<=G->vexnum;v++)
for(w=1;w<=G->vexnum;w++)
if(D[v][u]+D[u][w]<D[v][w])
{
D[v][w]=D[v][u]+D[u][w];
for(i=1;i<=G->vexnum;i++)
p[v][w][i]=p[v][u][i] || p[u][w][i];
}
while(flag)
{
printf("请输入出发点和目的地的编号:");
scanf("%d%d",&k,&j);
if(k<=0 || k>G->vexnum || j<=0 || j>G->vexnum)
{
printf("地点编号不存在!请重新输入出发点和目的地的编号:");
scanf("%d%d",&k,&j);
}
if(k==j)
{
printf("出发点和目的地一样!请重新输入出发点和目的地的编号:");
scanf("%d%d",&k,&j);
}
if(k>0 && k<=G->vexnum && j>0 && j<=G->vexnum)
flag=0;
}
printf("\n最短游览路线:%s",G->vexs[k].name);
if(k>j){
for(u=G->vexnum;u>0;u--)
if(p[k][j][u] && k!=u && j!=u)
printf("-->%s",G->vexs[u].name);}
if(k<j){
for(u=1;u<=G->vexnum;u++)
if(p[k][j][u] && k!=u && j!=u)
printf("-->%s",G->vexs[u].name);}
printf("-->%s",G->vexs[j].name);
printf(" 总路线长%dm\n",D[k][j]);
}//Floyd end
//****************************************************************
//*******************************************************************
void browse_view_distribute()
{//查看地点分布图
printf("*******************************************\n");
printf("* *\n");
printf("* ┏━━━┓ *\n");
printf("* ┃ ┃ *\n");
printf("* ┏┅┅┅┅┫图书馆┣┅┅┅┅┓ *\n");
printf("* ┇ ┃ ┃ ┇ *\n");
printf("* ┇ ┗━┳━┛ ┇ *\n");
printf("* ┏━┻━┓ ┏━┻━┓ ┏━┻━┓ *\n");
printf("* ┃ ┃ ┃ ┃ ┃ ┃ *\n");
printf("* ┃ 主楼 ┣┅┅┫ 喷泉 ┣┅┅┫ 超市 ┃ *\n");
printf("* ┃ ┃ ┃ ┃ ┃ ┃ *\n");
printf("* ┗━━━┛ ┗━┳━┛ ┗━┳━┛ *\n");
printf("* ┏━┻━┓ ┏━┻━┓ *\n");
printf("* ┃ ┃ ┃ ┃ *\n");
printf("* ┃教学楼┣┅┅┫宿舍楼┃ *\n");
printf("* ┃ ┃ ┃ ┃ *\n");
printf("* ┗━┳━┛ ┗━┳━┛ *\n");
printf("* ┇ ┏━━┓ ┇ *\n");
printf("* ┇ ┃ ┃ ┇ *\n");
printf("* ┗┅┫食堂┣┅┛ *\n");
printf("* ┃ ┃ *\n");
printf("* ┗━━┛ *\n");
printf("*******************************************\n");
printf("请按任意键继续!");
getch();
printf("\n");
}
//*******************************************************************
void print(MGraph *G)
{
int v,w,t=0;
for(v=1;v<=G->vexnum;v++)
for(w=1;w<=G->vexnum;w++)
{
if(G->arcs[v][w].adj==INFINITY)
printf("∞ ");
else printf("%-7d",G->arcs[v][w].adj);
t++;
if(t%G->vexnum==0)
printf("\n");
}
}
//*******************************************************************
void list(MGraph *G)
{
int v;
printf("┏━━┳━━━━━━┓\n");
printf("┃编号┃地点名称 ┃\n");
for(v=1;v<=G->vexnum;v++)
printf("┃%-4d┃%-12s┃\n",G->vexs[v].num,G->vexs[v].name);
printf("┗━━┻━━━━━━┛\n");
}
//*******************************************************************
(五)运行结果
主界面:
地点分布图:
查找任意两地最短路径:
实验二、情报压缩与传输
(一)题目内容与要求
对报文进行压缩编码,译码,要求能够正确地编译码,同时要使编码长度最短。
要求
输入一段明文,给出相应的密文
给出一段密文,能还原出相应的密文
各给出至少五个实例的结果
(二)设计思路
哈夫曼编码(Huffman Coding)是一种编码方式,以哈夫曼树─即最优二叉树,带权路径长度最小的二叉树,经常应用于数据压缩。是指使用一张特殊的编码表将源字符(例如某文件中的一个符号)进行编码。当利用哈夫曼编码对一篇英文进行压缩时,e极有可能用一个位(bit)来表示,而z则可能花去25个位(不是26)。用普通的表示方法时,每个英文字母均占用一个字节(byte),即8个位。二者相比,e使用了一般编码的1/8的长度,z则使用了3倍多。倘若能实现对于英文中各个字母出现概率的较准确的估算,就可以大幅度提高无损压缩的比例。
因此采用哈夫曼编码是一种合理高效的方法。
此程序用表1给出的字符集和频度的实际统计数据建立哈夫曼树,在输入字符串和相应的权值(频度)后,提供包括初始化、编码、译码、印代码文件和退出五个选项的功能菜单。其中初始化是建立哈夫曼树的过程,并将结果保存在hfmTree文件中,编码过程包括将正文写入ToBeTree文件、编码、输出编码和将结果保存在CodeFile文件中几个过程,印代码文件即打印CodeFile文件内容。
表1
字符空格 A B C D E F G H I J K L M
频度186 64 13 22 32 103 21 15 47 57 1 5 32 20
字符 N O P Q R S T U V W X Y Z
频度57 63 15 1 48 51 80 23 8 18 1 16 1
(三)程序源代码
#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <stdlib.h>
typedef struct
{
char data;
int weight;
int parent,lchild,rchild;
}HTNode,*HuffmanTree;
typedef char * * HuffmanCode;
void Select(HuffmanTree &HT,int n,int m)
{
HuffmanTree p=HT;
int tmp;
for (int j=n+1;j<=m;j++)
{
int tag1,tag2,s1,s2;
tag1=tag2=32767;
for (int x=1;x<=j-1;x++)
{
if (p[x].parent==0&&p[x].weight<tag1)
{
tag1=p[x].weight;
s1=x;
}
}
for (int y=1;y<=j-1;y++)
{
if (p[y].parent==0&&y!=s1&&p[y].weight<tag2)
{
tag2=p[y].weight;
s2=y;
}
}
if (s1>s2) //将选出的两个节点中的序号较小的始终赋给s1
{
tmp=s1;
s1=s2;
s2=tmp;
}
p[s1].parent=j;
p[s2].parent=j;
p[j].lchild=s1;
p[j].rchild=s2;
p[j].weight=p[s1].weight+p[s2].weight;
}
}
void HuffmanCoding(HuffmanTree &HT,int n,char *w1,int*w2)
{
int m=2*n-1;
if (n<=1) return;
HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode));
HuffmanTree p=HT;
int i;
for (i=1;i<=n;i++)
{
p[i].data=w1[i-1];
p[i].weight=w2[i];
p[i].parent=p[i].lchild=p[i].rchild=0;
}
for (i=1;i<=m;i++)
{
p[i].weight=p[i].parent=p[i].lchild=p[i].rchild=0;
}
Select(HT,n,m);
ofstream outfile; //生成hfmTree文件
outfile.open("hfmTree.txt",ios::out);
for (i=1;i<=m;i++)
{
outfile<<HT[i].weight<<"\t"<<HT[i].parent<<"\t"<<HT[i].lchild
<<"\t"<<HT[i].rchild<<"\t"<<endl;
}
outfile.close();
cout<<"初始化结果已保存在hfmTree文件中\n";
}
void ToBeTree() //将正文写入文件ToBeTree中
{
ofstream outfile;
outfile.open("ToBeTree.txt",ios::out);
outfile<<"THIS PROGRAM IS MYFAVORITE";
outfile.close();
}
void Encoding(HuffmanTree &HT,int n) //编码
{
HuffmanCode HC;
HC=(HuffmanCode)malloc((n+1)*sizeof(char *));
char *cd;
cd=(char *)malloc(n*sizeof(char));
cd[n-1]='\0';
int k;
for (k=1;k<=n;k++)
{
int start=n-1;
for (int c=k,f=HT[k].parent;f!=0;c=f,f=HT[f].parent)
{
if (HT[f].lchild==c) cd[--start]='0';
else cd[--start]='1';
}
HC[k]=(char *)malloc((n-start)*sizeof(char));
strcpy(HC[k],&cd[start]);
}
cout<<"输出哈夫曼编码:"<<endl;
for (int h=1;h<=n;h++) //输出编码
{
cout<<HT[h].data<<":";
cout<<HC[h];
cout<<" ";
if (h%8==0) cout<<endl;
}
cout<<endl<<"输出正文编码:"<<endl;
ToBeTree();
//读取TOBETREE文件里的正文,并进行编码
fstream infile;
infile.open("ToBeTree.txt",ios::in);
char s[80];
while (!infile.eof())
{
infile.getline(s,sizeof(s));
}
infile.close();
fstream outfile;
outfile.open("CodeFile.txt",ios::out);
int count=0;
int h;
for (h=0;s[h]!='\0';h++)
{
for (k=1;k<=n;k++)
if (s[h]==HT[k].data)
{
cout<<HC[k];
cout<<" ";
count++;
outfile<<HC[k];
break;
}
if (count%9==0) cout<<endl; //每输出7个换行
}
outfile.close();
cout<<"\n编码结果已保存在文件CodeFile中.";
cout<<endl;
}
void Decoding(HuffmanTree &HT,int n) //译码
{
int f=2*n-1;
fstream infile;
infile.open("CodeFile.txt",ios::in);
char s[1000];
while (!infile.eof())
{
infile.getline(s,sizeof(s));
}
infile.close();
int i=0;
int j=0;
fstream outfile;
outfile.open("TextFile.txt",ios::out);
while (s[i]!='\0')
{
f=2*n-1;
while (HT[f].lchild!=0)//以f对应的节点的左孩子的值==0作为结束
{
if (s[j]=='0') f=HT[f].lchild;
else f=HT[f].rchild;
j++;
}
i=j;
cout<<HT[f].data;
outfile<<HT[f].data;
}
outfile.close();
cout<<"\n译码结果已保存在文件TextFile中.";
cout<<endl;
}
void Print() //印代码文件
{
int count=0;
fstream infile;
infile.open("CodeFile.txt",ios::in);
char s[1000];
while (!infile.eof())
{
infile.getline(s,sizeof(s));
for (int i=0;s[i]!='\0';i++)
{
cout<<s[i];
count++;
if (count%50==0) cout<<endl; //在终端上每行显示50个代码
}
}
infile.close();
cout<<endl;
}
char menu() //菜单函数
{
cout<<"功能菜单如下:"<<endl;
cout<<"* * * * * * * * * * * * * * * * * * * * *"<<endl;
cout<<" I:初始化(Initialization) "<<endl;
cout<<" E:编码(Encoding) "<<endl;
cout<<" D:译码(Decoding) "<<endl;
cout<<" P:印代码文件(Print) "<<endl;
cout<<" Q:退出(Exit) "<<endl;
cout<<"* * * * * * * * * * * * * * * * * * * * *"<<endl;
cout<<"请输入功能字符:";
char ch;
cin>>ch;
return ch;
}
int main()
{
int n;
int Array[100];
char cArray[100];
HuffmanTree HT;
cout<<"输入n个字符:";
cin.getline(cArray,100);
n=strlen(cArray);
cout<<"一共"<<n<<"个字符.\n";
cout<<"依次输入各个字符的权值:"<<endl;
for (int i=1;i<=n;i++) cin>>Array[i];
int tag;
char x=menu();
while (1)
{
switch (x)
{
case 'I':
HuffmanCoding(HT,n,cArray,Array);
break;
case 'E':
Encoding(HT,n);
break;
case 'D':
Decoding(HT,n);
break;
case 'P':
Print();
break;
case 'Q':
tag=0;
cout<<"结束"<<endl;
break;
default:
cout<<"输入错误!"<<endl;
}
if (tag==0) break;
cout<<"y(继续) or n(退出)"<<endl;
char ch;
cin>>ch;
if (ch=='y')
{
cout<<"请输入功能字符:";
char c;
cin>>c;
x=c;
}
else exit(1);
}
}
(四)运行结果
三、心得与体会
在课程中对有关树和图的知识学习时感到有些吃力,对一些概念和算法的理解有很大欠缺,通过实际题目的程序设计与调试,了解和学习到在算法实现中哪些是重点,哪些是容易忽视的地方,对树和图的应用有了更加深入的理解和认识,包括以前比较生疏的文件操作内容,在此次实验内容中也进行了多次操纵,达到了比较熟练的程度。
对于第一个实验,基本满足了作为电子地图的要素,但是地图有些简单,没有严格按照实际情况进行度量和赋予权重,只能作为大致参考,如要作为可实际使用的程序还有待改善。对于第二个实验,编码部分基本没有缺陷,但译码部分有时会出现差错,由于能力有限,没能彻底解决;而且各字符的使用频度寻求了参考,可以再设计一个函数读取文本文件来获取频度。
总体来说,两个实验都基本达到了要求,自己的编程能力也得到了进一步提高,算是对一学期的数据结构与算法课程学习画上了一个圆满的句号。