1. 示例代码
triplet.h
// 抽象数据类型 triplet 和 ElemType 的基本操作头文件
#ifndef TRIPLET_H
#define TRIPLET_H
#include "errorRecord.h"
#define SIZE 3
// 定义抽象数据类型 ElemType 在本程序中为整型
typedef int ElemType;
// 由 InitTriplet 分配三个元素存储空间
typedef ElemType *Triplet;
typedef int (*Compare)(ElemType, ElemType);
Status InitTriplet(ElemType v1, ElemType v2, ElemType v3, Triplet *t);
Status DestroyTriplet(Triplet *t);
Status Get(Triplet t, int i, ElemType *e);
Status Put(int i, ElemType e, Triplet t);
Status IsAscending(Triplet t, Compare compare, Boolean *isAscend);
Status IsDescending(Triplet t, Compare compare, Boolean *isDescend);
Status Max(Triplet t, Compare compare, ElemType *maxVaule);
Status Min(Triplet t, Compare compare, ElemType *minValue);
#endif // !TRIPLET_H
triplet.c
// 抽象数据类型 Triplet 和 ElemType 的基本操作实现
#include "triplet.h"
#include <stdlib.h>
#include <stdio.h>
/*
前置条件:三元组 *t 已存在
操作结果:依次置 *t 的三个元素的初值为 v1, v2 和 v3
*/
Status InitTriplet(ElemType v1, ElemType v2, ElemType v3, Triplet *t)
{
if (!t) {
ERR_RECORD(ERR_NULL_PTR);
return ERR_NULL_PTR;
}
*t = (ElemType *)malloc(sizeof(ElemType) * SIZE);
if (!(*t)) {
ERR_RECORD(ERR_MEMORY_ALLOCATE);
return ERR_MEMORY_ALLOCATE;
}
(*t)[0] = v1;
(*t)[1] = v2;
(*t)[2] = v3;
return RET_OK;
}
/*
前置条件:三元组 *t 已存在
操作结果:三元组 *t 被销毁
*/
Status DestroyTriplet(Triplet *t)
{
if (!t) {
ERR_RECORD(ERR_NULL_PTR);
return ERR_NULL_PTR;
}
free(*t);
*t = NULL;
return RET_OK;
}
/*
前置条件:三元组 t 已存在,1 ≤ i ≤ 3
操作结果:用 *e 返回 t 的第 i 元的值
*/
Status Get(Triplet t, int i, ElemType *e)
{
if (!t || !e) {
ERR_RECORD(ERR_NULL_PTR, "t = %p, e = %p", t, e);
return ERR_NULL_PTR;
}
if ((i < 1) || (i > 3)) {
ERR_RECORD(ERR_NOT_EXIST, "i = %d", i);
return ERR_NOT_EXIST;
}
*e = t[i - 1];
return RET_OK;
}
/*
前置条件:三元组 t 已存在,1 ≤ i ≤ 3
操作结果:改变 t 的第 i 元的值为 e
*/
Status Put(int i, ElemType e, Triplet t)
{
if (!t) {
ERR_RECORD(ERR_NULL_PTR);
return ERR_NULL_PTR;
}
if ((i < 1) || (i > 3)) {
ERR_RECORD(ERR_NOT_EXIST, "i = %d", i);
return ERR_NOT_EXIST;
}
t[i - 1] = e;
return RET_OK;
}
/*
前置条件:三元组 t 已存在
操作结果:如果 t 的三个元素按升序排列,用 *isAscend 返回 TRUE,否则用 *isAscend 返回 FALSE
*/
Status IsAscending(Triplet t, Compare compare, Boolean *isAscend)
{
if (!t || !compare || !isAscend) {
ERR_RECORD(ERR_NULL_PTR, "t = %p, compare = %p, isAscend = %p", t, compare, isAscend);
return ERR_NULL_PTR;
}
*isAscend = ((compare(t[0], t[1]) <= 0) && (compare(t[1], t[2]) <= 0)) ? TRUE : FALSE;
return RET_OK;
}
/*
前置条件:三元组 t 已存在
操作结果:如果 t 的三个元素按降序排列,用 *isDescend 返回 TRUE,否则用 *isDescend 返回 FALSE
*/
Status IsDescending(Triplet t, Compare compare, Boolean *isDescend)
{
if (!t || !compare || !isDescend) {
ERR_RECORD(ERR_NULL_PTR, "t = %p, compare = %p, isDescend = %p", t, compare, isDescend);
return ERR_NULL_PTR;
}
*isDescend = ((compare(t[0], t[1]) >= 0) && (compare(t[1], t[2]) >= 0)) ? TRUE : FALSE;
return RET_OK;
}
/*
前置条件:三元组 t 已存在
操作结果:用 *maxVaule 返回 t 的三个元素中的最大值
*/
Status Max(Triplet t, Compare compare, ElemType *maxVaule)
{
if (!t || !compare || !maxVaule) {
ERR_RECORD(ERR_NULL_PTR, "t = %p, compare = %p, maxVaule = %p", t, compare, maxVaule);
return ERR_NULL_PTR;
}
*maxVaule = (compare(t[0], t[1]) >= 0) ? (compare(t[0], t[2]) >= 0 ? t[0] : t[2])
: (compare(t[1], t[2]) >= 0 ? t[1] : t[2]);
return RET_OK;
}
/*
前置条件:三元组 t 已存在
操作结果:用 *minValue 返回 t 的三个元素中的最小值
*/
Status Min(Triplet t, Compare compare, ElemType *minValue)
{
if (!t || !compare || !minValue) {
ERR_RECORD(ERR_NULL_PTR, "t = %p, compare = %p, minValue = %p", t, compare, minValue);
return ERR_NULL_PTR;
}
*minValue = (compare(t[0], t[1]) <= 0) ? (compare(t[0], t[2]) <= 0 ? t[0] : t[2])
: (compare(t[1], t[2]) <= 0 ? t[1] : t[2]);
return RET_OK;
}
main.c
// 入口程序头文件
#include <stdio.h>
#include "triplet.h"
static int Comp(ElemType e1, ElemType e2)
{
return e1 - e2;
}
int main(void)
{
Triplet t;
/* 初始化三元组 */
Status ret = InitTriplet(5, 7, 9, &t);
if (ret != RET_OK) {
ERR_RECORD(ret);
return ret;
}
printf("Successfully initialized triplet, t[0] = %d, t[1] = %d, t[2] = %d\n\n",
t[0], t[1], t[2]);
/* 获取三元组第 i 个值 */
ElemType e;
int i = 2;
ret = Get(t, i, &e);
if (ret != RET_OK) {
ERR_RECORD(ret);
return ret;
}
printf("Successfully obtained, the %dth value of t is:%d\n\n", i, e);
/* 改变三元组第 i 个值 */
e = 6;
ret = Put(i, e, t);
if (ret != RET_OK) {
ERR_RECORD(ret);
return ret;
}
printf("Successfully changed triplet, the %dth value of t is:%d\n\n", i, t[i - 1]);
/* 判断三元组是否升序 */
Boolean isAscend;
ret = IsAscending(t, Comp, &isAscend);
if (ret != RET_OK) {
ERR_RECORD(ret);
return ret;
}
printf("The triplet is %s\n\n", isAscend == TRUE ? "ascending order" :
"Non ascending order");
/* 判断三元组是否降序 */
Boolean isDescend;
ret = IsDescending(t, Comp, &isDescend);
if (ret != RET_OK) {
ERR_RECORD(ret);
return ret;
}
printf("The triplet is %s\n\n", isDescend == TRUE ? "descending order" :
"Non descending order");
/* 获取三元组中最大值 */
ret = Max(t, Comp, &e);
if (ret != RET_OK) {
ERR_RECORD(ret);
return ret;
}
printf("The maximum value in the triplet is:%d\n\n", e);
/* 获取三元组中最小值 */
ret = Min(t, Comp, &e);
if (ret != RET_OK) {
ERR_RECORD(ret);
return ret;
}
printf("The minimum value in the triplet is:%d\n\n", e);
/* 销毁三元组 */
ret = DestroyTriplet(&t);
if (ret != RET_OK) {
ERR_RECORD(ret);
return ret;
}
return 0;
}
2. 输出示例