数据结构三元组

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include
#include
#include

using namespace std;

#define TRRE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFRASIBLE -1
#define OVERFLOW -2

typedef int Status;
typedef double ElemType;
typedef ElemType *Triplet;
Status IniTriplet(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;
}
Status DestoryTriplet (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;
}
Status Show(Triplet &T){
int i;
cout<<"_______________"<if(!T){
cout<<"The Triplet is empty!"<return ERROR;
}
for(i=0;i<3;i++){
cout<<"|"<}
cout<<"_______________"<return OK;
}
float Average(Triplet &T){
float m;
m=(T[0]+T[1]+T[2])/3;
return m;
}
void main(){
Triplet T;
ElemType v1,v2,v3;
ElemType v_enter;
int order;
int place;
int k;
cout<<"Enter the first number:"<cin>>v1;
cout<<"Enter the second number:"<cin>>v2;
cout<<"Enter the third number:"<cin>>v3;
IniTriplet(T,v1,v2,v3);
Show(T);
while(T){
cout<<"1:put elem"<cout<<"2:get elem"<cout<<"3:average"<cout<<"4:the max elem"<cout<<"5:the min elem"<cout<<"6:destory and exit"<cin>>order;
switch(order){
case 1:
cout<<"输入一个数:"<cin>>v_enter;
cout<<"你想插入的位置:"<cin>>place;
Put(T,place,v_enter);
Show(T);
break;
case 2:
cout<<"选择一个位置(1-3)"<cin>>place;
Get(T,place,v_enter);
cout<<"_______________"<cout<<"|"<cout<<"_______________"<break;
case 3:
cout<<"_______________"<cout<<"|"<cout<<"_______________"<break;
case 4:
Max(T,v_enter);
cout<<"_______________"<cout<<"|"<cout<<"_______________"<break;
case 5:
Min(T,v_enter);
cout<<"_______________"<cout<<"|"<cout<<"_______________"<break;
case 6:
DestoryT

riplet(T);
}
}
system("pause");
}

相关文档
最新文档