/*
============================================================================
1.不使用全局变量,重写12.4的程序
============================================================================
*/
#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.在美国通常以英里每加仑来计算油耗,在欧洲是以升每百公里来计算,下面是某程序的一部分,该
程序让用户选择一个模式(公制和美制),然后收集数据来计算油耗。
*********************************************************************************/
//p12-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 mode,1 for US mode:");
printf("(-1 to quit):");
scanf("%d",&mode);
}
printf("Done.\n");
}
//p12-2a.h
void set_mode(int mode);
void get_info();
void show_info();
//pe12-2a.c
#include <stdio.h>
#include "pe12-2a.h"
static int modes;
static double distance;
static double fuel;
void set_mode(int mode){
modes = mode;
}
void get_info(){
if(modes == 0){
printf("Enter distance traveled in kilometers:");
scanf("%lf",&distance);
printf("Enter fuel consumed in liters:");
scanf("%lf",&fuel);
}else if(modes == 1){
printf("Enter distance traveled in miles:");
scanf("%lf",&distance);
printf("Enter fuel consumed in gallons:");
scanf("%lf",&fuel);
}else if(modes > 1){
printf("Invalid mode specified,Mode 1(US) used.\n");
printf("Enter distance traveled in miles:");
scanf("%lf",&distance);
printf("Enter fuel consumed in gallons:");
scanf("%lf",&fuel);
}
}
void show_info(){
if(modes == 0){
printf("Fuel consumption is %.2f liters per 100 km.\n",fuel/distance*100.0);
}else if(modes >= 1){
printf("Fuel consumption is %.2f miles per gallon.\n",distance/fuel);
}
}
/********************************************************************************
3.重新设计练习二,使用自动变量,程序提供相同的用户界面,也就是说,要提示用户输入模式等。
*********************************************************************************/
//p12-2b.c
#include <stdio.h>
void get_info(int modes,double *distance,double *fuel);
void show_info(int modes,double distance,double fuel);
int main(void){
int mode;
double distance;
double fuel;
printf("Enter 0 for metric mode,1 for US mode:");
scanf("%d",&mode);
while(mode >= 0){
get_info(mode,&distance,&fuel);
show_info(mode,distance,fuel);
printf("Enter 0 for metric mode,1 for US mode:");
printf("(-1 to quit):");
scanf("%d",&mode);
}
printf("Done.\n");
}
void get_info(int modes,double *distance,double *fuel){
if(modes == 0){
printf("Enter distance traveled in kilometers:");
scanf("%lf",distance);
printf("Enter fuel consumed in liters:");
scanf("%lf",fuel);
}else if(modes == 1){
printf("Enter distance traveled in miles:");
scanf("%lf",distance);
printf("Enter fuel consumed in gallons:");
scanf("%lf",fuel);
}else if(modes > 1){
printf("Invalid mode specified,Mode 1(US) used.\n");
printf("Enter distance traveled in miles:");
scanf("%lf",distance);
printf("Enter fuel consumed in gallons:");
scanf("%lf",fuel);
}
}
void show_info(int modes,double distance,double fuel){
if(modes == 0){
printf("Fuel consumption is %.2f liters per 100 km.\n",fuel/distance*100.0);
}else if(modes >= 1){
printf("Fuel consumption is %.2f miles per gallon.\n",distance/fuel);
}
}
/********************************************************************************
3.编写一个函数,它返回函数自身被调用的次数,并在一个循环中测试
*********************************************************************************/
//p12-2b.c
#include <stdio.h>
void ta();
int main(void){
for(int i = 0;i<5;i++)
ta();
printf("Done.\n");
}
void ta(){
static int a = 0;
a++;
printf("第%d次调用\n",a);
}
/********************************************************************************
5.编写产生100个1到10范围的随机数,并且以降序排序。
*********************************************************************************/
//p12-2b.c
#include <stdio.h>
#include <stdlib.h>
void get100(int *p,int n);
void sort(int *p,int n);
int main(void){
int a[100];
get100(a,100);
sort(a,100);
for(int i = 0;i<100;i++){
printf("%d: %d\n",i+1,a[i]);
}
printf("\nDone.\n");
}
void get100(int *p,int n){
for(int i = 0;i<100;i++){
p[i] = (rand() % 10) + 1;
}
}
void sort(int *p,int n){
int temp;
int i,j;
for(i=0;i< 99;i++){
for(j=i+1;j<100;j++){
if(p[i] < p[j]){
temp = p[j];
p[j] = p[i];
p[i] = temp;
}
}
}
}
/********************************************************************************
6.编写产生1000个1到10范围的随机数,并统计每个数产生的次数
*********************************************************************************/
//p12-2b.c
#include <stdio.h>
#include <stdlib.h>
void get1000(int *p,int *s,int n,int m);
int main(void){
int a[1000];
int s[10];
for(int i = 0;i<10;i++){
s[i] = 0;
}
get1000(a,s,1000,10);
for(int i=0;i<10;i++){
printf("%d:%d\n",i+1,s[i]);
}
printf("\nDone.\n");
}
void get1000(int *p,int *s,int n,int m){
for(int i = 0;i<1000;i++){
p[i] = (rand() % 10)+1;
for(int j = 1;j<=m;j++){
if(j == p[i]){
s[j]++;
}
}
}
}
/*
7.编写一个程序,该程序与我们在显示程序清单12.13输出之后所讨论的修改程序具有相同的表现,也就是说,输出应像下面这样:
Enter the number of sets,enter q to stop.
How many sides and how many dice?
Here are 18 sets of 3 6-sided throws:
11 7 10 11 10 15 13 9 12 13 11 9 8 12 8
16 7 9
Enter the number of sets,enter q to stop.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "diceroll.h"
int main(void){
int dice, roll;
int sides;
int sets;
srand((unsigned int) time(0));
printf("Enter the number of sets,enter q to stop.\n");
while(scanf("%d",&sets) == 1 && sets > 0){
printf("How many sides and how many dice?\n");
scanf("%d %d",&sides,&dice);
printf("Here are %d sets of %d %d-sided throws:\n",sets,dice,sides);
for(int i = 0;i<sets;i++){
roll = roll_n_dice(dice,sides);
if(i % 15 == 0) printf("\n");
printf("%d ",roll);
}
printf("\nEnter the number of sets,enter q to stop.\n");
}
return 0;
}
/*
* 8.下面是某程序的一部份,给出函数make_array()和show_array()的定义以使程序完整。函数make_array()接受两个参数。
* 第一个是int数组的元素个数,第二个是要赋给每个元素的值。函数使用malloc()来创建一个适当大小的数组,把每个元素设定
* 为指定的值,并返回一个数组指针,函数show_array()以8个数一行的格式显示数组的内容。
*
* pe12-8.c
*/
#include <stdio.h>
#include <stdlib.h>
int * make_array(int elem,int val);
void show_array(const int ar[],int n);
int main(void){
int * pa;
int size;
int value;
printf("Enter the number of elements: ");
scanf("%d",&size);
while(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)");
scanf("%d",&size);
}
printf("Done.\n");
}
int * make_array(int elem,int val){
int * pd;
pd = (int *) malloc(elem*sizeof(int));
for(int i =0;i<elem;i++){
pd[i] = val;
}
return pd;
}
void show_array(const int ar[],int n){
for(int i = 0;i<n;i++){
if(i % 8 == 0) printf("\n");
printf("%d ",ar[i]);
}
printf("\n");
}