• 抽象数据类型的表示与实现


     /* f.c 用函数指针代替C++的引用参数 */
     #include<stdio.h>
     void fa(int a) /* 在函数中改变a,将不会带回主调函数(主调函数中的a仍是原值) */
     {
       a=5;
       printf("在函数fa中:a=%d\n",a);
     }
    
     void fb(int *a) /* a为指针类型,在函数中改变*a,改变后的值将带回主调函数 */
     {
       *a=5;
       printf("在函数fb中:*a=%d\n",*a);
     }
    
     void main()
     {
       int n=1;
       printf("在主程中,调用函数fa之前:n=%d\n",n);
       fa(n);
       printf("在主程中,调用函数fa之后,调用函数fb之前:n=%d\n",n);
       fb(&n); /* 实参为n的地址 */
       printf("在主程中,调用函数fb之后:n=%d\n",n);
     }
     /* c1.h (程序名) */
     #include<string.h>
     #include<ctype.h>
     #include<malloc.h> /* malloc()等 */
     #include<limits.h> /* INT_MAX等 */
     #include<stdio.h> /* EOF(=^Z或F6),NULL */
     #include<stdlib.h> /* atoi() */
     #include<io.h> /* eof() */
     #include<math.h> /* floor(),ceil(),abs() */
     #include<process.h> /* exit() */
     /* 函数结果状态代码 */
     #define TRUE 1
     #define FALSE 0
     #define OK 1
     #define ERROR 0
     #define INFEASIBLE -1
     /* #define OVERFLOW -2 因为在math.h中已定义OVERFLOW的值为3,故去掉此行 */
     typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */
     typedef int Boolean; /* Boolean是布尔类型,其值是TRUE或FALSE */
     /* c1-1.h 采用动态分配的顺序存储结构 */
     typedef ElemType *Triplet; /* 由InitTriplet分配三个元素存储空间 */
     /* Triplet类型是ElemType类型的指针,存放ElemType类型的地址 */
     /* bo1-1.c 抽象数据类型Triplet和ElemType(由c1-1.h定义)的基本操作(8个) */
     Status InitTriplet(Triplet *T,ElemType v1,ElemType v2,ElemType v3)
     { /* 操作结果:构造三元组T,依次置T的三个元素的初值为v1,v2和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)
     { /* 操作结果:三元组T被销毁 */
       free(*T);
       *T=NULL;
       return OK;
     }
    
     Status Get(Triplet T,int i, ElemType *e)
     { /* 初始条件:三元组T已存在,1≤i≤3。操作结果:用e返回T的第i元的值 */
       if(i<1||i>3)
         return ERROR;
       *e=T[i-1];
       return OK;
     }
    
     Status Put(Triplet T,int i,ElemType e)
     { /* 初始条件:三元组T已存在,1≤i≤3。操作结果:改变T的第i元的值为e */
       if(i<1||i>3)
         return ERROR;
       T[i-1]=e;
       return OK;
     }
    
     Status IsAscending(Triplet T)
     { /* 初始条件:三元组T已存在。操作结果:如果T的三个元素按升序排列,返回1,否则返回0 */
       return(T[0]<=T[1]&&T[1]<=T[2]);
     }
    
     Status IsDescending(Triplet T)
     { /* 初始条件:三元组T已存在。操作结果:如果T的三个元素按降序排列,返回1,否则返回0 */
       return(T[0]>=T[1]&&T[1]>=T[2]);
     }
    
     Status Max(Triplet T,ElemType *e)
     { /* 初始条件:三元组T已存在。操作结果:用e返回T的三个元素中的最大值 */
       *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)
     { /* 初始条件:三元组T已存在。操作结果:用e返回T的三个元素中的最小值 */
       *e=T[0]<=T[1]?T[0]<=T[2]?T[0]:T[2]:T[1]<=T[2]?T[1]:T[2];
       return OK;
     }
     /* algo1-1.c 计算1-1/x+1/x*x… */
     #include<stdio.h>
     #include<sys/timeb.h>
     void main()
     {
       struct 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/x;
         sum+=sum1;
       }
       ftime(&t2); /* 求得当前时间 */
       t=(t2.time-t1.time)*1000+(t2.millitm-t1.millitm); /* 计算时间差 */
       printf("sum=%lf 用时%ld毫秒\n",sum,t);
     }
     /* algo1-1.c 计算1-1/x+1/x*x… */
     #include<stdio.h>
     #include<sys/timeb.h>
     void main()
     {
       struct 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/x;
         sum+=sum1;
       }
       ftime(&t2); /* 求得当前时间 */
       t=(t2.time-t1.time)*1000+(t2.millitm-t1.millitm); /* 计算时间差 */
       printf("sum=%lf 用时%ld毫秒\n",sum,t);
     }
     /* algo1-2.cpp 计算1-1/x+1/x*x…的更快捷的算法 */
     #include<stdio.h>
     #include<sys/timeb.h>
     void main()
     {
       struct timeb t1,t2;
       long t=0;
       double x,sum1=1,sum=1;
       int i,n;
       printf("请输入x n: ");
       scanf("%lf%d",&x,&n);
       ftime(&t1); /* 求得当前时间 */
       for(i=1;i<=n;i++)
       {
         sum1=-sum1/x;
         sum+=sum1;
       }
       ftime(&t2); /* 求得当前时间 */
       t=(t2.time-t1.time)*1000+(t2.millitm-t1.millitm); /* 计算时间差 */
       printf("sum=%lf 用时%ld毫秒\n",sum,t);
     }
  • 相关阅读:
    【转】Java并发编程:阻塞队列
    【计算机二级Java语言】卷005
    【计算机二级Java语言】卷004
    【计算机二级Java语言】卷003
    【计算机二级Java语言】卷002
    【计算机二级Java语言】卷001
    【计算机二级C语言】卷020
    【计算机二级C语言】卷019
    【计算机二级C语言】卷017
    【计算机二级C语言】卷016
  • 原文地址:https://www.cnblogs.com/cpoint/p/3479497.html
Copyright © 2020-2023  润新知