#include <stdio.h>
#include <stdlib.h>
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;
case 0:printf("finish\n");break;
default:printf("input error\n");
}/*switch*/
}while (select!=0);
}/*main*/
/*实验一:抽象数据类型三元组基本操作的实现(2学时)
本次实验的主要目的在于帮助学生熟悉抽象数据类型的表示和实现方法。
(一)问题描述
设计一个实现三元组基本操作的程序。
(二)基本要求
1.实现三元组的构造、取值、修改、有序判断、最大和最小值求解和销毁。
每个功能应用一个独立的函数实现。
2.自己设计菜单界面,可用最简单的DOS菜单(黑屏上各种功能各占一行,前有序号,可选)
3.各模块进入前应实现刷屏(用clrscr()函数即可)
(三)测试数据
自行拟定测试数据。
尽量保证测试完全。
(四)实现提示
注意在C语言中的值调用方式及指针的使用。
注意:①报告上机前的部分应在实验前完成,上机时带上报告。
②代码在每次上机前请输入好,自备U盘,保证上机2小时调试成功。
*/。