⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 project1.c

📁 自己上课时老师要求的PROJECT
💻 C
字号:
#include <stdio.h>/*head file*/
#include <time.h>

clock_t start, stop;
double TotalTime, duration, ticks;

void menu()/*the menu will be show at the beginning of the operation*/
{
printf("\n\n\n\nWelcome to Use The Program for Performance Measure\n\n");
printf("*****************************************************\n");
printf("0.Exit\n");/*four opitions you can choose*/
printf("1.Algorithm 1\n");
printf("2.Algorithm 2(iterative version)\n");
printf("3.Algorithm 2(recursive version)\n\n");
printf("Notice:If N and K you input is floating point number,\n");
printf("the program will conside it only the integral part!\n\n");
printf("*****************************************************\n\n");
}

double Algo1(double X, long int N)/*The first Algorithm*/
{
	long int i;
	double Mul;

	if(N==0) /*N=0 are the simple situation*/
		return 1;
	 Mul=X;
	 for(i=1;i<=N-1;i++)/*using N-1 multiplications*/
	   {
	   Mul=Mul*X;
	   }
	   return Mul;
}

double Algo2_Iter(double X, long int N)/*using iterative method*/
{
	double X1=1.0, X2;

	X2=X;
	if(N==0)/*N=0 and N=1 are the simple situation*/
		return 1.0;
	if(N==1)
		return X;
	while(N>1)
	{
	    if(N%2==0)/*judge the number whether even*/
	    {
		X2=X2*X2;/*X2 will be changed*/
		N=N/2;
		}
	    else
        {
        X1=X1*X2;/*if the number is odd, X1 will be changed*/
        N=N-1;
		}
	}
		 return X1=X1*X2;/*the result is composed by the results in “if” and “else*/
}

double Algo2_Recur(double X, long int N)/*the recursive algorithm*/
{
	if(N==0) /*N=0 and N=1 are the simple situation*/
	    return 1;
	if(N==1)
	    return X;
	if(N%2==0)/*judge the number whether even*/
		return Algo2_Recur(X*X,N/2);
	else
	    return Algo2_Recur(X*X,N/2)*X;
}

void Measure(double X, long int N, int num)/*the function to measure the time of the three algorithms, number 1 to 3 stand for the three algorithm*/
{
	long int K, j;

	while(1)/*this circulation will ensure K a positive number*/
	{
	printf("Please input the number of test:");
	scanf("%ld",&K);
		if(K<=0)
		printf("Please input a positive integer!\n");/*the number K must be a positive integer*/
		else
		break;
	}
	start=clock();/* records the ticks at the beginning of the function call */
	for(j=1;j<=K;j++)/*run the function for K times*/
	switch(num)/*run different algorithms according to different number*/
	{
		 case 1:
		Algo1(X, N);break;
		 case 2:
		Algo2_Iter(X, N);break;
		 case 3:
		Algo2_Recur(X, N);break;
		 default:
		 break;
	}
	stop=clock();/* records the ticks at the end of the function call */
	ticks=(double)(stop-start);
	TotalTime=((double)(stop-start))/CLK_TCK;/* CLK_TCK is a built-in constant = ticks per second */
	duration=TotalTime/K;/*the run-time for once*/
}

main()
{
	double X;
	long int N;
	char c;/*we use c to record 0 to 3 in case that the tester input any letters*/
	int num;/*num to record 0 to 3 by change the letter c to integer*/

	while(1)/*the circulation will ensure that tester could repeat his work continuously*/
	{
	menu();/*show the menu*/
	while(1)/*this circulation will ensure the choice is number 0 to 3*/
	{
	printf("The number you choose is:");/*make a choice*/
	scanf("%c",&c);
	num=c-'0';
	if(num!=1&&num!=2&&num!=3&&num!=0) /*in case input mistake*/
printf("Please input 0 to 3!\n");	
else
		break;
	}
	if(num==0)
		break;
	printf("Please input X:");
	scanf("%lf",&X);
	while(1) /*this circulation to make sure N a positive integer*/

	{
	printf("Please input N:");
	scanf("%ld",&N);
		if(N<0)
		printf("Please input a positive integer!\n");		
else
		break;
	}
	Measure(X, N, num);/*run the measure function*/
	printf("The ticks is: %lf\n", ticks);/*output  the result*/
	printf("The totaltime is: %lf s\n", TotalTime);
	printf("The duration is: %lf s\n", duration);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -