C PRIMER PLUS第六版 第十二章编程练习

1.

#include <stdio.h>

void critic(int * num);

int main(void)
{
    int units = 0;

    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 * num)
{
    printf("No luck, my friend. Try again.\n");
    scanf("%d",num);
}

2.

#include <stdio.h>

void set_mode(int);
void get_info(void);
void show_info(void);

static int mode;
static float distance;
static float fuel;

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

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

void show_info(void)
{
    if(mode == 0)
    {
        printf("Fuel comsumption is %lf liters per 100 km\n", (fuel / (distance / 100)));
    }
    else if(mode == 1)
    {
        printf("Fuel comsuption is %lf miles per gallon.\n", distance / fuel);
    }
}

3.

#include <stdio.h>

void get_info(int m, float *, float *);
void show_info(int m, float *, float *);

int main(void)
{
    int mode = 0;
    int pre = 0;
    float distance = 0;
    float fuel = 0;

    printf("Enter 0 for metric mode, 1 for US mode:");
    scanf("%d",&mode);
    while(mode >= 0)
    {
        if(mode != 0 && mode != 1)
        {
            printf("Invalid mode specified. Mode %s used.\n", (pre ? "1(US)" : "0(metric)"));
            mode = pre;
        }
        get_info(mode,&distance,&fuel);
        show_info(mode,&distance,&fuel);
        pre = mode;
        printf("Enter 0 for metric mode, 1 for US mode");
        printf(" (-1 to quit): ");
        scanf("%d", &mode);
    }
    printf("Done.\n");

    return 0;
}

void get_info(int m, float * dis, float * fue)
{
    if(m == 0)
    {
        printf("Enter distance traveled in kilometers: ");
        scanf("%f", dis);
        printf("Enter fuel consumed in liters: ");
        scanf("%f", fue);
    }
    else if(m == 1)
    {
        printf("Enter distance traveled in miles: ");
        scanf("%f", dis);
        printf("Enter fuel consumed in gallons: ");
        scanf("%f", fue);
    }
}

void show_info(int m, float * dis, float * fue)
{
    if(m == 0)
    {
        printf("Fuel consumption is %.2lf liters per 100 km\n", (*fue / (*dis / 100)));
    }
    else if(m == 1)
    {
        printf("Fuel consumption is %.2lf miles per gallon.\n", *dis / *fue);
    }
}

4.

#include <stdio.h>

static int count = 1;  //静态变量count,用来记录测试函数的次数,其实也可以用main中的i来记录。

void ct(void);

int main(void) //测试函数
{
    int i = 0;
    while(i <= 3)
    {
        ct();
        i++;
    }

    return 0;
}

void ct(void)
{
    printf("This is the %dst times using the function.\n",count);
    count += 1;
}

5.

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

int main(void)
{
    int array[100];
    int i;
    int temp;

    srand((unsigned int) time(0));

    for(i = 0; i < 100; i++)
    {
        array[i] = (rand() % 10 + 1);
    }

    printf("Before sorting the array is :\n");
    for(i = 0; i < 100; i++)
    {
        printf("%d ",array[i]);
    }

    for(i = 0; i < 100; i++)
    {
        for(int j = i + 1; j < 100; j++)
        {
            if(array[i] < array[j])
            {
                temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
    }

    printf("\nAfter sorting the array is :\n");
    for(i = 0; i < 100; i++)
    {
        printf("%d ",array[i]);
    }

    return 0;
}

6.

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

int main(void)
{
    int num[100] = {0};
    int randnum;

    srand((unsigned int) time(0));
    for(int i = 0; i < 100; i++)
    {
        randnum = (rand() % 10 + 1);
        num[randnum - 1]++;
    }

    for(int i = 0; i < 10; i++)
    {
        printf("%d: %-5d\n",i+1,num[i]);
    }

    return 0;
}

7.

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

int roll_n_dice(int dice, int sides);
static int rollem(int sides);

int main(void)
{
    int sets;
    int dice, sides;
    int i;

    srand((unsigned int) time(0));  //随机种子
    printf("Enter the number of sets; enter q to stop :");
    while((scanf("%d",&sets)) == 1)
    {
        printf("How many sides and how many dice?");
        scanf("%d %d",&sides,&dice);
        printf("Here are %d sets of %d %d-sided throws.\n",sets,dice,sides);
        for(i = 0; i < sets; i++)
        {
            printf("%d ", roll_n_dice(dice, sides));
        }
        printf("\n");
        while(getchar() != '\n');   //清空输入缓冲区
        printf("How many sets? Enter q to stop : ");
    }

    return 0;
}

static int rollem(int sides)
{
    int roll;
    roll = rand() % sides + 1;

    return roll;
}

int roll_n_dice(int dice, int sides)
{
    int total = 0;
    int d = 0;
    if (sides < 2)
    {
        printf("Need at least 2 sides.\n");
        return -2;
    }
    if (dice < 1)
    {
        printf("Need at least 1 dice.\n");
        return -1;
    }
    for ( d = 0; d < dice; d++)
    {
        total += rollem(sides);
    }

    return total;
}

8.

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

int * make_array(int, int);
void show_array(const int ar[], int);
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 * pt;
   int i;

   pt = (int *)malloc(sizeof(int) * elem);
   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(((i + 1) % 8) == 0)
            printf("\n");
    }
    printf("\n");
}

9.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 64

int main(void)
{
    int words;
    int len;
    char ** pwords;
    char * temp;

    printf("How many words do you wish to enter?");
    scanf("%d",&words);
    pwords = (char **)malloc(words * sizeof(char *));  //数组里储存的是指向char类型的指针,pwords是指向指针的指针
    printf("Enter %d words now:\n",words);
    temp = (char *)malloc(SIZE * sizeof(char)); //分配一段动态内存,用来临时储存每个单词,并返回指向该单词的指针
    for(int i = 0; i < words; i++)
    {
        scanf("%s",temp);   //scanf在从第一个非空格读取到第一个空格时结束第一次读取,用循环来分别读取五个单词,每个单词用字符串形式储存在动态分配的内存中,由temp指向这段内存
        len = strlen(temp); //检查第i+1个单词的长度,用于分配空间
        pwords[i] = (char *)malloc((len + 1) * sizeof(char)); //pwords的元素是指针,这里分配一个动态内存,用pwords中的指针指向该动态内存,且内存的长度是单词长度加一,即多储存一个'\0',字符串结尾
        strcpy(pwords[i],temp); //在pwords[i]指向的内存分配完成后,将该字符串temp拷贝到这个动态内存中
    }
    free(temp); //临时空间使用完毕后释放
    printf("Here are your words:\n");
    for(int i = 0; i < words; i++)
    {
        puts(pwords[i]); //打印pwords[i]所指向的地址中的内容(是一个字符串,即用%s的形式来储存每一个单词),并用puts()自动打印换行符
    }

    free(pwords); //释放内存

    return 0;
}

猜你喜欢

转载自blog.csdn.net/o707191418/article/details/81904734
今日推荐