16.17 编程练习

1.开发一个包含您需要使用的预处理定义的文件

2.两个数的调和平均数可以用如下方法得到:首先对两数的倒数求平均值,最后再求倒数。使用#define 命令定一个宏“函数”执行这个运算。编写一个简单的程序测试该宏。

#include <stdio.h>

#define HAVERAGE(X,Y) (1/((1/(double)(X)+1/(double)(Y))/2))

double haverage(double X,double Y);
int main(void)
{
	printf("%.2f\n",HAVERAGE(40,50));
	printf("%f",haverage(40,50));

	return 0;
}

double haverage(double X,double Y)
{
	return (1/((1/(X)+1/(Y))/2));
}

3.极坐标用向量的长度和向量相对于x轴的逆时针转角来描述该向量。直角坐标用向量x和y坐标来描述该向量。编写程序,它读取向量的长度和角度,然后现实x和y坐标。相关等式如下。

x=r cos A 

y=r sin A

要完成这个转换,需要使用一个函数,该函数接受一个包含极坐标的结构作为参数,返回一个包含指教坐标的结构(也可以使用指向结构的指针)

#include <stdio.h>
#include <math.h>

typedef struct{
	double x;
	double y;
}COOR;

typedef struct{
	double length;
	double angle; 
}VECTOR;

COOR convert(VECTOR a);

int main(void)
{
	VECTOR a={
		8,
		30,
	};
	
	COOR b;
	b=convert(a);
	printf("x=%.2f,y=%.2f",b.x,b.y);
}

COOR convert(VECTOR a)
{
	COOR c;
	c.x=a.length*(cos(a.angle));
	c.y=a.length*(sin(a.angle));
	return c;
}

4.ANSI库这样描述clock()函数:

#include <time.h>

clock_t clock(void);

clock_t是在time.h中定义的类型。clock()函数返回处理器时间,其单位依赖于实现(如果无法得到或者无法表示处理器时钟,函数返回-1)。而同样在time.h中定义的CLOCKS_PER_SEC是每秒的处理器时间单位个数。因此,求出两次调用函数clock()的返回值的差,在用CLOCKS_PER_SEC去除这个差值,结果就似乎以秒为单位的两次调用之间的时间间隔。在做除法之前,将值的类型指派为double 类型,可以将时间精确到小数点之后。编写一个函数,接受一个时间延迟数作为参数,然后运行一个循环,直到这段时间过完。编写一个简单的程序测试该函数。

#include <stdio.h>
#include <time.h>
#include <math.h>

void FunTest(double SetTime);
int main(void)
{
	double SetTime;
	printf("Please enter a number for duration (0 for quit):\n");
	while(scanf("%lf",&SetTime)==1 && SetTime)
	{
		FunTest(SetTime);
		printf("Next duration!\n");		
	}
}

void FunTest(double SetTime)
{
	double Star;
	double End;
	
	Star=(double)clock()/(double)CLOCKS_PER_SEC;	//开始时间
	printf("Star=%f\n",Star); 
	while(1)
	{
		End=(double)clock()/(double)CLOCKS_PER_SEC;
		if(End-Star>=SetTime)
		{
			printf("Stat=%f,End=%f,SetTime=%f,End-Star=%f\n",Star,End,SetTime,End-Star);
			break;
		}
	}	
}
5.编写一个函数。该函数接受下列参数:一个int 数组的名称,数组大小和一个代表选取次数的值,然后函数从数组中随机选择指定数量的元素并打印他们。每个元素最多选择一次。另外,如果您的实现支持time()或者类似的其他函数,可以在srand()中使用这个函数的输出来初始化随机数生成器。编写一个简单的程序测试该函数。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define SIZE 100

void RondomNumGeneration(int arr[],int size,int n);
void InitializeArray(int arr[],int size);
void ShowArray(int arr[],int size);
void CheckRepeat(int arr[],int size);
int main(void)
{
	int n;
	int RandArray[SIZE];
	
	InitializeArray(RandArray,SIZE);
	printf("Source Array:\n");
	ShowArray(RandArray,SIZE);
	printf("Please enter how many random number do you want to generate from array.\n");
	printf("0 to quit:");
	srand(time(NULL));
	while(scanf("%d",&n)==1 && n!=0)
	{
		RondomNumGeneration(RandArray,SIZE,n);
		printf("Enter another number.\n");
	}
	printf("Done.");
	
	return 0;
}

void InitializeArray(int arr[],int size)
{
	int i;
	for(i=0;i<size;i++)
		arr[i]=i*2;
}
void RondomNumGeneration(int arr[],int size,int n)
{
	int count;
	int index;
	int temp;
	int MaxIndex=size-1;
	int result[n];
	if(n>size)
		printf("error!\n");
	else
	{
		for(count=0;count<n;count++)
		{
			/*generate a random number in limited scope*/
			index=rand()%(MaxIndex-count+1)+count;
			result[count]=arr[index];

			/*change value*/
			temp=arr[index];
			arr[index]=arr[count];
			arr[count]=temp;	
		}
		CheckRepeat(result,n);
		printf("Random Array:\n");
		ShowArray(result,n);
	}
}

void ShowArray(int arr[],int size)
{
	int i;
	for(i=0;i<size;i++)
	{
		printf("%4d",arr[i]);
		if((i+1)%5==0)
			putchar('\n');
	}
	printf("\n");
}

void CheckRepeat(int arr[],int size)
{
	int i,j;
	for(i=0;i<size-1;i++)
	{
		for(j=i+1;j<size;j++)
		{
			if(arr[i]==arr[j])
			{
				printf("The repeated number (index %d and index %d).\n",i,j);
			}
		}
	}
}
6.修改程序清单16.15,使其使用由struct names元素组成的数组,而不是使用double数组。使用较少元素并显示的初始化数组为由合适名字组成的数组。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define LEN 10
#define SIZE 20

typedef struct names{
	char first[LEN];
	char last[LEN];
}NAME;

void CreateArrayName(NAME list[],int size);				//随机产生姓名 
void ShowStructure(const NAME list[],int size);			//显示结构中的姓和名成员 
int mycomp(const void *p1,const void *p2);			//按姓排序,姓相同则按名字排序 

int main(void)
{
	NAME list[SIZE];
	srand(time(NULL));
	CreateArrayName(list,SIZE);
	printf("Original order:\n");
	ShowStructure(list,SIZE);	
	putchar('\n');
	putchar('\n');
	qsort(list,SIZE,sizeof(NAME),mycomp);
	printf("Sorted order:\n");
	ShowStructure(list,SIZE);		

} 
void CreateArrayName(NAME list[],int size)
{
	int LenghtOfName;
	int ch;
	int StructureIndex;
	for(StructureIndex=0;StructureIndex<size;StructureIndex++)
	{
		/*随机产生名字长度,最大为LEN-1,最小为5*/
		LenghtOfName=rand()%((LEN-1)-5+1)+5;
		int i;
		for(i=0;i<LenghtOfName;i++)
		{
			/*随机产生字符填充结构中的first成员*/
			list[StructureIndex].first[i]=rand()%('z'-'a'+1)+'a';
		}
		/*首字母改为大写*/
		list[StructureIndex].first[0]-=32;
		/*末尾补充\0标识符*/
		list[StructureIndex].first[i]='\0';
		
		
		
		/*随机产生姓的长度*/
		LenghtOfName=rand()%((LEN-1)-5+1)+5;
		for(i=0;i<LenghtOfName;i++)
		{
			/*随机产生字符填充结构中的last成员*/
			list[StructureIndex].last[i]=rand()%('z'-'a'+1)+'a';
		}
		/*首字母改为大写*/
		list[StructureIndex].last[0]-=32;
		/*末尾补充\0标识符*/
		list[StructureIndex].last[i]='\0';
		
	} 
}

void ShowStructure(const NAME list[],int size)
{
	int StructureIndex;
	for(StructureIndex=0;StructureIndex<size;StructureIndex++)
	{
		printf("%s %s\n",list[StructureIndex].first,list[StructureIndex].last);
	}
}

int mycomp(const void *p1,const void *p2)
{
	int rt;
	NAME *a1=(NAME *)p1;
	NAME *a2=(NAME *)p2;
	
	rt=strcmp(a1->first,a2->first);
	if(rt==0)
		rt=strcmp(a1->last,a2->last);
	return rt;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值