提交 1b39e46f 创建 作者: 宋海霞's avatar 宋海霞

template

上级
流水线 #478 已失败 于阶段
/Debug
/Release
*.bak
image: "registry.cn-hangzhou.aliyuncs.com/engintime/ubuntu_16.04_cpp:latest"
stages:
- make
- case1
- teacher-check
variables:
TEMPLATE_REPO: "engintime/ds-lab/Project-Template/Lab013.git"
.codecode-runner: &codecode-runner
tags:
- ubuntu-16.04
- short-job
make:
stage: make
<<: *codecode-runner
script:
- make
- execscore.sh 40
only:
- master
case1:
stage: case1
<<: *codecode-runner
script:
- make
- ./app > user_output.txt
- diff output.txt user_output.txt -b -B -y -i --suppress-common-lines
- execscore.sh 100
only:
- master
teacher-check:
stage: teacher-check
<<: *codecode-runner
script:
- make
- ./app
- git clone ${CODECODE_PROTOCOL}gitlab-ci-token:${CI_JOB_TOKEN}@${CODECODE_DOMAIN}/${TEMPLATE_REPO} template
- diff template/.gitlab-ci.yml .gitlab-ci.yml -b -B -y -i --suppress-common-lines
- diff template/output.txt output.txt -b -B -y -i --suppress-common-lines
- fileidentity.sh
only:
- master
when: manual
allow_failure: false
<?xml version="1.0" encoding="gb2312"?>
<OSLProject Version="1.00" Name="HuffmanTree" SubjectID="cf4dda31-1275-49ef-9b0f-36a6eff372e4" ProjectTemplateID="9a14bb7b-426a-45ad-a83b-0c72fc05e081">
<Configurations>
<Configuration Name="Debug">
<Tool Name="PreBuildEventTool"/>
<Tool Name="CustomBuildTool"/>
<Tool Name="GCCCompilerTool" PreprocessorDefinitions="_DEBUG" GenerateDebugInformation="-1"/>
<Tool Name="PreLinkEventTool"/>
<Tool Name="GCCLinkerTool" AdditionalDependencies="&quot;$(DSLInstallDir)Dump\lib\HuffmanTree_Demo.o&quot;"/>
<Tool Name="PostBuildEventTool"/>
<VisualContext>
<WatchPoints>
<WatchPoint FunctionName="HuffmanTree" ObserverID="D6BD5BEA-B49C-414f-8CA7-2757AA5AD4F0">
</WatchPoint>
</WatchPoints>
</VisualContext>
</Configuration>
<Configuration Name="Release">
<Tool Name="PreBuildEventTool"/>
<Tool Name="CustomBuildTool"/>
<Tool Name="GCCCompilerTool" PreprocessorDefinitions="NDEBUG"/>
<Tool Name="PreLinkEventTool"/>
<Tool Name="GCCLinkerTool"/>
<Tool Name="PostBuildEventTool"/>
</Configuration>
</Configurations>
<Files>
<Filter Name="ͷļ" Filter="h;hpp;hxx">
<File RelativePath=".\HuffmanTree.h">
</File>
</Filter>
<Filter Name="Դļ" Filter="cpp;c;cc;cxx">
<File RelativePath=".\main.c">
</File>
</Filter>
<File RelativePath=".\makefile">
</File>
<File RelativePath=".\output.txt">
</File>
<File RelativePath=".\readme.md">
</File>
</Files>
</OSLProject>
#ifndef _HUFFMANTREE_H_
#define _HUFFMANTREE_H_
//
// 在此处包含 C 标准库头文件
//
#include <stdio.h>
//
// 在此处包含其他头文件
//
//
// 在此处定义数据结构
//
#define MAXVALUE 1000 // 定义最大权值
// 节点的结构
typedef struct HtNode{
int Weight; // 节点权值
int parent, lchild, rchild; // 父节点和左右孩子在数组中的下标
}HtNode;
// 哈夫曼树的结构
typedef struct _HtTree{
int Count; // 叶子节点的个数
int Root; // 哈夫曼根节点在数组中的下标
struct HtNode *HtArray; // 存放 2*Count-1 个节点的数组
}HtTree, *PHtTree;
//
// 在此处声明函数
//
PHtTree HuffmanTree(PHtTree pTree, int Count); // 哈夫曼树的构造函数
void OutputResult(PHtTree pTree, int Length);
//
// 在此处声明全局变量
//
#endif /* _HUFFMANTREE_H_ */
添加文件
#include "HuffmanTree.h"
int main(int argc, char* argv[])
{
int i;
//
// 初始化数组
//
int WeightArray[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41};
int Count = sizeof(WeightArray) / sizeof(WeightArray[0]);
int HuffmanLength = 2 * Count - 1;
//
// 初始化赫夫曼树
//
PHtTree pTree = (PHtTree)malloc(sizeof(HtTree));
pTree->HtArray = (HtNode*)malloc(sizeof(HtNode) * HuffmanLength);
for(i = 0; i < HuffmanLength; i++)
{
pTree->HtArray[i].lchild = -1;
pTree->HtArray[i].rchild = -1;
pTree->HtArray[i].parent = -1;
pTree->HtArray[i].Weight = i < Count ? WeightArray[i] : -1;
}
//
// 构造赫夫曼树
//
pTree = HuffmanTree(pTree, Count);
OutputResult(pTree, HuffmanLength);
//
// 销毁赫夫曼树
//
free(pTree->HtArray);
free(pTree);
return 0;
}
/*
功能:
用给定的一组权值构造赫夫曼树。
参数:
pTree -- 赫夫曼树结构体指针
Count -- 数组元素个数
返回值:
返回赫夫曼树结构体指针。
*/
PHtTree HuffmanTree(PHtTree pTree, int Count)
{
int i, j; // 游标,主要用于查找最小节点和次小节点的下标
int Index1, Index2; // 存放最小和次小节点下标
int Number1, Number2; // 存放最小和次小节点权值
//
// TODO: 在此添加代码
//
return pTree;
}
void OutputResult(PHtTree pTree, int Length)
{
int i;
printf("subscript\tweight\tparent\tlchild\trchild\n");
for(i = 0; i < Length; i++)
{
printf("%d", i);
printf("\t\t%d",pTree->HtArray[i].Weight);
printf("\t\t%d",pTree->HtArray[i].parent);
printf("\t\t%d",pTree->HtArray[i].lchild);
printf("\t\t%d\n",pTree->HtArray[i].rchild);
}
}
app:main.o
gcc -o app main.o
main.o:main.c HuffmanTree.h
gcc -c -w -O3 -std=c99 -fsigned-char main.c -o main.o
subscript weight parent lchild rchild
0 2 13 -1 -1
1 3 13 -1 -1
2 5 14 -1 -1
3 7 15 -1 -1
4 11 16 -1 -1
5 13 16 -1 -1
6 17 17 -1 -1
7 19 18 -1 -1
8 23 18 -1 -1
9 29 19 -1 -1
10 31 20 -1 -1
11 37 21 -1 -1
12 41 21 -1 -1
13 5 14 0 1
14 10 15 2 13
15 17 17 3 14
16 24 19 4 5
17 34 20 6 15
18 42 22 7 8
19 53 22 16 9
20 65 23 10 17
21 78 23 11 12
22 95 24 18 19
23 143 24 20 21
24 238 -1 22 23
# 阅读实验源代码
**main.c文件**
在main函数中首先创建了赫夫曼树的初始状态,然后调用函数HuffmanTree构造赫夫曼树。
在main函数的后面,定义了赫夫曼函数HuffmanTree,此函数的函数体还不完整,留给读者完成。
**HuffmanTree.h文件**
定义了与赫夫曼树相关的数据结构并声明了相关的操作函数。
# 在演示模式下调试项目
**按照下面的步骤调试项目:**
1. 按F7生成项目。
2. 在演示模式下,按F5启动调试项目。程序会在观察点函数的开始位置中断。
3. 重复按F5,直到调试过程结束。
在调试的过程中,每执行“演示流程”窗口中的一行后,仔细观察“可视化数据”窗口内容所发生的变化,理解赫夫曼函数的执行过程。“可视化数据”窗口显示的数据信息(如下图所示),包括:
- 赫夫曼树的存储结构,包括内部结点和外部结点。
- 可以看到最小和次小节点权值及游标。可以显示最小和次小结点的查找过程,已查找出的最小和次小结点的填充色为灰色。
- 赫夫曼树的存储结构,对应的当前值与上一次值不相同时,用红色的字体显示出来。
- 在根据已选出的最小和次小结点,构造的哈夫曼树中,内部结点使用双线的边框,外部结点使用单线的边框。
![赫夫曼树的构造](./img/13.png)
# 编写源代码并通过验证
**按照下面的步骤继续实验:**
1. 为HuffmanTree函数编写源代码。注意,尽量使用已定义的局部变量。
2. 按F7生成项目。如果生成失败,根据“输出”窗口中的提示信息修改源代码中的语法错误。
3. 按Alt+F5启动验证。如果验证失败,可以使用“输出”窗口中的“比较”功能,或者在“非演示模式”下按F5启动调试后重复按F10单步调试读者编写的源代码,从而定位错误的位置,然
后回到步骤1。
# 教师参考答案
访问教师参考答案[domain relative url](engintime/ds-lab/teachers-packet/Lab013.git)
\ No newline at end of file
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论