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;
}

 

  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 第七章主要介绍了C语言中的函数,包括函数的定义、调用、参数传递、返回值等方面的内容。具体内容包括: 1. 函数的定义和调用:介绍了如何定义函数以及如何调用函数,包括函数的返回类型、函数名、参数列表和函数体等。 2. 函数的参数传递:介绍了C语言中的参数传递方式,包括值传递和地址传递,以及如何在函数中使用参数。 3. 函数的返回值:介绍了函数的返回值类型和返回值的作用,以及如何在函数中使用返回值。 4. 函数的声明和定义:介绍了函数的声明和定义的区别,以及如何在不同的文件中使用函数。 5. 函数的递归:介绍了递归函数的概念和使用方法,以及递归函数的优缺点。 6. 函数指针:介绍了函数指针的概念和使用方法,以及如何在程序中使用函数指针。 总的来说,第七章是C语言中非常重要的一章,对于理解和使用函数有很大的帮助。 ### 回答2: 《C Primer Plus》第六版第七章主要介绍了C语言中的输入和输出函数。这章的内容包括标准I/O库、`printf()`、`scanf()`等函数以及文件输入输出等。 在这一章中,首先讲解了如何使用标准I/O库进行输入输出。标准I/O库提供了一组函数,可以用于从键盘读取输入,或将结果输出到屏幕上。`printf()`函数可以用于格式化输出,可以控制输出的格式,比如输出特定长度的整数、浮点数等。`scanf()`函数可以用于从键盘读取输入,并将其存储到变量中,也可以使用特定的格式来读取特定类型的数据。 接下来,讲解了如何使用`getchar()`和`putchar()`函数。`getchar()`函数用于从键盘读取单个字符,`putchar()`函数用于向屏幕输出单个字符。 此外,还介绍了文件的输入输出。通过使用`fopen()`函数打开文件,可以读取或写入文件的内容。使用`fprintf()`函数可以将数据写入文件中,使用`fscanf()`函数可以从文件中读取数据并存储到变量中。同时还介绍了如何使用`fclose()`函数关闭文件。 最后,本章还讲解了格式化输出的一些高级特性,比如控制字段宽度、对齐方式以及使用转换说明符等。 通过学习《C Primer Plus》第六版第七章,我们能够了解C语言中输入输出的基本概念和原理,掌握使用输入输出库函数进行读写操作的方法,以及如何进行文件的读写操作。这对于日后编写C语言程序以及处理文件输入输出都有着重要的作用。 ### 回答3: C Primer Plus第六版第七章主要介绍了C语言中的函数。函数是一段完成特定任务的可重复使用的代码块,它可以接收输入参数并返回一个值。 在这一章中,我们学习了如何定义函数并明确函数的返回类型、函数名和参数列表。通过使用函数,我们可以将程序中的代码划分为更小、更可管理的部分。函数的主要好处之一是提高了代码的可读性和可维护性。 我们还学习了传递参数的不同方式,包括按值传递、按地址传递以及传递指针。这些方法允许我们在函数之间传递数据,并在函数内部对数据进行修改。 此外,我们还研究了递归函数的概念。递归函数是指可以调用自身的函数。使用递归可以通过将问题划分为更小的子问题来解决复杂的问题。 在这一章中,我们还学习了函数的作用域和生命周期。函数的作用域定义了函数内部和外部变量的可见性。函数的生命周期指的是函数在程序运行期间的保持状态的时间。 最后,我们还讨论了函数的多文件组织和调用。通过将函数定义和函数声明分离到不同的文件中,我们可以更好地组织和管理大型项目的代码。 通过学习C Primer Plus第六版第七章,我们可以更好地理解和应用函数在C语言中的重要性。掌握函数的知识将有助于我们编写更模块化、可读性更强、可维护性更高的代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值