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

template

上级
流水线 #485 已失败 于阶段
/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/Lab021.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="BinarySearch" SubjectID="cf4dda31-1275-49ef-9b0f-36a6eff372e4" ProjectTemplateID="67c1e38c-0975-4621-9092-34b324264151">
<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\BinarySearch_Demo.o&quot;"/>
<Tool Name="PostBuildEventTool"/>
<VisualContext>
<WatchPoints>
<WatchPoint FunctionName="BinarySearch" ObserverID="72CB98D6-40F6-4c8e-9E8A-A503495590B2">
</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=".\SeqTable.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 _SEQTABLE_H_
#define _SEQTABLE_H_
//
// 在此处包含 C 标准库头文件
//
#include <stdio.h>
//
// 在此处包含其他头文件
//
//
// 在此处定义数据结构
//
// 线性表中的元素
typedef int KeyType;
typedef struct ElemType {
KeyType key; // 主关键字
// 省略其他数据,不再定义
}ElemType;
// 线性表的顺序存储结构
typedef struct SeqTable {
ElemType* elem; // 数据元素存储空间基址,建表时按实际长度分配,
// 0 号单元留空,从 1 号单元开始使用。
int length; // 表长度
}SeqTable;
//
// 在此处声明函数
//
int BinarySearch(SeqTable* pST, KeyType key);
void InitSeqTable(SeqTable* pST);
void DeleteSeqTable(SeqTable* pST);
//
// 在此处声明全局变量
//
#endif /* _SEQTABLE_H_ */
#include "SeqTable.h"
int main(int argc, char* argv[])
{
SeqTable ST; // 线性表
int i, pos1, pos2;
//
// 初始化线性表
//
InitSeqTable(&ST);
//
// 折半查找
//
pos1 = BinarySearch(&ST, 60);
pos2 = BinarySearch(&ST, 30); // 查找失败
printf("%d %d\n", pos1, pos2);
//
// 销毁线性表
//
DeleteSeqTable(&ST);
return 0;
}
/*
功能:
折半查找。
参数:
pST -- 线性表指针
key -- 查找关键字
返回值:
如果查找成功返回关键字在表中的位置
如果查找失败返回 0
*/
int BinarySearch(SeqTable* pST, KeyType key)
{
int low = 1;
int high = pST->length;
int mid;
//
// TODO: 在此添加代码
//
return 0;
}
/*
功能:
初始化线性表。
参数:
pST -- 线性表指针
*/
// 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
int InitArray[] = { 2, 4, 11, 18, 25, 32, 39, 46, 53, 60, 67, 74 }; // 顺序必须为从小到大
void InitSeqTable(SeqTable* pST)
{
int i;
pST->length = sizeof(InitArray) / sizeof(InitArray[0]);
pST->elem = (ElemType*)malloc((pST->length + 1) * sizeof(ElemType)); // 因为单元 0 留空,所以长度必须加 1
for(i=1; i<=pST->length; i++)
pST->elem[i].key = InitArray[i-1];
}
/*
功能:
销毁线性表。
参数:
pST -- 线性表指针
*/
void DeleteSeqTable(SeqTable* pST)
{
free(pST->elem);
pST->elem = NULL;
pST->length = 0;
}
app:main.o
gcc -o app main.o
main.o:main.c SeqTable.h
gcc -c -w -O3 -std=c99 -fsigned-char main.c -o main.o
# 阅读实验源代码
**main.c文件**
在main函数中首先初始化了一个已经排序的线性表(注意,没有使用单元0),然后调用了两次折半查找函数BinarySearch查找指定的关键字,第一次查找关键字成功,第二次查找关键字失败,最后销毁了线性表。
在main函数的后面,分别定义了折半查找函数BinarySearch,以及线性表的初始化函数和销毁函数。其中,折半查找函数BinarySearch的函数体还不完整,留给读者完成。
**SeqTable.h文件**
定义了与线性表相关的数据结构并声明了相关的操作函数。
# 在演示模式下调试项目
**按照下面的步骤调试项目:**
1. 按F7生成项目。
2. 在演示模式下,按F5启动调试项目。程序会在观察点函数的开始位置中断。
3. 重复按F5,直到调试过程结束。
在调试的过程中,每执行“演示流程”窗口中的一行后,仔细观察“可视化数据”窗口内容所发生的变化,理解折半查找的执行过程。“可视化数据”窗口显示的数据信息(如下图所示),包括:
- 已经排序的线性表。
a) 序号从1开始计数,要查找的关键字用蓝色的字体显示。
b) 在调试过程中,可以看到 low、mid和high三个游标的移动。
c) 在查找的过程中,被命中的关键字所在的行的填充色为绿色。
- 根据线性表生成的判定树。
a) 每个节点包括了元素的序号和关键字,并用蓝色的字体显示出要查找的关键字。
b) 在查找的过程中,被命中的关键字所在的结点的填充色为绿色。
![折半查找1](./img/21_1.png)
![折半查找2](./img/21_2.png)
# 编写源代码并通过验证
**按照下面的步骤继续实验:**
1. 为BinarySearch函数编写源代码。注意,尽量使用已定义的局部变量。
2. 按F7生成项目。如果生成失败,根据“输出”窗口中的提示信息修改源代码中的语法错误。
3. 按Alt+F5启动验证。如果验证失败,可以使用“输出”窗口中的“比较”功能,或者在“非演示模式”下按F5启动调试后重复按F10单步调试读者编写的源代码,从而定位错误的位置,然后回到步骤1。
>
**`提示:`**在验证过程中,主要是检测BinarySearch函数的返回值是否正确,所以,为了顺利通过验证,请读者一定要注意让BinarySearch函数返回正确的值。
>
# 教师参考答案
访问教师参考答案[domain relative url](engintime/ds-lab/teachers-packet/Lab021.git)
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论