第一题
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("12.9.1:\n");
printf("How many pounds to a firkin of butter?\n");
int units;
while(scanf("%d",&units)==1){
if(units==56){
printf("You must have looked it up!\n");
break;
}
else{
printf("No luck, my friend. Try again.\n");
}
}
return 0;
}
第二题
#include "pe12-2a.h"
#include <stdio.h>
#include <stdlib.h>
static int m;
static double s;
static double f;
void set_mode(int mode ){
if(mode==0||mode==1){
m=mode;
}
else{
printf("Invalid mode specified. Mode %s used.\n", (m? "1US" : "0(metric)"));
}
}
void get_info(){
printf("Enter distance traveled in %s: ",m==0 ? "kilometers":"miles");
scanf("%lf",&s);
printf("Enter fuel consumed in %s: ",m==0 ? "liters":"gallons");
scanf("%lf",&f);
}
void show_info(){
if(m==0)
printf("Fuel consumption is %.2f liters per 100km.\n",f/s*100);
else if(m==1)
printf("Fuel consumption is %.1f miles per gallons.\n",s/f);
}
#include "pe12-2a.h"
#include <stdio.h>
#include <stdlib.h>
int main(){
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 mode, 1 for US mode(-1 tp quir): ");
scanf("%d",&mode);
}
printf("Done!");
return 0;
}
#ifndef PE12-2A_H
#define PE12-2A_H
void set_mode(int );
void get_info();
void show_info();
#endif // PE12-2A_H
第三题
#include