三元组s1(a,b,c) 元素类型可自定义 elemtype
下面直接贴代码
triple.h 头文件
#ifndef _TRIP_H_
#define _TRIP_H_
#define ERROR -1
//可更改三元组的数据类型
typedef int elem_type;
//三元组结构体定义
typedef struct _TRIP
{
elem_type e1;
elem_type e2;
elem_type e3;
}TRIP;
bool init_trip(TRIP *p);
TRIP *init_trip_ex();
bool destory_trip(TRIP *p);
bool destory_trip_ex(TRIP *p);
bool put(TRIP *p, int pos ,elem_type *e);
bool get(TRIP *p, int pos,elem_type *e);
elem_type get_max(elem_type a,elem_type b);
elem_type get_min(elem_type a,elem_type b);
elem_type max(TRIP *p);
elem_type min(TRIP *p);
bool is_ascending(TRIP *p);
bool is_descending(TRIP *p);
bool sort(TRIP *p);
bool show(TRIP *p,void (*pfunc)(elem_type *,elem_type *,elem_type *));
void print_int(int *a,int *b,int *c);
bool add1(TRIP *p,TRIP *s);// p=p+s;
bool add2(TRIP *p,TRIP *s,TRIP *x);//p+s=x;
TRIP *add3(TRIP *p,TRIP *s);//p+s=x;
bool swap_trip(TRIP *p,TRIP *s);
#endif
#include"trip.h"
#include
#include
#include
#include
//初始化三元组
bool init_trip(TRIP *p)
{
if(p == NULL)
{
return false;
}
p->e1 = 0;
p->e2 = 0;
p->e3 = 0;
return true;
}
TRIP *init_trip_ex()
{
TRIP *p = (TRIP *)malloc(sizeof(TRIP)*1);
assert(p != NULL);
p->e1 = 0;
p->e2 = 0;
p->e3 = 0;
return p;
}
//销毁三元组
bool destory_trip(TRIP *p)
{
if(p == NULL)
{
return false;
}
p->e1 = 0;
p->e2 = 0;
p->e3 = 0;
return true;
}
//三元组空间在堆上开辟
bool destory_trip_ex(TRIP *p)
{
if(p == NULL)
{
return false;
}
free(p);
return true;
}
elem_type get_max(elem_type a,elem_type b)
{
return a>b?a:b;
}
elem_type get_min(elem_type a,elem_type b)
{
return ae1;
max = get_max(get_max(p->e1,p->e2),p->e3);
return max;
}
elem_type min(TRIP *p)
{
if ( p == NULL)
{
return false;
}
elem_type min = p->e1;
min = get_min(get_min(p->e1,p->e2),p->e3);
return min;
}
bool put(TRIP *p, int pos ,elem_type *e)
{
if (p == NULL)
{
return false;
}
switch (pos)
{
case 1:
p->e1 = *e;
break;
case 2:
p->e2 = *e;
break;
case 3:
p->e3 = *e;
break;
default:
return false;
}
return true;
}
bool get(TRIP *p, int pos,elem_type *e)
{
if (p == NULL)
{
return false;
}
switch(pos)
{
case 1:
*e = p->e1;
case 2:
*e = p->e2;
case 3:
*e = p->e3;
default:
return false;
}
}
bool add1(TRIP *p,TRIP *s)
{
if (p == NULL||s == NULL)
{
return false;
}
p->e1 +=s->e1;
p->e2 +=s->e2;
p->e3 +=s->e3;
return true;
}
bool add2(TRIP *p,TRIP *s,TRIP *x)
{
if (p == NULL||s == NULL||x == NULL)
{
return false;
}
x->e1 = p->e1 + s->e1;
x->e2 = p->e2 + s->e2;
x->e3 = p->e3 + s->e3;
return true;
}
TRIP *add3(TRIP *x,TRIP *s)
{
if (x == NULL || s == NULL)
{
exit(-1);
}
TRIP *p = (TRIP *)malloc(sizeof(TRIP)*1);
assert(p != NULL);
p->e1 = x->e1 + s->e1;
p->e2 = x->e2 + s->e2;
p->e3 = x->e3 + s->e3;
return p;
}
bool swap_trip(TRIP *p,TRIP *s)
{
if (p == NULL||s ==NULL)
{
return false;
}
TRIP *x = (TRIP *)malloc(sizeof(TRIP)*1);
assert(x != NULL);
p->e1 = s->e1;
s->e1 = x->e1;
x->e1 = p->e1;
p->e2 = s->e2;
s->e2 = x->e2;
x->e2 = p->e2;
p->e3 = s->e3;
s->e3 = x->e3;
x->e3 = p->e3;
return true;
}
//不知道三元组元素的数据类型,不能再此处直接printf
bool show(TRIP *p,void (*pfunc)(elem_type *,elem_type *,elem_type *))//函数指针
{
if (p == NULL)
{
return false;
}
pfunc(&p->e1,&p->e2,&p->e3);
return true;
}
void print_int(int *a,int *b,int *c)
{
printf("e1-%d,e2-%d,e3-%d",*a,*b,*c);
}
bool is_ascending(TRIP *p)
{
if(p == NULL)
{
return false;
}
if (p->e1e2 && p->e2e3)
{
return true;
}
return false;
}
bool is_descending(TRIP *p)
{
if(p == NULL)
{
return false;
}
if (p->e1>p->e2 && p->e2>p->e3)
{
return true;
}
return false;
}
//三元组元素排序
bool sort(TRIP *p)
{
elem_type tmp;
if(p == NULL)
{
return false;
}
if (p->e1 >p->e2)
{
tmp = p->e1;
p->e1 = p->e2;
p->e2 = tmp;
}
if (p->e2 >p->e3)
{
tmp = p->e2;
p->e2 = p->e3;
p->e3 = tmp;
}
if (p->e1 >p->e3)
{
tmp = p->e1;
p->e1 = p->e3;
p->e3 = tmp;
}
return true;
}
#include"trip.h"
#include
int main()
{
TRIP *t;
elem_type a = 20;
elem_type b = 10;
elem_type c = 30;
elem_type rs;
t = init_trip_ex();
put(t, 1,&a);
put(t, 2,&b);
put(t, 3,&c);
show(t,print_int);
printf("\n");
get(t,3,&rs);
printf("p->e3 = %d\n",rs);
sort(t);
show(t,print_int);
destory_trip_ex(t);
return 0;
}