数据结构C-1-抽象数据结构与算法时间测试程序

#include <stdio.h>
#include <stdlib.h>
//输入三个整数并判断其排列顺序及最大值
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2

//预定义类型
typedef int Status;
typedef int ElemType;
typedef ElemType *Triplet;

//函数原型
Status InitTriplet(Triplet *pT,ElemType v1,ElemType v2,ElemType v3);
Status DestroyTriplet(Triplet *pT);
Status Get(Triplet T,int i,ElemType *pElem);
Status Put(Triplet T,int i,ElemType e);
Status IsAscending(Triplet T);
Status IsDescending(Triplet T);
Status Max(Triplet T,ElemType *pElem);
Status Min(Triplet T,ElemType *pElem);
int main()
{
    Triplet tr;
    ElemType e,e1,e2,e3;
    int a1,a2,a3;
    int result;
    printf("Please enter 3 integers:");
    scanf("%d%d%d",&a1,&a2,&a3);
    e1=a1;e2=a2;e3=a3;
    InitTriplet(&tr,e1,e2,e3);
    result=IsAscending(tr);
    if(result)
        printf("Ascending!\n");
    else
        printf("Not Ascending!\n");
    result=IsDescending(tr);
    if(result)
        printf("Descending!\n");
    else
        printf("Not Descending!\n");
    Min(tr,&e);
    printf("The smallest number is : %d\n",e);
    Max(tr,&e);
    printf("The biggest number is : %d\n",e);
    DestroyTriplet(&tr);
    return 0;
}
Status InitTriplet(Triplet *pT,ElemType v1,ElemType v2,ElemType v3){
    *pT=(ElemType *)malloc(3*sizeof(ElemType));
    if(!(*pT))
        exit(OVERFLOW);
    (*pT)[0]=v1;(*pT)[1]=v2;(*pT)[2]=v3;
    return OK;
}
Status DestroyTriplet(Triplet *pT){
    free(*pT);
    *pT=NULL;
    return OK;
}
Status Get(Triplet T,int i,ElemType *pElem){
    if(i<1||i>3)
        return ERROR;
    *pElem=T[i-1];
    return OK;
}
Status Put(Triplet T,int i,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 *pElem){
    *pElem=(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 *pElem){
    *pElem=(T[0]<=T[1])?( (T[0]<=T[2]) ?T[0]:T[2]): ((T[1]<=T[2]) ?T[1]:T[2]);
    return OK;
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//加法的时间测试
int main(){
    int i,j,n;
    int x,s;
    clock_t start,end,total;

    printf("Please enter the calculation scale:n= ");
    scanf("%d",&n);
    start=clock();

    for(i=1;i<=n;i++)
        for(j=1;j<=n;j++){
            ++x;s+=x;
        }
    end=clock();
    total=end-start;

    printf("The start time is %d,and the end time is %d.\n",(int)start,(int)end);
    printf("When n=%d ,the time is %d ms.\n",n,(int)total);

    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//加法的时间测试
int main(void){
    int i,j,k;
    int n,*pMa,*pMb,*pMc;
    time_t start,end,total;
    //输入规模;
    printf("Please enter the calculation scale:n= ");
    scanf("%d",&n);
    //矩阵建立
    pMa=(int *)malloc(n*n*sizeof(int));
    if(!pMa)
        return 1;
    pMb=(int *)malloc(n*n*sizeof(int));
    if(!pMb)
        return 1;
    pMc=(int *)malloc(n*n*sizeof(int));
    if(!pMc)
        return 1;
    //矩阵初始化
    k=0;
    for(i=0;i<n;i++)
        for(j=0;j<n;j++){
        pMa[i*n+j]=++k;
        pMb[i*n+j]=k*k;
    }
    //时间变量设置
    start=time(NULL);
    //矩阵相乘
    for(i=0;i<n;i++)
        for(j=0;j<n;j++){
            pMc[i*n+j]=0;
            for(k=0;k<n;k++)
                    pMc[i*n+j]=pMa[i*n+k]*pMb[k*n+j];
    }

    end=time(NULL);
    total=end-start;

    printf("The start time is %d,and the end time is %d.\n",(int)start,(int)end);
    printf("When n=%d ,the time is %d s.\n",n,(int)total);

    free(pMa);
    free(pMb);
    free(pMc);

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值