C Primer Plus 第六版(中文版)第十二章(完美版)编程练习答案

//本博主所写的代码仅为阅读者提供参考;
//若有不足之处请提出,博主会尽所能修改;
//附上课后编程练习题目;
//若是对您有用的话请点赞或分享提供给它人;



//12.9 - 1.c

#include <stdio.h>

void critic(int *units);

int main(void)
{
    
    
    int units;

    printf("How many pounds to a firkin of butter?\n");
    scanf("%d", &units);

    while (units != 56)
    {
    
    
        critic(&units);
    }
    printf("You must have looked it up!\n");

    return 0;
}

void critic(int *units)
{
    
    
    printf("No luck, my friend. Try again.\n");
    scanf("%d", units);
    return;
}

//-------------

//12.9 - 2.c
//注意:要分成多个编译单元进行同时编译;

//----pe12-2a.h-----;
void set_mode(int n);
void get_info(void);
void show_info(void);
//------------------;

//----pe12-2b.c-----;
#include <stdio.h>
#include "pe12-2a.h"

int main(void)
{
    
    
    int mode;

    printf("Enter 0 for metric mode, 1 for US mode:");
    scanf("%d", &mode);
    while (mode >= 0)
    {
    
    
        set_mode(mode);
        get_info();
        show_info();
        printf("Enter 0 for metric mode, 1 for US mode");
        printf(" (-1 to quit): ");
        scanf("%d", &mode);
    }
    printf("Done.\n");

    return 0;
}
//------------------;

//----pe12-2a.c-----;
#include <stdio.h>

static int mode;
static double range;
static double fuel;

void set_mode(int n)
{
    
    
    if (n > 1)
    {
    
    
        printf("Invalid mode specified. Mode %s used.\n",
               (0 == mode) ? "0(metric)" : "1(US)");
    }
    else
    {
    
    
        mode = n;
    }
    return;
}

void get_info(void)
{
    
    
    if (0 == mode)
    {
    
    
        printf("Enter distance traveled in kilometers: ");
    }
    else
    {
    
    
        printf("Enter distance traveled in miles: ");
    }
    scanf("%lf", &range);

    if (0 == mode)
    {
    
    
        printf("Enter fuel consumed in liters: ");
    }
    else
    {
    
    
        printf("Enter fuel consumed in gallons: ");
    }
    scanf("%lf", &fuel);

    return;
}

void show_info(void)
{
    
    
    if (0 == mode)
    {
    
    
        printf("Fuel consumption is %.2lf liters per 100km.\n",
               (fuel / range) * 100);
    }

    else
    {
    
    
        printf("Fuel consumption is %.1lf miles per gallon.\n",
               range / fuel);
    }
    return;
}
//------------------;

//-------------

//12.9 - 3.c
//注意:要分成多个编译单元进行同时编译;

//----pe12-2a.h-----;
void set_mode(int *mode, int *n);
void get_info(int mode, double *range, double *fuel);
void show_info(int mode, double range, double fuel);
//------------------;

//----pe12-2b.c-----;
#include <stdio.h>
#include "pe12-2a.h"

int main(void)
{
    
    
    int temp, mode;
    double range, fuel;

    printf("Enter 0 for metric mode, 1 for US mode:");
    scanf("%d", &mode);
    temp = mode;
    while (mode >= 0)
    {
    
    
        set_mode(&mode, &temp);
        get_info(temp, &range, &fuel);
        show_info(temp, range, fuel);
        printf("Enter 0 for metric mode, 1 for US mode");
        printf(" (-1 to quit): ");
        scanf("%d", &mode);
    }
    printf("Done.\n");

    return 0;
}
//------------------;

//----pe12-2a.c-----;
#include <stdio.h>

void set_mode(int *mode, int *n)
{
    
    
    if (*mode > 1)
    {
    
    
        printf("Invalid mode specified. Mode %s used.\n",
               (0 == *n) ? "0(metric)" : "1(US)");
    }
    else
    {
    
    
        *n = *mode;
    }
    return;
}

void get_info(int mode, double *range, double *fuel)
{
    
    
    if (0 == mode)
    {
    
    
        printf("Enter distance traveled in kilometers: ");
    }
    else
    {
    
    
        printf("Enter distance traveled in miles: ");
    }
    scanf("%lf", range);

    if (0 == mode)
    {
    
    
        printf("Enter fuel consumed in liters: ");
    }
    else
    {
    
    
        printf("Enter fuel consumed in gallons: ");
    }
    scanf("%lf", fuel);

    return;
}

void show_info(int mode, double range, double fuel)
{
    
    
    if (0 == mode)
    {
    
    
        printf("Fuel consumption is %.2lf liters per 100 km.\n",
               (fuel / range) * 100);
    }
    else
    {
    
    
        printf("Fuel consumption is %.1lf miles per gallon.\n",
               range / fuel);
    }
    return;
}
//------------------;

//-------------

//12.9 - 4.c

#include <stdio.h>

static int count = 0;

int counter(void);

int main(void)
{
    
    
    int i, j, k;
    printf("请输入一个正整数:");
    scanf("%d", &i);

    for (j = 1; j <= i; j++)
    {
    
    
        printf("count = %d\n", counter());
    }
    printf("函数总共被调用了%d次\n", count);

    return 0;
}

int counter(void)
{
    
    
    return ++count;
}

//-------------

//12.9 - 5.c

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define LEN 100

void sort(int a[], int n);
void show_array(const int a[], int n);

int main(void)
{
    
    
    int i, a[LEN];

    srand((unsigned int)time(0));
    for (i = 0; i < LEN; i++)
    {
    
    
        a[i] = rand() % 10 + 1;
    }
    printf("最初的%d个数是:\n", LEN);
    show_array(a, LEN);
    sort(a, LEN);
    printf("\n降序排序后的%d个数是:\n", LEN);
    show_array(a, LEN);

    return 0;
}

void sort(int a[], int n)
{
    
    
    int i, j, t;

    for (i = 0; i < n - 1; i++)
    {
    
    
        for (j = i + 1; j < n; j++)
        {
    
    
            if (a[i] < a[j])
            {
    
    
                t = a[j];
                a[j] = a[i];
                a[i] = t;
            }
        }
    }
    return;
}

void show_array(const int a[], int n)
{
    
    
    int i;

    for (i = 0; i < n; i++)
    {
    
    
        printf("%-3d", a[i]);
    }
    putchar('\n');
    return;
}

//-------------

//12.9 - 6.c

//也可用标准库中的rand和srand;
//本程序用的是书上所说明的2个函数的具体实现;
//便于充分理解rand和srand两个函数;
#include <stdio.h>
#define N 10
#define LEN 1000

int myrand(void);
void mysrand(unsigned int seed);

static unsigned long int next = 1;

int main(void)
{
    
    
    int i, temp, a[N + 1];
    unsigned int seeds;

    for (seeds = 1; seeds <= N; seeds++)
    {
    
    
        printf("第%d次生成随机数:\n", seeds);
        mysrand(seeds);
        for (i = 0; i < N + 1; i++)
        {
    
    
            a[i] = 0;
        }
        for (i = 0; i < LEN; i++)
        {
    
    
            temp = myrand() % 10 + 1;
            a[temp]++;
        }
        for (i = 1; i < N + 1; i++)
        {
    
    
            printf("%d出现了%d次\n", i, a[i]);
        }
        printf("随机数总数:%d个\n\n", LEN);
    }

    return 0;
}

int myrand(void)
{
    
    
    //↑rand函数的实现;
    next = next * 1103515245 + 12345;
    return (unsigned int)(next / 65536) % 32768;
}

void mysrand(unsigned int seed)
{
    
    
    //↑srand函数的实现;
    next = seed;
    return;
}

//-------------

//12.9 - 7.c

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

int rollem(int sides);

int main(void)
{
    
    
    int dice, count, roll;
    int sides;
    int set, sets;

    srand((unsigned int)time(0));
    printf("Enter the number of sets.\nEnter q to stop: ");
    while (scanf("%d", &sets) == 1)
    {
    
    
        printf("How many sides and how many dice?\n");
        printf("Please two integers: ");
        if (scanf("%d %d", &sides, &dice) != 2)
        {
    
    
            puts("Not integers -- terminating input loop.");
            break;
        }
        printf("Here are %d sets of %d %d-sided throws.\n", sets, dice, sides);
        for (set = 0; set < sets; set++)
        {
    
    
            for (roll = 0, count = 0; count < dice; count++)
            {
    
    
                roll += rollem(sides);
            }
            printf("%-3d", roll);
            if (0 == (set + 1) % 8)
            {
    
    
                putchar('\n');
            }
        }
        printf("\nHow many sets? Enter q to stop: ");
    }
    puts("GOOD FORTUNE TO YOU!\n");

    return 0;
}

int rollem(int sides)
{
    
    
    return rand() % sides + 1;
}

//-------------

//12.9 - 8.c

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

int *make_array(int elem, int val);
void show_array(const int ar[], int n);

int main(void)
{
    
    
    int *pa;
    int size;
    int value;

    printf("Enter the number of elements: ");
    while (scanf("%d", &size) == 1 && size > 0)
    {
    
    
        printf("Enter the initialization value: ");
        scanf("%d", &value);
        pa = make_array(size, value);
        if (pa)
        {
    
    
            show_array(pa, size);
            free(pa);
        }
        printf("Enter the number of elements (<1 to quit): ");
    }
    printf("Done.\n");

    return 0;
}

int *make_array(int elem, int val)
{
    
    
    int i;
    int *pt;
    pt = (int *)malloc(elem * sizeof(int));

    if (NULL == pt)
    {
    
    
        printf("Memory allocation failed!\n");
        exit(EXIT_FAILURE);
    }
    printf("Output %d numbers:\n", val);
    for (i = 0; i < elem; i++)
    {
    
    
        pt[i] = val;
    }
    return pt;
}

void show_array(const int ar[], int n)
{
    
    
    int i;

    for (i = 0; i < n; i++)
    {
    
    
        printf("%d ", ar[i]);
        if (0 == (i + 1) % 8)
        {
    
    
            putchar('\n');
        }
    }
    putchar('\n');
    return;
}

//-------------

//12.9 - 9.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LEN 100

int main(void)
{
    
    
    char **pt;
    char temp[LEN];
    int i, n, length;

    printf("How many words do you wish to enter? ");
    scanf("%d", &n);
    pt = (char **)malloc(n * sizeof(char *));
    if (NULL == pt)
    {
    
    
        printf("Memory allocation failed!\n");
        exit(EXIT_FAILURE);
    }

    printf("Enter %d words now:", n);
    for (i = 0; i < n; i++)
    {
    
    
        scanf("%99s", temp);
        length = strlen(temp) + 1;
        //↑+1便于保存空字符'\0';
        pt[i] = (char *)malloc(length * sizeof(char));
        //↑使用malloc分配足够的存储空间来存储单词;
        if (NULL == pt[i])
        {
    
    
            printf("Memory allocation failed!\n");
            exit(EXIT_FAILURE);
        }
        strcpy(pt[i], temp);
        //↑从临时数组中把单词拷贝到动态分配的存储空间中;
    }

    printf("Here are your words:\n");
    for (i = 0; i < n; i++)
    {
    
    
        puts(pt[i]);
        free(pt[i]);
        pt[i] = NULL;
    }
    free(pt);
    pt = NULL;
    //↑指针仍然指向malloc分配的存储空间;
    //↑因此令指针指向NULL后防止内存滥用;

    return 0;
}

//-------------

//----------------------------2020年4月4日 -------------------------------

//致敬英雄,缅怀同胞,愿山河无恙,人间皆安!

猜你喜欢

转载自blog.csdn.net/m0_46181359/article/details/105311306