#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef ElemType;
typedef int status;
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);
/*基本操作(8个)*/
status InitTriplet(Triplet &T,ElemType v1,ElemType v2,ElemType v3)
{
if(!(T=(ElemType *)malloc(3*sizeof(ElemType))))
exit(OVERFLOW);
T[0]=v1;T[1]=v2;T[2]=v3;
return OK;
}
status DestroyTriplet(Triplet &T)
{
free(T);
T=NULL;
return OK;
}
status Get(Triplet T,int i, ElemType &e)
{
if(i<1||i>3)
return ERROR;
e=T[i-1];
return OK;
}
status Put(Triplet &T,int i,ElemType e)
{
if(i<1||i>3)
return ERROR;
T[i-1]=e;
return OK;
}
status IsAscending(Triplet T)
{
return(T[0]<=T[1]&&T[1]<=T[2]);
}
status IsDescending(Triplet T)
{
return(T[0]>=T[1]&&T[1]>=T[2]);
}
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;
}
void main()
{
Triplet T;
ElemType m;
status i;
i=InitTriplet(T,5,7,9);
printf("调用初始化函数后,i=%d/n",i);
printf("T的三个值为:%d %d %d/n",T[0],T[1],T[2]);
i=Get(T,2,m);
if(i==OK)
printf("T的第2个值为:%d/n",m);
i=Put(T,2,6);
if(i==OK)
printf("将T的第2个值改为6后,T的三个值为:%d %d %d/n",T[0],T[1],T[2]);
i=IsAscending(T); // 此类函数实参与ElemType的类型无关,当ElemType的类型变化时,实参不需改变
printf("调用测试升序的函数后,i=%d(0:否 1:是)/n",i);
i=IsDescending(T);
printf("调用测试降序的函数后,i=%d(0:否 1:是)/n",i);
if((i=Max(T,m))==OK) // 先赋值再比较
printf("T中的最大值为:%d/n",m);
if((i=Min(T,m))==OK)
printf("T中的最小值为:%d/n",m);
DestroyTriplet(T); // 函数可以不带回返回值
printf("销毁T后,T=%d/n",NULL);
}