本书所涉及的部分头文件如下:
#include<string.h> #include<ctype.h> #include<malloc.h> #include<limits.h> #include<stdio.h> #include<stdlib.h> #include<io.h> #include<math.h> #include<sys/timeb.h> #include<stdarg.h> /*函数结果状态代码*/ #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 typedef int Status; typedef int Boolean;
三元组抽象数据类型及其相关操作
typedef int ElemType; typedef ElemType *Triplet; Status InitTriplet(Triplet &T,ElemType v1,ElemType v2,ElemType v3) { T = (ElemType*)malloc(3*sizeof(ElemType)); if(!T) exit(OVERFLOW); T[0] = v1,T[1] = v2,T[2] = v3; return OK; } Status DestroyTriplet(Triplet &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,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 PrintE(ElemType e) { printf("%d\n",e); } void PrintT(Triplet T) { printf("%d,%d,%d\n",T[0],T[1],T[2]); } void main() { Triplet T; ElemType m; Status i; i = InitTriplet(T,5,7,9); printf("调用初始化函数后,i = %d(1:成功).T的3个值为",i); PrintT(T); i = Get(T,2,m); if(i == OK) { printf("T的第2个值为"); PrintE(m); } i = Put(T,6); if(i == OK) { printf("将T的第2个值改为6后,T的3个值为"); PrintT(T); } i = IsAscending(T); printf("调用测试升序的函数后,i = %d (0:否 1:是)\n",i); i = IsDescending(T); printf("调用测试降序的函数后,i); if((i = Max(T,m)) == OK) { printf("T中的最大值为"); PrintE(m); } if((i = Min(T,m)) == OK) { printf("T中的最小值为"); PrintE(m); } DestroyTriplet(T); printf("销毁T后,T = %u\n",T); }
算法和算法分析
void main() { timeb T1,T2; long t; double X,Sum=1,Sum1; int i,j,N; printf("请输入X N:"); scanf("%lf%d",&X,&N); ftime(&T1); for(i=1;i<=N;i++) { Sum1=1; for(j=1;j<=i;j++) Sum1=Sum1*(-1.0/X); Sum+=Sum1; } ftime(&T2); t=(T2.time-T1.time)*1000+(T2.millitm-T1.millitm); printf("Sum=%lf,用时%ld毫秒\n",Sum,t); }
void main() { timeb T1,Sum1=1,Sum=1; int i,&N); ftime(&T1); for(i=1;i<=N;i++) { Sum1*=-1.0/X; Sum+=Sum1; } ftime(&T2); t=(T2.time-T1.time)*1000+(T2.millitm-T1.millitm); printf("Sum = %lf,用时%ld毫秒\n",t); }原文链接:https://www.f2er.com/datastructure/383206.html