cprimerplus(第六版)第十六章编程练习答案(代码片段)

水番正文 水番正文     2023-02-03     143

关键词:

距离上次隔了十二天,好像是有点慢,期间还看了下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");

cprimerplus(第六版)第十四章编程练习答案(代码片段)

这次打了真的好多天,不过坚持下来了,现在知道开发并不容易,不过值得学习。--10.16CH14 Codeanswer1:#include<stdio.h>#include<string.h>intDays(char*);structmonth charname[10]; charabbrve[4]; intdays; intmon 查看详情

cprimerplus(第六版)第十五章编程练习答案(代码片段)

这次也是隔了好久,不过这本书一定要在十一月内搞完。--10.16CH15 Codeanswer1:#include<stdio.h>#include<string.h>intBin2Int(char*);intPow(int,int);intmain(void) char*pbin="01001001"; printf("T 查看详情

cprimerplus(第六版)第十一章编程练习答案(代码片段)

前言:这次感觉融起来了,各章的知识都有用到,不过这次时间隔的是够久的。仅供参考,新手勿喷。CH11 Codeanswer1:#include<stdio.h>#defineSIZE100voids_gots(char*);intmain(void) charstr[SIZE]; printf("Enteryourstr 查看详情

cprimerplus(第六版)第十二章编程练习答案(代码片段)

如果说之前是在编程的基础上扩展,那么这次就是从线到面的第一步了!有问题欢迎提问,有BUG欢迎提出!本人用的编译器的DevC++多文件编译要创建项目附一篇别人的博客C语言(一)Dev-C++安装... 查看详情

cprimerplus(第六版)第十三章编程练习答案(代码片段)

这章打了两天,也不知道是算久还是不久,当然不是一天写到底,这次感觉打开了程序与其他文件的路口,本来对fopen一知半解,还有关于文件该如何操作,但这章让我了解挺多!--9.28CH13 Codeanswer1:#incl... 查看详情

cprimerplus(第六版)第十七章编程练习答案(代码片段)

结束了,这本树,现在就差道复习题的答案确认了,最后一题沉浸式解题,仿佛在做逆向题一样,好了,这就是我的这本书看完了每一页做完了每一题的最后一章。--11.26CH17 Codeanswer1:FilmA/*films2.c--usingalinked... 查看详情

cprimerplus第六章编程练习2

使用嵌套循环产生下列图案:$$$$$$$$$$$$$$$#include<stdio.h>intmain(void){inti,j;for(i=0;i<5;i++){for(j=0;j<=i;j++){printf("$");}printf(" ");}return0;}  查看详情

cprimerplus(第六版)第十章编程练习答案(代码片段)

前言:这次真的隔了好久才更,今日翻了几篇DASCTF文章,发现个个都是六边形战士,自己要得更努力,淦!仅供参考,新手勿喷。CH09 Codeanswer1:/*rain.c--findsyearlytotals,yearlyaverage,andmonthlyaverageforseveralyear 查看详情

cprimerplus(第六版)第二章编程练习答案(代码片段)

前言:由于不是太复杂,第二章的程序我就全部集成在一个程序了,仅供参考,新手勿喷。CH02Codeanswer:#include<stdio.h>voidjolly(void) printf("Forhe'sajollygoodfellow\\n"); printf("Forhe'sajol 查看详情

cprimerplus(第六版)第五章编程练习答案(代码片段)

 前言:由于不是太复杂,第五章的程序我就全部集成在一个程序了,仅供参考,新手勿喷。(梅开三度CH05 Codeanswer:#include<stdio.h>#defineSIXTY60constfloatYINCUN=0.5;constfloatYINCHI=0.3;voiddouble_printf(doubl 查看详情

cprimerplus(第六版)第三章编程练习答案(代码片段)

 前言:由于不是太复杂,第三章的程序我就全部集成在一个程序了,仅供参考,新手勿喷。(原地copy,xixi~CH03 Codeanswer:#include<stdio.h>intmain() printf("3.11.1\\n"); printf("\\n"); printf( 查看详情

cprimerplus(第六版)第四章编程练习答案(代码片段)

 前言:由于不是太复杂,第四章的程序我就全部集成在一个程序了,仅供参考,新手勿喷。(梅开二度CH04 Codeanswer:#include<stdio.h>#include<string.h> //strlen#include<float.h> //FLT_DIGandDBL_DIGconstfloa 查看详情

cprimerplus(第六版)第七章编程练习答案(代码片段)

前言:由于都是之前写完的题,已经没有什么感想和特别注意点,所以机器式上传复制,如有幸给小伙伴参考学习,有问题欢迎问,仅供参考,新手勿喷。CH07 Codeanswer1-6:#include<stdio.h>#include<ctype.... 查看详情

cprimerplus(第六版)第九章编程练习答案(代码片段)

前言:这周课程比较繁忙,抽出来的时间莫名都去做题了,没太顾着看书,下周开始以看书为主,题目就不怎么做了,emmmmmm扯远了。仅供参考,新手勿喷。CH08 Codeanswer1:#include<stdio.h>doublemin(double,do... 查看详情

cprimerplus(第六版)第八章编程练习答案(代码片段)

前言:这章是昨日刚打完的,其实第五题不是很满意,我也有点搞的一头雾水,当然其他章肯定有点小细节问题我还没发现,突然想起作者写到的有句话“不管你的程序提示打完多好,总会有人吐槽这个程... 查看详情

甩她脸上(cprimerplus第六版基础整合)

CPrimerPlus第六版​​前言​​​​第一章初识C语言​​​​一、C语言的起源​​​​二、C语言的应用​​​​三、C语言的特点​​​​四、编译的过程​​​​五、编码机制​​​​1.简述​​​​2.完成机制​​​​六、在UN... 查看详情

第六章编程练习4

---恢复内容开始---根据用户输入,使用嵌套循环输出如下面的金字塔图案    A   ABA  ABCBA ABCDCDAABCDEDCBA#include<stdio.h>intmain(void){charletter[26]="ABCDEFGHIJKLMNOPQRSTUVWXYZ 查看详情

cprimerplus(第六版)中文版中的错误1

1#include<stdio.h>2#include<stdlib.h>3#include<string.h>4#defineTSIZE4556structfilm{7chartitle[TSIZE];8intrating;9structfilm*next;10};11char*s_gets(char*st,intn);1213intmain(void)14{ 查看详情