文档库 最新最全的文档下载
当前位置:文档库 › 抽象数据类型Triplet的表示和实现

抽象数据类型Triplet的表示和实现

“数据结构”上机实验报告

班级:计师二班学号:222013321081113 姓名:陈雅红实验日期:9.19.2014

一、实验名称:ADT的类C描述向C语言程序的转换实验

二、实验目的:熟练掌握抽象数据类型ADT的定义、表示和实现

三、实验内容:抽象数据类型三元组的表示及实现

四、实验过程:

实现教材P12给出的ADT Triplet基本操作:

头文件的编写:

Common.h

#define TRUE 1

#define FALSE 0

#define OK 1

#define ERROR 0

#define INFEASIBLE -1

#define OVERFLOW -2

typedef int Status;

Global.h (教师提供)

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define TRUE 1

#define FALSE 0

#define OK 1

#define ERROR 0

#define INFEASIBLE -1

typedef int Status;

typedef int Boolean;

Triplet.h(书上摘录)

#include"Common.h"

#define ElemType int

typedef ElemType * Triplet

Status InitTriplet(Triplet &T, ElemType v1, ElemType v2, ElemType v3); Status DestroyTriplet(Triplet &T);

Status Get(Triplet T, int i, ElemType &e);

Status Put(Triplet &T, int i, ElemType e);

Status IsAscending(Triplet T);

Status IsDescending(Triplet T);

Status Max(Triplet T, ElemType &e);

Status Min(Triplet T, ElemType &e);

源文件的编写:

InitTriplet.cpp

#include

#include

#include"Common.h"

#include"Triplet.h"

Status InitTriplet(Triplet &T, ElemType v1, ElemType v2, ElemType v3) {

T = (ElemType *)malloc(3 * sizeof(ElemType));

if(!T) exit(OVERFLOW);

T[0] = v1; T[1] = v2; T[2] = v3;

return OK;

} //InitTriplet

Status DestroyTriplet(Triplet &T)

{

free(T);

T = NULL;

return OK;

} //DestroyTriplet

Status Get(Triplet T, int i, ElemType &e){

if(i<1 || i>3)

return ERROR;

e = T[i-1];

return OK;

} //Get

Status Put(Triplet &T, int i, ElemType e) {

if(i<1 || i>3)

return ERROR;

T[i-1] = e;

return OK;

} // Put

Status IsAscending(Triplet T) {

return (T[0] >= T[1]) && (T[1] >= T[2]);

} //IsAscending

Status IsDescending(Triplet T) {

return (T[0] <= T[1]) && (T[1] <= T[2]);

} //IsDescending

Status Max(Triplet T, ElemType &e) {

e = (T[0] >= T[1]) ? ((T[0] >= T[2]) ? T[0]:T[2]) : ((T[1] >= T[2]) ? T[1]:T[2]);

return OK;

}

Status Min(Triplet T, ElemType &e) {

e = (T[0] <= T[1]) ? ((T[0] <= T[2]) ? T[0]:T[2]) : ((T[1] <= T[2]) ? T[1]:T[2]);

return OK;

}

Main.h(输入数据)

#include

#include

#include"Triplet.h"

int main()

{

Triplet triplet;

ElemType e;

int i;

InitTriplet(triplet, 4, 7, 5);

for(i=0;i<3;i++)

{

Get(triplet, i+1, e);

printf("%d %d\n", triplet[i],e);

}

e = 13;

Put(triplet, 2, e);

for(i=0;i<3;i++)

printf("%d ", triplet[i]);

printf("\n");

if(IsAscending(triplet))

printf("升序\n");

else {

if(IsDescending(triplet))

printf("降序\n");

else

printf("乱序\n");

}

Max(triplet, e);

printf("最大值:% d\n", e);

Min(triplet, e);

printf("最小值:%d\n", e);

system("pause");

return 0;

}

(很多注释内容省略了,在编写程序文件里有写上)

调试运行结果为:

4 4

7 7

5 5

4 13 5

乱序

最大值:13

最小值:4

五、实验心得:

在编写头文件的时候,要注意把其他头文件相互联系起来。如在Trilet.h中要写出#includ e “Common.h”,否则就容易出错,同理,在编写原文件的时候,液压注意这个问题,开始自己就是照本宣科,导致在调试的时候出现很多错却不知道为什么,检查编写也没有问题,结果自己这些#includ e #includ e #includ e "Common.h" #includ e "Tripl et.h"都没有写。

主函数在编写过程中要注意细节,自己也并没有独立完成,总是想不到各种细节,老是出错,无法一次完善,只有大概思路,在同学的帮助和讨论下完成的,以后在这些方面都需要勤加练习。

抽象数据类型三元组的表示及实现还是很不熟悉,很多地方需要加强练习。

相关文档
相关文档 最新文档