c prime plus 第十二章

1.

#include <stdio.h>
void critic(int *p);
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 *p)
{
    printf("No luck, chummy.Try again.\n");
    scanf("%d", p);
}

2.

//pe12-2a.h
#define US 1
#define METRIC 0

void set_mode(int);
void get_info(void);
void show_info(void);
//pe12-2a.c
#include <stdio.h>
#include "pe12-2a.h"

static int present_mode = METRIC;
static double fuel;
static double distance;

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

void get_info()
{
    if(present_mode = METRIC)
    {
        printf("Enter distance traveled distance in kilometers:");
        scanf("%lf", &distance);
        printf("Enter fuel consumed in liters:");
        scanf("%lf", &fuel);
    }
    else
    {
        printf("Enter distance traveled distance in miles:");
        scanf("%lf", &distance);
        printf("Enter fuel consumed in gallons:");
        scanf("%lf", &fuel);
    } 
}

void show_info(void)
{
    if(present_mode == METRIC)
        printf("Fuel consumption is %g liters per 100 km.\n", fuel / (distance/100));
    else
        printf ("Fuel consumption is %g miles per gallon.\n", distance / fuel );
}
//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, 1 for US mode");
        printf("(-1 to quit):");
        scanf("%d", &mode);
    }
    printf("Done.\n");
    return 0;
}

3.

//pe12-3a.h
#define METRIC 0
#define US 1 

void set_mode(int mode, int * present_mode);
void get_info(int mode, int present_mode, double * p_distance, double * p_fuel);
void show_info(int  present_mode, double distance, double pfuel);

//pe12-3a.c
#include <stdio.h>
#include "pe12-3a.h"

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

void get_info(int mode, int present_mode, double * p_distance, double * p_fuel)
{
    if(mode = METRIC)
    {
        printf("Enter distance traveled in kilometers:");
        scanf("%lf", p_distance);
        printf("Enter fuel consumed in liters: ");
        scanf("%lf", p_fuel);
    }
    else
    {
        printf("Enter distance traveled in miles:");
        scanf("%lf", p_distance);
        printf("Enter fuel consumed in gallons: ");
        scanf("%lf", p_fuel);
    }
}

void show_info(int present_mode, double distance, double fuel)
{
    if(present_mode == METRIC)
        printf("Fuel consumption is %g liters per 100km.\n", fuel / (distance / 100));
    else
        printf("Fuel consumption is %g miles per gallon.\n", distance / fuel);
}

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

int main(void)
{
    int mode, present_mode = METRIC;
    double distance, fuel;

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

4.

#include <stdio.h>
int fibonacci(int n);
int count = 0;
int main(void)
{
    int n;

    printf("input the maxium terms of fibonacci:");
    while(scanf("%d", &n) == 1)
    {
        count = 0;
        fibonacci(n);
        printf("The function of fibonacci has call %d times\n", count);
        printf("input the max term of fibonacci:");
    }
    return 0;
}

int fibonacci(int n)
{
    count++;
    if(n > 2)
        return fibonacci(n - 1) + fibonacci(n - 2);
    else
        return 1;
}

5.

#include <stdio.h> 
#include <stdlib.h>
#define WIDTH 100

int main(void)
{
    char num[WIDTH];
    int i, j, temp;

    for(i = 0; i<WIDTH;i++)
        num[i] = rand()  % 10 + 1;
    printf("Original\n");
    for(i = 0; i<WIDTH;i++)
    {
        printf("%d\t", num[i]);
        if(i % 10 == 9)
        printf("\n");
    }
    printf("Sort:\n");
    for(i = 0; i<WIDTH-1;i++)
        for(j = 0; j<WIDTH-1-i; j++)
            if(num[j] < num[j+1])
            {
                temp = num[j];
                num[j] = num[j+1];
                num[j+1] = temp;
            }
    for(i = 0; i<WIDTH; i++)
    {
        printf("%d\t", num[i]);
        if(i %10 == 9)
        printf("\n");
    }
    return 0;
}

6.

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

int main(void)
{
    char seed[10]={0,1,2,3,4,5,6,7,8,9};
    int i, j, count[10];

    for(i = 0; i<10;i++)
    {
        for(j = 0; j<10; j++)
            count[j] = 0;
        srand(seed[i]);
        printf("Seed = %d", seed[i]);
        for(j =0; j<1000;j++)
            count[rand() % 10]++;
        for(j = 0; j<10; j++)
            printf("%d : %d times\t", j+1, count[j]);
        printf("\n");
    }
    return 0;
}

7.

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

int main(void)
{
    char set,side,dice,i,sum;
    puts("Enter the number of sets:enter q to stop.\n");
    while(scanf("%d", &set) == 1)
    {
        srand(time(0));
        puts("How many sides and how many dice?");
        while(scanf("%d %d", &side, &dice) != 2)
        {
            scanf("%*s");
            printf("Input error!Input again:");
        }
        printf("Here are %d sets of %d %d-side throws.\n", set, dice, side);
        while(set--)
        {
            for(i =0, sum = 0; i<dice;i++)
                sum += rand() % side + 1;
            printf("\t%d", sum);
        }
        puts("\nHow many sets?enter q to stop");
    }
    return 0;
}

8.

int * make_array(int elem, int val)
{
    int *p;
    p = (int * p)malloc(sizeof(int));
    while(elem--)
        *(p +elem) = val;
    return p;
}

void show_array(const int ar[], int n)
{
    int i;
    for(i =0; i<n; i++)
    {
        printf("%d"\t, ar[i]);
        if(i%8 == 7)
            printf("\n);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值