目录
- /Programming Exercise12-1/
- /Programming Exercise12-2/
- /Programming Exercise12-3/
- /Programming Exercise12-4/
- /Programming Exercise12-6/
- /Programming Exercise12-7/
- /Programming Exercise12-8/
/Programming Exercise12-1/
1 /*Programming Exercise12-1*/
2 #include <stdio.h>
3 int critic(void);
4 int main()
5 {
6 int units=0;
7 printf("How many pounds to a firkin of butter?\n");
8 scanf("%d",&units);
9 while(units!=56)
10 units=critic();
11 printf("You must have looked it up!\n");
12 return 0;
13 }
14 int critic(void)
15 {
16 int units;
17 printf("No luck,chummy.Try again.\n");
18 scanf("%d",&units);
19 return units;
20 }
/Programming Exercise12-2/
pe12-2b.c
1
2 #include "pe12-2a.h"
3 #include <stdio.h>
4 void set_mode(int *pm)
5 {
6 extern int prev_mode;//外部变量
7 if (*pm!=METRIC && *pm!=US)
8 {
9 printf("Invalid mode specified.Mode %d will be used\n",prev_mode);
10 *pm=prev_mode;//mode需要利用上一个mode
11 }
12 prev_mode=*pm;//储存当前的mode值,作为下一次的前值
13 }
14 void get_info(int mode,double *pd,double *pf)
15 {
16 if(mode==METRIC)
17 {
18 printf("_______METRIC MODE_________\n");
19 printf("Enter distane traveled in km : ");
20 scanf("%lf",pd);
21 printf("Enter fuel consumed in litter : ");
22 scanf("%lf",pf);
23 }
24 else if(mode==US)
25 {
26 printf("__________US MODE_________\n");
27 printf("Enter distane traveled in miles : ");
28 scanf("%lf",pd);
29 printf("Enter fuel consumed in gallon : ");
30 scanf("%lf",pf);
31 }
32 }
33 void show_info(int mode,double dist,double fuel)
34 {
35 if(mode==METRIC)
36 printf("Fuel consumption is %lf litters per 100 km.\n",(fuel/(dist*100)));
37 else
38 printf("Fuel consumption is %lf miles per gallon km.\n",dist/fuel);
39 }
pe12-2a.c
1
2 #include "pe12-2a.h"
3 #include <stdio.h>
4 int prev_mode=METRIC;//储存上一个mode,文件作用域,外部链接
5 int main()
6 {
7 int mode;
8 double dist;
9 double fuel;
10 printf("Enter 0 for metric mode,1 for US mode: ");
11 scanf("%d",&mode);
12 while(mode>=0)
13 {
14 set_mode(&mode);//检查并设置合适的mode值
15 get_info(mode,&dist,&fuel);//要求用户输入
16 show_info(mode,dist,fuel);//计算并显示结果
17 printf("Enter 0 for metric mode,1 for US mode ");
18 printf("(-1 to quit):");
19 scanf("%d",&mode);
20 }
21 printf("Done\n");
22 return 0;
23 }
pe12-2b.h
1
2 #define METRIC 0
3 #define US 1
4 void set_mode(int *pm);//需要通过指针修改mode
5 void get_info(int mode,double *pd,double *pf);//需要通过指针修改dist和fuel
6 void show_info(int mode,double dist,double fuel);//只需调用不需要修改
/Programming Exercise12-3/
1
2 /*Programming Exercise12-3*/
3 #include <stdio.h>
4 #define METRIC 0
5 #define US 1
6 int set_mode(int mode,int *prev);//需要通过指针修改mode
7 void get_info(int mode,double *pd,double *pf);//需要通过指针修改dist和fuel
8 void show_info(int mode,double dist,double fuel);//只需调用不需要修改
9 int main()
10 {
11 int mode;
12 double dist;
13 double fuel;
14 int prev_mode=METRIC;//储存上一个mode,文件作用域,外部链接
15 printf("Enter 0 for metric mode,1 for US mode: ");
16 scanf("%d",&mode);
17 while(mode>=0)
18 {
19 mode=set_mode(mode,&prev_mode);//检查并设置合适的mode值
20 get_info(mode,&dist,&fuel);//要求用户输入
21 show_info(mode,dist,fuel);//计算并显示结果
22 printf("Enter 0 for metric mode,1 for US mode ");
23 printf("(-1 to quit):");
24 scanf("%d",&mode);
25 }
26 printf("Done\n");
27 return 0;
28 }
29 int set_mode(int mode,int *prev)
30 {
31 if (mode!=METRIC && mode!=US)
32 {
33 printf("Invalid mode specified.Mode %d will be used\n",*prev);
34 mode=*prev;//mode需要利用上一个mode
35 }
36 *prev=mode;//储存当前的mode值,作为下一次的前值
37 return mode;
38 }
39 void get_info(int mode,double *pd,double *pf)
40 {
41 if(mode==METRIC)
42 {
43 printf("_______METRIC MODE_________\n");
44 printf("Enter distane traveled in km : ");
45 scanf("%lf",pd);
46 printf("Enter fuel consumed in litter : ");
47 scanf("%lf",pf);
48 }
49 else if(mode==US)
50 {
51 printf("__________US MODE_________\n");
52 printf("Enter distane traveled in miles : ");
53 scanf("%lf",pd);
54 printf("Enter fuel consumed in gallon : ");
55 scanf("%lf",pf);
56 }
57 }
58 void show_info(int mode,double dist,double fuel)
59 {
60 if(mode==METRIC)
61 printf("Fuel consumption is %lf litters per 100 km.\n",(fuel/(dist*100)));
62 else
63 printf("Fuel consumption is %lf miles per gallon km.\n",dist/fuel);
64 }
/Programming Exercise12-4/
1
2 /*Programming Exercise12-4*/
3 #include <stdio.h>
4 void call_time();
5 int times=0;
6 int i;
7 int main(int argc, char const *argv[])
8 {
9 for(i=0;i<10;i++)
10 {
11 call_time();
12 }
13 printf("%d\n",times);
14 return 0;
15 }
16
17 void call_time()
18 {
19 times++;
20 }
1
2 ###/*Programming Exercise12-5*/
3 #include <stdio.h>
4 #include <stdlib.h>
5 #define SIZE 100
6 int main(int argc, char const *argv[])
7 {
8 int i,j;
9 int num[SIZE];
10 int temp;/*用于交换的中间容器*/
11 srand(time(NULL));
12 for(i=0;i<SIZE;i++)
13 {
14 num[i]=rand()%10;/*生成0-10的随机数,储存在一个数组中等待排序
15 }
16 for(i=0;i<SIZE;i++)/*打印原始数据*/
17 {
18 printf("%d ", num[i]);
19 }
20 printf("\n");
21 for(i=0;i<SIZE;i++)/*冒泡排序*/
22 {
23 for(j=SIZE-1;j>i;j--)
24 {
25 if (num[j]>num[j-1])
26 {
27 temp=num[j-1];
28 num[j-1]=num[j];
29 num[j]=temp;
30 }
31
32 }
33 }
34 for(i=0;i<SIZE;i++)
35 {
36 printf("%d ", num[i]);
37 }
38 printf("\n");
39 return 0;
40 }
/Programming Exercise12-6/
1
2 /*Programming Exercise12-6*/
3 #include <stdio.h>
4 #include <stdlib.h>
5 int main()
6 {
7 int num;
8 int temp[10]={0};
9 int i;
10 int total=0;
11 srand(time(NULL));
12 for(i=0;i<1000;i++)
13 {
14 num=rand()%10;
15 temp[num]++;/*用产生的数做索引,并把相应位置的数+1*/
16 }
17 for(num=0;num<10;num++)
18 {
19 printf("%d :%d\n", num,temp[num]);
20 total+=temp[num];
21 }
22 printf("\n");
23 printf("total:%d\n",total );
24 return 0;
25 }
/Programming Exercise12-7/
12-7.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <time.h>
4 #include "diceroll.h"
5
6 int main(int argc, char const *argv[])
7 {
8 int roll;
9 int dice,sides,sets;
10 int i;
11 srand((unsigned int)time(NULL));
12 printf("Enter the number of sets;q to stop.\n");
13 while(scanf("%d",&sets)==1 && sets>0)
14 {
15 printf("How many sides and how many dice?\n");
16 scanf("%d %d",&sides,&dice);
17 printf("Here are %d sets of %d %d-sided throw.\n ",sets,sides,dice );
18 for(i=0;i<sets;i++)
19 {
20 roll=roll_n_dice(dice,sides);
21 printf("%d ", roll);
22 }
23 printf("\n");
24 printf("Enter the number of sets;q to stop.\n");
25 }
26 return 0;
27 }
/diceroll.c/
1 /*diceroll.c*/
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include "diceroll.h"
5 static int rollem(int sides)
6 {
7 int roll;
8 roll=rand()%sides+1;//产生1-sides的随机数
9 return roll;
10 }
11 int roll_n_dice(int dice,int sides)
12 {
13 int i;
14 int total=0;
15 if (sides<2 )
16 {
17 printf("Need at least 2 sides.\n");
18 return -2;
19 }
20 if (dice<1)
21 {
22 printf("Need at least 1 die.\n");
23 return -1;
24 }
25 for(i=0;i<dice;i++)
26 total+=rollem(sides);
27 return total;
28 }
/diceroll.h/
1 /*diceroll.h*/
2 int roll_n_dice(int dice,int sides);
/Programming Exercise12-8/
1 /*Programming Exercise12-8*/
2 #include <stdio.h>
3 #include <stdlib.h>
4 int * make_array(int elem,int val);
5 void show_array(const int ar[],int n);
6 int main()
7 {
8 int *pa;
9 int size;
10 int value;
11 printf("Enter the number of elements: ");
12 scanf("%d",&size);
13 while(size>0)
14 {
15 printf("Enter the initialization value: ");
16 scanf("%d",&value);
17 pa=make_array(size,value);//如果没有分配成功返回NULL
18 if(pa)//分配成功
19 {
20 show_array(pa,size);
21 free(pa);//使用malloc就要free
22 }
23 printf("Enter the number of elements(<1 to stop)\n");
24 scanf("%d",&size);
25 }
26 printf("Done.\n");
27 return 0;
28 }
29 int * make_array(int elem,int val)
30 {
31 int *p;
32 int *start;
33 int i;
34 p=(int *)malloc(sizeof(int)*elem);//分配elem个int大小的空间
35 start=p;//保存指针初始位置
36 for(i=0;i<elem;i++)
37 {
38 *(p++)=val;//指针移动,所以要保存初始指针位置
39 }
40 return start;//返回数组首地址
41 }
42 void show_array(const int ar[],int n)//使用const防止修改数组
43 {
44 int i;
45 for(i=0;i<n;i++)
46 {
47 if (i%8==0)
48 printf("\n");//每个8个数字进行一次换行
49 printf("%d ", ar[i]);
50 }
51 printf("\n");
52 }
源代码: