在vc++上简单搭建环境(包括文件引用)

1,triplet_head.h 文件

 1 #define TRUE        1
 2 #define FALSE        0
 3 #define OK            1
 4 #define ERROR        0
 5 #define OVER_FLOW    -2
 6 
 7 typedef int Status;
 8 typedef int ElemType;
 9 typedef ElemType *Triplet;
10 
11 Status init_triplet(Triplet &t, ElemType v1, ElemType v2, ElemType v3);
12 //构造 Triplet t,并赋予3个元素的值为 v1,v2,v3
13 Status destroy_triplet(Triplet &t);
14 //销毁 Triplet t
15 Status get(Triplet t, int i, ElemType &e);
16 //用e返回t的第i个元素的值
17 Status put(Triplet &t, int i, ElemType e);
18 
19 Status isAscending(Triplet t);
20 
21 Status isDescending(Triplet t);
22 
23 Status max(Triplet t, ElemType &e);
24 
25 Status min(Triplet t, ElemType &e);
View Code

2,triplet_impl.cpp 文件(首先,需要include "stdafx.h"(stdafx.hinclude了stdio.h);其次,引入stdlib.h和string.h; 再次,引入triplet_head.h)

 1 #include "stdafx.h"
 2 #include "triplet_head.h"
 3 
 4 #include <stdlib.h>
 5 #include <string.h>
 6 
 7 Status init_triplet(Triplet &t, ElemType v1, ElemType v2, ElemType v3)
 8 {
 9     t = (ElemType *)malloc(3*sizeof(ElemType));
10     if(!t) return OVER_FLOW;
11     t[0]=v1; t[1]=v2; t[2]=v3;
12     return OK;
13 }
14 
15 Status destroy_triplet(Triplet &t)
16 {
17     free(t);
18     t=NULL;
19     return OK;
20 }
21 
22 Status get(Triplet t, int i, ElemType &e)
23 {
24     if(i<1||i>3) return ERROR;
25     
26     e=t[i-1];
27     return OK;
28 }
29 
30 Status put(Triplet &t, int i, ElemType e)
31 {
32     if(i<1||i>3) return ERROR;
33 
34     t[i-1]=e;
35     return OK;
36 }
37 
38 Status isAscending(Triplet t) 
39 {
40     return t[0]<=t[1] && t[1]<=t[2];
41 }
42 
43 Status max(Triplet t, ElemType &e)
44 {
45     e= (t[0]>=t[1])?(t[0]>=t[2]?t[0]:t[2]) : (t[1]>=t[2]?t[1]:t[2]);
46     return OK;
47 }
View Code

3,main函数(首先,还是需要include "stdafx.h",其次,引入实现triplet_impl.cpp)

 1 // Triplet.cpp : Defines the entry point for the console application.
 2 //
 3 
 4 #include "stdafx.h"
 5 #include "triplet_impl.cpp"
 6 
 7 
 8 int main()
 9 {
10     //初始化
11     Triplet t;
12     init_triplet(t, 1,3,2);
13     //获取第3个元素
14     ElemType e;
15     get(t,3,e);
16     printf("e=%d\n",e);
17     //设置第2个元素
18     put(t,2,4);
19     get(t,2,e);
20     printf("e=%d\n",e);
21     //判断是否 升序
22     printf("isAscending=%d\n", isAscending(t));
23     put(t,2,2);
24     printf("isAscending=%d\n", isAscending(t));
25     //取最大值
26     max(t,e);
27     printf("max=%d\n",e);
28     return 0;
29 }
View Code

 

posted @ 2017-12-27 18:04  wonkju  阅读(585)  评论(0编辑  收藏  举报