三元组操作
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include
#include
typedef int ElemType;
typedef int Status;
typedef ElemType *Triplet;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
Status InitTriplet (Triplet *T)
{
ElemType v1,v2,v3;
*T=(ElemType*)malloc(3*sizeof(ElemType)); if (*T==0) return OVERFLOW;
scanf("%d%d%d",&v1,&v2,&v3);
(*T)[0]=v1;(*T)[1]=v2;(*T)[2]=v3;
return OK;
}
Status DestroyTriplet(Triplet *t){
/*销毁三元组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 e;
int select, i,e1;
printf("please input three numbers:\n");
if (InitTriplet(&T)==OVERFLOW)
printf("Fail,exit!");
else
do
{
printf("\nplease choose the following operating,input the number:\n"); printf("1:To get the i number\n");
printf("2:To get the max number\n");
printf("3:To get the min number\n");
printf("4: see it if Ascending?\n");
printf("5: change number\n");
printf("6: DestroyTriplet(*T)");
printf("0:end\n");
scanf("%d",&select);
switch (select)
{
case 1:printf("\ni=");scanf("%d",&i);
if (Get(T,i,&e)==ERROR) printf("i is wrong(i is form 1 to 3)\n");
else printf("the i number is %d\n",e);break;
case 2:Max(T,&e);
printf("the max number is %d\n",e);break;
case 3:Min(T,&e);
printf("the min number is %d\n",e);break;
case 4:if(IsAscending(T)) printf("triplet is IsAescending");
else if(IsDscending(T)) printf("Triplet is IsDescending!");
else printf("Triplet is not sepuence!");break;
case 5:printf("which number do you want to
change?\n");scanf("%d",&i);printf("put number");scanf("%d",&e);T[i-
1]=e;if(put(*T,i,e));printf("succeed!");break;
case 6:DestroyTriplet(*T);break;