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