C++编写的三元组类
实现了严蔚敏版数据结构书上的三元组数据结构。
头文件Triplet.h的具体代码如下:
#include <cstdlib>
#include <iostream>
using namespace std;
namespace Trip{
//常量定义
#define OK 1
#define TRUE 1
#define ERROR 0
#define FALSE 0
typedef int Status;
typedef int ElemType;
typedef ElemType *Triplet;
//函数原型
Status InitTriple(Triplet &T,ElemType v1,ElemType v2,ElemType v3); //初始化
Status DestroyTriplet(Triplet &T); //销毁指针
Status Get(const Triplet T,int i,ElemType &e); //获取值,注意多个返回值的用法
Status Set(Triplet &T,int i,ElemType e); //设置值
Status IsAscending(const Triplet T); //是否升序,用const避免改变
Status IsDescending(const Triplet T); //是否降序
Status Max(const Triplet T,ElemType &e); //最大值
Status Min(const Triplet T,ElemType &e); //最小值
//函数定义
Status InitTriple(Triplet &T,ElemType v1,ElemType v2,ElemType v3)
{
T = (ElemType *)calloc(3,sizeof(ElemType));
if(!T) //考虑分配未成功的情况
exit(EXIT_FAILURE);
T[0] = v1;
T[1] = v2;
T[2] = v3;
return OK;
}
Status DestroyTriplet(Triplet &T)
{
free(T);T=NULL; //注意释放内存空间后,避免野指针
return OK;
}
Status Get(const Triplet T,int i,ElemType &e)
{
if(i<0||i>2)
cout << "i必须为0,1或者2" << endl;
e = T[i-1];
return OK;
}
Status Set(Triplet &T,int i,ElemType e)
{
if(i<0||i>2)
cout << "i必须为0,1或者2" << endl;
T[i-1] = e;
return OK;
}
Status IsAscending(const Triplet T)
{
return (T[0] <= T[1]) && (T[1] <= T[2]);
}
Status IsDescending(const Triplet T)
{
return (T[0] >= T[1]) && (T[1] >= T[2]);
}
Status Max(const 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(const 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;
}
}
#include <iostream>
using namespace std;
namespace Trip{
//常量定义
#define OK 1
#define TRUE 1
#define ERROR 0
#define FALSE 0
typedef int Status;
typedef int ElemType;
typedef ElemType *Triplet;
//函数原型
Status InitTriple(Triplet &T,ElemType v1,ElemType v2,ElemType v3); //初始化
Status DestroyTriplet(Triplet &T); //销毁指针
Status Get(const Triplet T,int i,ElemType &e); //获取值,注意多个返回值的用法
Status Set(Triplet &T,int i,ElemType e); //设置值
Status IsAscending(const Triplet T); //是否升序,用const避免改变
Status IsDescending(const Triplet T); //是否降序
Status Max(const Triplet T,ElemType &e); //最大值
Status Min(const Triplet T,ElemType &e); //最小值
//函数定义
Status InitTriple(Triplet &T,ElemType v1,ElemType v2,ElemType v3)
{
T = (ElemType *)calloc(3,sizeof(ElemType));
if(!T) //考虑分配未成功的情况
exit(EXIT_FAILURE);
T[0] = v1;
T[1] = v2;
T[2] = v3;
return OK;
}
Status DestroyTriplet(Triplet &T)
{
free(T);T=NULL; //注意释放内存空间后,避免野指针
return OK;
}
Status Get(const Triplet T,int i,ElemType &e)
{
if(i<0||i>2)
cout << "i必须为0,1或者2" << endl;
e = T[i-1];
return OK;
}
Status Set(Triplet &T,int i,ElemType e)
{
if(i<0||i>2)
cout << "i必须为0,1或者2" << endl;
T[i-1] = e;
return OK;
}
Status IsAscending(const Triplet T)
{
return (T[0] <= T[1]) && (T[1] <= T[2]);
}
Status IsDescending(const Triplet T)
{
return (T[0] >= T[1]) && (T[1] >= T[2]);
}
Status Max(const 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(const 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;
}
}
实现测试代码如下:
#include <stdio.h>
#include <tchar.h>
#include <cstdlib>
#include <iostream>
#include <sys/timeb.h>
#include <ctime>
#include <climits>
#include "triplet.h"
using namespace std;
using namespace Trip;
int _tmain(int argc, _TCHAR* argv[])
{
Triplet trip;
InitTriple(trip,50,45,38);
int maxVal = 0,minVal = 0,val = 0;
Max(trip,maxVal);
Min(trip,minVal);
for(int i=0;i<=2;i++)
{
cout << "T[" << i << "] = " << trip[i] << ";";
}
cout << "三元组最大值为:" << maxVal << endl;
cout << "三元组最小值为:" << minVal << endl;
if(IsAscending(trip))
{
cout << "三元组是升序排列" << endl;
}
if(IsDescending(trip))
{
cout << "三元组是降序排列" << endl;
}
cout << "T[2] = " << trip[2] << endl;
Set(trip,2,100);
cout <<"赋值后..." << endl;
Get(trip,2,val);
cout << "T[2] = " << val << endl;
system("pause");
return 0;
}
#include <tchar.h>
#include <cstdlib>
#include <iostream>
#include <sys/timeb.h>
#include <ctime>
#include <climits>
#include "triplet.h"
using namespace std;
using namespace Trip;
int _tmain(int argc, _TCHAR* argv[])
{
Triplet trip;
InitTriple(trip,50,45,38);
int maxVal = 0,minVal = 0,val = 0;
Max(trip,maxVal);
Min(trip,minVal);
for(int i=0;i<=2;i++)
{
cout << "T[" << i << "] = " << trip[i] << ";";
}
cout << "三元组最大值为:" << maxVal << endl;
cout << "三元组最小值为:" << minVal << endl;
if(IsAscending(trip))
{
cout << "三元组是升序排列" << endl;
}
if(IsDescending(trip))
{
cout << "三元组是降序排列" << endl;
}
cout << "T[2] = " << trip[2] << endl;
Set(trip,2,100);
cout <<"赋值后..." << endl;
Get(trip,2,val);
cout << "T[2] = " << val << endl;
system("pause");
return 0;
}