C Peimer 第16章编程习题

宏macro,这部分从前欠缺的也相当厉害,就知道个define与include,条件编译果然强大。最让我佩服的大概就是设计这些编程范式的人了,竟然想到用一个#define NDEBUG等可以截阻测试句。虽然我用的还不太熟,但真的让人思路大开……

这次的题少,仅七道,但我是分文件写的,也算熟练一下include等技术。


1.

#include <stdio.h>
#include "Ch16_1.h"
#include <time.h>
#include <stdlib.h>


int main(void) {

	//1
	//创建头文件——成功!

	//2
	/*
	double x1 = 3;
	double x2 = 2;
	double modi;
	modi = MODIFYFUN(x1, x2);
	printf("the final result:%lf", modi);
	*/

	//3
	/*
	struct Polar polarP;
	struct Rect rectP;
	polarP.len = 2;//由1:sqrt(3):2三角形测试成功!
	polarP.ang = 3.14/3;
	rectP = Polar2Rect(&polarP);
	printf("x = %lf,y = %lf", rectP.x, rectP.y);
	*/

	//4
	/*
	int x = 10;
	TimeDelay(10);//自定义延迟函数,声明于头文件,定义于独立.c
	printf("success! Function has delayed %ds", x);
	*/

	//5
	/*
	int arr[NAMELEN];
	int times = 9;
	int count = 0;
	for (int i = 0; i < NAMELEN; i++)
	{//初始化数组,组内元素为是否被选择
		arr[i] = 0;
	}
	ChooseMem(arr, NAMELEN, 9);
	printf("total number:%d, choose times:%d, final results:\n", NAMELEN, times);
	for (int i = 0; i < NAMELEN; i++)
	{
		if (arr[i]==1)
		{
			count++;
			printf("The %dth number is %d!\n", count, i + 1);
		}
	}
	*/
	
	//6
	/*
	struct name list[12];
	int size = 12;
	fillarray(list,size);//fill letters in a random order
	showarray(list, size);
	qsort(list, size, sizeof(struct name), mycomp);
	printf("\nin an order\n\n");
	showarray(list, size);
	*/

	//7
	double *p1;
	double *p2;
	p1 = new_d_array(6, 1.2, 2.3, 3.2, 4.2, 4.5, 5.6);
	p2 = new_d_array(4, 100.3, 5.5, 3.5, 3.1);
	show_array(p1, 6);
	printf("\nnew line\n\n");
	show_array(p2, 4);
	free(p1);
	free(p2);

	return 0;
}

2.头文件

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>


#define MODIFYFUN(x1,x2) 1/((1/(x1)) + (1/(x2)))
#define NAMELEN 20
struct Polar
{
	double len;
	double ang;
};

struct Rect
{
	double x;
	double y;
};

struct name
{
	char first[NAMELEN];
	char last[NAMELEN];
};

struct Rect Polar2Rect(const struct Polar *temp);

void TimeDelay(int);

//5
void ChooseMem(int *temp, int size, int times);

//6
void fillarray(struct name *ar, int n);
void showarray(struct name *ar, int n);
int mycomp(const void *p1, const void *p2);

//7
void show_array(const double ar[], int n);
double * new_d_array(int n, ...);

3.

#include <stdio.h>
#include "Ch16_1.h"


void ChooseMem(int *temp, int size, int times) {
	int choice;
	int count = 0;
	srand(time(NULL));
	while (count < times)
	{
		choice = rand() % size;
		if (*(temp + choice) != 1)
		{
			*(temp + choice) = 1;
			count++;
		}
	}
}

4.

#include "Ch16_1.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void fillarray(struct name *ar, int n) {
	char temp1[NAMELEN];
	char temp2[NAMELEN];
	srand(time(NULL));
	int i;
	for (int j = 0; j < n; j++)
	{
		for (i = 0; i < rand() % (NAMELEN - 5) + 3; i++)
		{
			temp1[i] = rand() % 26 + 'a';
		}
		temp1[i] = '\0';			
		strcpy((*(ar+j)).first, temp1);
		for (i = 0; i < rand() % (NAMELEN - 5) + 3; i++)
		{
			temp2[i] = rand() % 26 + 'a';
		}
		temp2[i] = '\0';			
		strcpy((*(ar + j)).last, temp2);
	}
}
void showarray(struct name *ar, int n) {
	for (int i = 0; i < n; i++)
	{
		printf("%s %s\n", ar[i].first, ar[i].last);
	}
}
int mycomp(const void *p1, const void *p2) {
	const struct name *a1 = (const struct name *)p1;
	const struct name *a2 = (const struct name *)p2;
	if (strcmp((*a1).first,(*a2).first) > 0)
	{
		return 1;
	}
	else if (strcmp((*a1).first, (*a2).first) < 0)
	{
		return -1;
	}
	else
	{
		if (strcmp((*a1).last, (*a2).last) > 0)
		{
			return 1;
		}
		else
		{
			return -1;
		}
	}
}

5.

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>


void show_array(const double ar[], int n) {
	for (int i = 0; i < n; i++)
	{
		printf("%lf\n", ar[i]);
	}
}
double * new_d_array(int n, ...) {

	double *p1;
	p1 = malloc(sizeof(double)*n);
	va_list temp;
	va_start(temp, n);
	for (int i = 0; i < n; i++)
	{
		*(p1 + i) = va_arg(temp, double);
	}
	return p1;
}

6.

#include "Ch16_1.h"
#include <math.h>
struct Rect Polar2Rect(const struct Polar *temp) {
	struct Rect rectP;
	rectP.x = (*temp).len*cos((*temp).ang);
	rectP.y = (*temp).len*sin((*temp).ang);
	return rectP;
}

7.

#include "Ch16_1.h"
#include <stdio.h>
#include <time.h>

void TimeDelay(int x) {
	double start;
	double divide = 0;
	start = clock();
	while (divide<x)
	{
		divide = (clock() - divide) / CLOCKS_PER_SEC;
	}
}





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值