#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
typedef int status;
typedef struct{
int a[3];
}triplet;
status inittriplet(triplet*e,int v1,int v2,int v3)
{
e=(triplet*)malloc(sizeof(triplet));
if(!e) return ERROR;
e->a[0]=v1;
e->a[1]=v2;
e->a[2]=v3;
return OK;
}
status delete(triplet*a){
if (!a)
{
return ERROR;
}
free (a);
return OK;
}
status get(triplet*e,int b,int c){
if(!e&&1>b&&b>3) return ERROR;
c=e->a[b-1];
return OK;
}
status put(triplet*e,int b,int c){
if (!e&&b<1&&b>3) return ERROR;
e->a[b-1]=c;
return OK;
}
status isascending(triplet*e){
return(e->a[0]<e->a[1]&&e->a[1]<e->a[2]);
}
status max(triplet*e,int b){
b=(e->a[0]>e->a[1])?((e->a[0]>e->a[2])?e->a[0]:e->a[2]):((e->a[1]>e->a[2])?e->a[1]:e->a[2]);
return OK;
}
int main(void)
{
triplet*e;
int b,c,i;
inittriplet(e,1,2,4);
get(e,2,c);
printf("%d",c);
put(e,2,5);
i=0;
while(i<3)
{
printf("%d\t",e->a[i]);
i++;
}
printf("\n");
max(e,c);
printf("%d\n",c);
if(isascending(e))
for(int k=0;k<3;k++)
printf("%d\t",e->a[i]);
system("pause");
return 0;
}
修改如下,问题见注释,供参考:
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
typedef int status;
typedef struct{
int a[3];
}triplet;
status inittriplet(triplet *&e,int v1,int v2,int v3)//status inittriplet(triplet*e,int v1,int v2,int v3)
{
e=(triplet*)malloc(sizeof(triplet));
if(!e) return ERROR;
e->a[0]=v1;
e->a[1]=v2;
e->a[2]=v3;
return OK;
}
status del(triplet*a){ //status delete(triplet*a)
if (!a)
{
return ERROR;
}
free (a);
return OK;
}
status get(triplet*e,int b,int &c){//status get(triplet*e,int b,int c)
if(!e&&1>b&&b>3) return ERROR;
c=e->a[b-1];
return OK;
}
status put(triplet*e,int b,int c){
if (!e&&b<1&&b>3) return ERROR;
e->a[b-1]=c;
return OK;
}
status isascending(triplet*e){
return(e->a[0]<e->a[1]&&e->a[1]<e->a[2]);
}
status max(triplet*e,int &b){ //status max(triplet*e,int b)
b=(e->a[0]>e->a[1])?((e->a[0]>e->a[2])?e->a[0]:e->a[2]):((e->a[1]>e->a[2])?e->a[1]:e->a[2]);
return OK;
}
int main(void)
{
triplet*e;
int b,c,i;
inittriplet(e,1,2,4);
get(e,2,c);
printf("%d\n",c); //printf("%d",c);
put(e,2,5);
i=0;
while(i<3)
{
printf("%d\t",e->a[i]);
i++;
}
printf("\n");
max(e,c);
printf("%d\n",c);
if(isascending(e))
for(int k=0;k<3;k++)
printf("%d\t",e->a[k]);//printf("%d\t",e->a[i]);
system("pause");
return 0;
}
//输出:
//2
//1 5 4
//5
//请按任意键继续. . .