《C Prime Plus》第十二章编程练习

132 阅读3分钟

由于电脑对随机函数的支持出了问题,所以只能做部分的题。。。

第一题

#include <stdio.h>
//既然不能使用全局变量,那就最初交的办法,指针构建函数之间的通信,搞起!

void critic(int *);

int main(void) {
    int uints = 0;
    int *ptr = &uints;
    printf("How many pounds to a firkin of butter?\n");
    scanf("%d", &uints);
    while (uints != 56) critic(ptr);
    printf("You must have looker it upp!\n");
    return 0;
}

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

第二题

CMAKElist:

cmake_minimum_required(VERSION 3.20)
project(C_Prime_Plus C)

set(CMAKE_C_STANDARD 11)

add_executable(C_Prime_Plus pel2-2a.h pel2-2a.c pel2-2b.c)

pel2-2a.h

#include <stdio.h>
//头文件存放函数原型
void get_info();

void show_info();

void set_mode(int);

pel2-2a.c

#include <stdio.h>
#include "pel2-2a.h"

static double distance; //文件作用域的内部链接变量

static double goal; //文件作用域的内部链接变量

static int mode; //文件作用域的内部链接变量

void set_mode(int x) {
    if (x == 0 || x == 1) mode = x;
    else printf("Invalid mode specified. Mode %d used.\n", mode);
}


void get_info() {
    if (mode == 0) {
        printf("Enter distance travled in kilometers: ");
        scanf("%lf", &distance);
        while (getchar() != '\n')continue;
        printf("Enter fuel consumed in liters: ");
        scanf("%lf", &goal);
        while (getchar() != '\n')continue;
    } else if (mode == 1) {
        printf("Enter distance traveled in miles: ");
        scanf("%lf", &distance);
        while (getchar() != '\n')continue;
        printf("Enter fuel consumed in gallon: ");
        scanf("%lf", &goal);
        while (getchar() != '\n')continue;
    }
    return;
}

void show_info() {
    double a = goal / (distance / 100);
    double b = distance / goal;
    if (mode == 0) {
        printf("Fuel consumption is %.2f liters per 100 km.\n", a);
    } else if (mode == 1) {
        printf("Fuel consumption is %.2f miles per gallon.\n", b);
    }
}

pel2-2b.c

#include <stdio.h>
#include "pel2-2a.h"


int main(void) {
    int mode; //这个mode变量是属于main()这个块作用域的,与另一个文件的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);
    }
    puts("Done!");
    return 0;
}

第三题

CMAKElist:

cmake_minimum_required(VERSION 3.20)
project(C_Prime_Plus C)

set(CMAKE_C_STANDARD 11)

add_executable(C_Prime_Plus pel2-2a.h pel2-2a.c pel2-2b.c)

pel2-2a.h

#include <stdio.h>

//头文件存放函数原型
void set_mode(int x, int *ptr_mode);

void get_info(const int *ptr_mode, double *ptr_distance, double *ptr_goal) ;

void show_info(const int *ptr_mode, const double *ptr_distance, const double *ptr_goal);

pel2-2a.c

#include <stdio.h>
#include "pel2-2a.h"

void set_mode(int x, int *ptr_mode) {
    if (x == 0 || x == 1) *ptr_mode = x;
    else printf("Invalid mode specified. Mode %d used.\n", *ptr_mode);
}

void get_info(const int *ptr_mode, double *ptr_distance, double *ptr_goal) {
    if (*ptr_mode == 0) {
        printf("Enter distance travled in kilometers: ");
        scanf("%lf", ptr_distance);
        while (getchar() != '\n')continue;
        printf("Enter fuel consumed in liters: ");
        scanf("%lf", ptr_goal);
        while (getchar() != '\n')continue;
    } else if (*ptr_mode == 1) {
        printf("Enter distance traveled in miles: ");
        scanf("%lf", ptr_distance);
        while (getchar() != '\n')continue;
        printf("Enter fuel consumed in gallon: ");
        scanf("%lf", ptr_goal);
        while (getchar() != '\n')continue;
    }
}

void show_info(const int *ptr_mode, const double *ptr_distance, const double *ptr_goal) {
    double a = *ptr_goal / (*ptr_distance / 100);
    double b = *ptr_distance / *ptr_goal;
    if (*ptr_mode == 0) {
        printf("Fuel consumption is %.2f liters per 100 km.\n", a);
    } else if (*ptr_mode == 1) {
        printf("Fuel consumption is %.2f miles per gallon.\n", b);
    }
}

pel2-2b.c

#include <stdio.h>
#include "pel2-2a.h"

int main(void) {
    double distance, goal;
    int select, mode;
    double *ptr_distance = &distance;
    double *ptr_goal = &goal;
    int *ptr_mode = &mode;
    printf("Enter 0 for metric mode, 1 for US mode: ");
    scanf("%d", &select);
    while (select >= 0) {
        set_mode(select, ptr_mode);
        get_info(ptr_mode, ptr_distance, ptr_goal);
        show_info(ptr_mode, ptr_distance, ptr_goal);
        printf("Enter 0 for metric mode, 1 for US mode");
        printf("(-1 to quit)");
        scanf("%d", &select);
    }
    puts("Done!");
    return 0;
}

第四题

#include <stdio.h>

static int count = 0; //使用静态内存的内部链接。

void function(void);

int main(void) {
    for (int i = 0; i < 10; ++i) {
        function();
    }
    printf("%d", count);
    return 0;
}

void function(void) {
    count++;
}

第八题

#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, 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): ");
    }
    puts("Done!");
    return 0;
}

int *make_array(int elem, int val) {
    int *ptr;
    ptr = (int *) malloc(sizeof(int) * elem);
    for (int i = 0; i < elem; ++i) {
        ptr[i] = val;
    }
    return ptr;
}

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

第九题

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

#define LEN 20

static char ar[LEN];//临时数组

void words(int);

int main(void) {
    int num;
    puts("How many words do you wish to enter?");
    scanf("%d", &num);
    words(num);
    printf("Done!");
    return 0;
}

void words(int n) {
    int len;
    printf("Enter 5 words now:\n");
    char **ptr = (char **) malloc(n * sizeof(char *));
    for (int i = 0; i < n; ++i) {
        scanf("%s", ar); //scanf每次都会先把对象擦掉再写入
        len = strlen(ar);
        ptr[i] = (char *) malloc((len + 1) * sizeof(char)); //len+1是为了留出空白符的位置。
        strncpy(ptr[i], ar, len); //将用户输入的单词从临时数组复制到要用到的空间中。
    }
    for (int i = 0; i < n; ++i) {
        puts(ptr[i]);
        free(ptr[i]);
        ptr[i] = NULL; //虽然清除了内存空间,但是指针依然存在,不要让他成为野指针。
    }
    free(ptr);
    ptr = NULL; //虽然清除了内存空间,但是指针依然存在,不要让他成为野指针。
    return;
}