抽象数据类型三元组的定义
#include <stdio.h>
#include <stdlib.h>
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<自定义数据类型<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
typedef int Status;
typedef int ElemType;
typedef ElemType *Triplet;
#define OVERFLOW -2
#define OK 1
#define ERROR 0
//---------------------------------------------构造三元组T-----------------------------------------------
Status InitTriplet(Triplet &T,ElemType v1,ElemType v2,ElemType v3)
{//构造三元组T,依此置T的三个元素的初值为v1,v2,v3
T=(ElemType*)malloc(3*sizeof(ElemType));//分配三个元素的存储空间
if(!T) exit(OVERFLOW);//分配存储空间失败
T[0] =v1; T[1]=v2; T[2]=v3;
return OK;
}
//--------------------------------------------销毁三元组T------------------------------------------------
Status DestroyTriplet(Triplet &T)
{
free(T);
T=NULL;
return OK;
}
//---------------------------------------用e返回T的第i元的值----------------------------------------------
Status Get(Triplet T,int i,ElemType e)
{
if(i<1||i>3) return ERROR;
e=T[i-1];
return e;
}
//------------------------------------置T的第i元的值为e--------------------------------------------------
Status Put(Triplet T,int i,ElemType e)
{
if(i<1||i>3) return ERROR;
T[i-1]=e;
return OK;
}
//----------------------------------升序排列返回1,否则返回0----------------------------------------------------
Status IsAscending(Triplet T)
{
return (T[0]<=T[1])&&(T[1]<=T[2]);
}
//---------------------------------降序排列返回1,否则返回0-----------------------------------------------------
Status IsDescending(Triplet T)
{
return (T[0]>=T[1])&&(T[1]>=T[2]);
}
//---------------------------------------用e返回最大值--------------------------------------------------------
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 e;
}
//----------------------------------------用e返回最小值-------------------------------------------------
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 e;
}
//-----------------------------------------显示三元组---------------------------------------------------
Status Print(Triplet T)
{
printf("%d %d %d",T[0],T[1],T[2]);
return OK;
}
//------------------------------------------主函数---------------------------------------------------------
main()
{
Triplet T;
ElemType e,v1,v2,v3;
int i,k,m;
printf("请输入v1,v2,v3的值(用空格隔开):");
scanf("%d %d %d",&v1,&v2,&v3);
InitTriplet(T,v1,v2,v3);
printf("功能选择:\n");
printf("1.用e返回T的第i元的值;\n");
printf("2.置T的第i元的值为e;\n");
printf("3.是否为升序排列;\n");
printf("4.是否为降序排列;\n");
printf("5.显示最大值;\n");
printf("6.显示最小值;\n");
printf("7.显示三元组;\n");
printf("8.销毁三元组;\n");
printf("9.结束\n");
for(int num=1;;num++)
{
printf("\n---------------------\n");
printf("请输入所选功能:");
scanf("%d",&k);
if(k==1)
{
printf("请输入i值:");
scanf("%d",&i);
m=Get(T,i,e);
printf("%d",m);
}
if(k==2)
{
printf("请输入i,e值(用空格隔开)");
scanf("%d %d",&i,&e);
Put(T,i,e);
}
if(k==3)
{
m=IsAscending(T);
if(m==1)
printf("是");
else printf("否");
}
if(k==4)
{
m=IsDescending(T);
if(m==1)
printf("是");
else printf("否");
}
if(k==5)
{
m=Max(T,e);
printf("%d",m);
}
if(k==6)
{
m=Min(T,e);
printf("%d",m);
}
if(k==7)
Print(T);
if(k==8)
DestroyTriplet(T);
if(k==9)
break;
}
}