1. 程式人生 > >C PRIMER PLUS第六版 第十二章程式設計練習

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

相關推薦

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 but

C++學習筆記——C++ Primer Plus中文 STL程式設計練習解答

發現答案資源不全,因此貼出自己的解答,都為STL應用基礎題,如有謬誤,還請不吝賜教。 第一題 要求:迴文字串判斷(假定字串中沒有大小寫、空格、標點符號等問題) 解答: #include<iostream> #include<string>

C primer plus 程式設計練習答案

#include<stdio.h> int main(void) { int lower = 0; // Save input value. int uper = 0; //

C primer plus 一題 程式設計練習答案

#include<stdio.h> int main(void) { int i = 0; // Create for loop. char n = '\n'; int int

C primer plus 程式設計練習答案

#include<stdio.h> #define l 1.0 int main(void) { int i = 0; // Create for loop. i

C primer plus 程式設計練習答案

#include<stdio.h> int main(void) { int i = 0; // Save years. float Da = 100; f

C primer plus 七題 程式設計練習答案

#include<stdio.h> int main(void) { int i = 0; // Create for loop. And count years.

C primer plus 第一題 程式設計練習答案

Github地址:φ(>ω<*)這裡這裡。 /*     本次任務為設計一個獲取輸入字元(包括空白字元)的函式,       並把結果儲存在一個數組裡,它的地址被傳遞作為一個引數。 */   #i

C primer plus 四題 程式設計練習答案

Github地址:φ(>ω<*)這裡這裡。 /*    本程式應 習題 - 14 建立。      題目要求: 以變長陣列作為函式形參,完成程式設計練習13。          &

C primer plus 十三題 程式設計練習答案

Github地址:φ(>ω<*)這裡這裡。 /*     本程式應 習題-13 建立。      題目要求: 編寫一個程式,提示使用者輸入3組數,每組數包含5個double型別的數。      &nb

C primer plus 程式設計練習答案

Github地址: φ(>ω<*)這裡這裡。   /*    本程式應 習題-12 建立。     題目要求: 重寫程式清單 10.7 的 rain.c 程式,把 main() 中的主要任務都改成用函式來完成。 */ #inc

C primer plus 一題 程式設計練習答案

Github地址:φ(>ω<*)這裡這裡。 /*     本程式應 習題-11 建立。      題目要求: 編寫一個程式,宣告一個 int 型別的 3X5 二維陣列,並用合適的值初始化它。     

C primer plus 程式設計練習答案

Github地址:φ(>ω<*)這裡這裡。 /*     本程式應 習題-10 建立。      題目要求: 編寫一個函式,把兩個陣列中相對應的元素相加,然後把結果儲存到第3個數組中。      &n

C primer plus 九題 程式設計練習答案

Github地址:φ(>ω<*)這裡這裡。 /*     本程式應 習題-9 建立。       題目要求: 編寫一個程式,初始化一個double型別的3x5二維陣列,使用一個處理變長陣列的函式將其拷貝至另一個二維陣列中

C primer plus 七題 程式設計練習答案

Github地址: φ(>ω<*)這裡這裡。 /*     本程式應 習題-7 建立。      題目要求: 編寫一個程式,初始化一個 double 型別的二維陣列,        &nbs

C primer plus 程式設計練習答案

Github地址: φ(>ω<*)這裡這裡。 /*    本程式應 習題-6 建立。     題目要求: 編寫一個函式,把double型別陣列中的資料倒序排列,並在一個簡單的程式中測試該函式。 */   #include<

C primer plus 五題 程式設計練習答案

Github地址:φ(>ω<*)這裡這裡。 /* 本程式應 習題-5 建立。 題目要求: 編寫一個函式,返回儲存在 double 型別陣列中最大值和最小值的差值,並在一個簡單的程式中測試該函式。 */ #include<stdio.h> #define o

C primer plus 四題 程式設計練習答案

Github地址:φ(>ω<*)這裡這裡。 /*     本程式應 習題-4 建立。       題目要求: 編寫一個函式,返回儲存在 double 型別陣列中的最大值的下標,並在一個簡單的程式中測試該函式。 */ &n

C primer plus 八題 程式設計練習答案

Github地址: φ(>ω<*)這裡這裡。 /*    本程式應 習題-8 建立。     題目要求: 使用程式設計練習 2 中的拷貝函式,把一個內含7個元素的陣列中的第3-第5個元素拷貝至內含3個元素的陣列中。   

C primer plus 三題 程式設計練習答案

Github地址:φ(>ω<*)這裡這裡。 /*     本程式應 習題-3 建立。       題目要求: 編寫一個函式,返回儲存在 int 型別陣列中的最大值,並在一個簡單的程式中測試該函式。 */   #