C Primer Plus(第六版)第十六章 编程练习答案
Posted 水番正文
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C Primer Plus(第六版)第十六章 编程练习答案相关的知识,希望对你有一定的参考价值。
距离上次隔了十二天,好像是有点慢,期间还看了下C++pp,看到句话,每章内容不是很多,建议一周内完成一章,虽然之后要看琢石成器,C++也要顺带看一下。--11.16
CH16 Code answer 1:
我的头文件,后续会越写越多,感觉作者把这个放在第一条十分有意思。
#ifndef POZ_H_ //检查是否已经包含我的头文件
#define POZ_H_
#define JQ(X) ((X) % 2 ? 1 : 0) //奇偶 奇数返回1 偶数返回0
#define MAX(X, Y) ((X) > (Y) ? X : Y) //return MAX
#define MIN(X, Y) ((X) < (Y) ? X : Y) //return MIN
inline static void EatLine() //内联函数且是内部链接 编译器有可能优化
{
while (getchar() != '\\0')
continue;
}
inline static void PT_C(int * ar, int n) //接受一个int型数组 以字符形式打印传入的数组
{
int i;
for (i = 0; i < n; i++)
printf("%c", ar[i]);
}
#endif
CH16 Code answer 2:
#include <stdio.h>
#include "P0Z.h"
#define T_AVG(X, Y) ((1 / (X)) + (1 / (Y))) / 2
int main(void)
{
double x = 0;
double y = 0;
printf("Enter x and y(enter q to quit)\\n");
while (scanf("%lf %lf", &x, &y))
{
printf("Answer is %lf\\n", T_AVG(x, y));
}
printf("Bye!");
return 0;
}
CH16 Code answer 3:
#include <stdio.h>
#include <math.h>
#include "P0Z.h"
#define GET_ZBX(R, A) ((R) * cos(A * PI))
#define GET_ZBY(R, A) ((R) * sin(A * PI))
#define PI 3.14159265 / 180.0 //转弧度
typedef struct Po_Coordinate
{
double r;
double a;
}P_Cdnt;
typedef struct Ca_Coordinate
{
double x;
double y;
}C_Cdnt;
C_Cdnt Get_Cdnt(P_Cdnt *);
int main(void)
{
P_Cdnt input = { 0 };
C_Cdnt result = { 0 };
// printf("%lf\\n", cos(60.0 * PI) ); 返回弧度角的余弦值
printf("Enter angle and magnitude(enter q to quit)\\n");
while (scanf("%lf %lf", &input.a, &input.r))
{
result = Get_Cdnt(&input);
printf("x: %lf, y: %lf\\n", result.x, result.y);
}
printf("Bye!");
return 0;
}
C_Cdnt Get_Cdnt(P_Cdnt * p_zb)
{
C_Cdnt c_zb;
c_zb.x = GET_ZBX(p_zb->r, p_zb->a);
c_zb.y = GET_ZBY(p_zb->r, p_zb->a);
return c_zb;
}
CH16 Code answer 4:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include "P0Z.h"
void Clock_Time(double);
int main(void)
{
double count;
printf("Enter the delay time:(enter q to quit)\\n");
while (scanf("%lf", &count))
Clock_Time(count);
printf("Bye!");
return 0;
}
void Clock_Time(double time)
{
double x, y;
x = (double)clock(); //clock代表的是处理器的时间 也就是处理器处理到这行的时间?
_sleep(time);
y = (double)clock();
printf("The delay is %lf\\n", (y - x) / CLOCKS_PER_SEC);
}
CH16 Code answer 5:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "P0Z.h"
#define LEN 10
void Print_sd(int *, int, int);
int Find_df(int *, int);
int main(void)
{
int data[LEN] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int count;
srand(time(0));
printf("Enter a number, i will pick you randomly(enter q to quit)\\n");
while (scanf("%d", &count) && count <= 10)
{
// printf("%d", count);
Print_sd(data, LEN, count);
}
printf("Bye!");
return 0;
}
void Print_sd(int * ar, int n, int count)
{
// int index[count] = { 0 }; //定义形参变量就会报错?? 我怎么记得在DEV不会报错
int index[LEN] = { 0 };
int i = 0;
// printf("%d %d", n, count);
while (i < count)
{
index[i] = Find_df(index, n);
printf("%d ", index[i]);
i++;
}
printf("The result of random selection:");
for ( i = 0; i < count; i++)
printf("%d ", ar[index[i] - 1]); //下标 -1
printf("\\n");
}
int Find_df(int * index, int n)
{
int i;
int key;
key = rand() % 10 + 1; //由于0为初始化 所以每次找到0 都会导致找其他数 于是就定义到1 - 10 使用下标-1即可
// printf("%d", key);
for ( i = 0; i < n; i++)
if ( key == index[i] ) //如果找到相同的下标 就向下递归
{
key = Find_df(index, n);
break;
}
return key;
}
CH16 Code answer 6:
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include "P0Z.h"
#define FLEN 10
#define LLEN 10
#define YR 10
typedef struct Names
{
char first[FLEN];
char last[LLEN];
}names;
void Show_St(names * mfriend, int n);
int comp(const void * p1, const void * p2);
int main(void)
{
names mfriend[YR] =
{
{ "Z", "P" },
{ "NN", "PY" },
{ "ZZ", "C" },
{ "KH", "L" },
{ "JH", "Z" },
{ "XL", "Z" },
{ "WJ", "L" },
{ "XW", "H" },
{ "JF", "Milk"},
{ "X", "ZG"}
};
puts("Look my friend list:\\n");
Show_St(&mfriend, YR);
puts("After looking at the quick sort");
qsort(mfriend, YR, sizeof(names), comp);
Show_St(&mfriend, YR);
return 0;
}
void Show_St(names * mfriend, int n)
{
int i;
for ( i = 0; i < n; i++)
printf("First name: %-3s Last name: %-3s\\n", mfriend[i].first, mfriend[i].last);
puts("\\n");
}
int comp(const void * p1, const void * p2)
{
const names * ps1 = (const names *)p1;
const names * ps2 = (const names *)p2;
int res;
res = strcmp(ps1->last, ps2->last);
if (res != 0)
return;
else
strcmp(ps1->first, ps2->first);
}
CH16 Code answer 7:
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "P0Z.h"
void Show_Array(const double * ar, int n);
double * New_d_Array(int n, ...);
int main(void)
{
double * p1;
double * p2;
p1 = New_d_Array(5, 1.2, 2.3, 3.4, 4.5, 5.6);
p2 = New_d_Array(4, 100.0, 20.00, 8.08, -1890.0);
puts("Level1:");
Show_Array(p1, 5);
puts("Level2:");
Show_Array(p2, 4);
free(p1);
free(p2);
return 0;
}
double * New_d_Array(int n, ...)
{
va_list ap; // 1. 创建va_list类型
int i;
double * ar = (double *)malloc(n * sizeof(double));
va_start(ap, n); // 2. 初始化为一个参数列表
for ( i = 0; i < n; i++)
ar[i] = va_arg(ap, double); // 3. 用宏访问参数列表
va_end(ap); // 4. 用宏清理工作
return ar;
}
void Show_Array(const double * ar, int n)
{
int i;
for ( i = 0; i < n; i++)
printf("%-5.2lf\\n", ar[i]);
puts("\\n");
}
以上是关于C Primer Plus(第六版)第十六章 编程练习答案的主要内容,如果未能解决你的问题,请参考以下文章