第12章存储类、链接和内存管理
c primer plus 课本P352中第2、3、4、5、6、7题
第2题:
<span style="font-size:18px;">//Pel2-2a.h:
#include <stdio.h>
#define METRIC 0
#define US 1
void set_mode(int m);
void get_info();
void show_info();</span>
<span style="font-size:18px;">//Pel2-2a.c:
#include <stdio.h>
#include "pel2-2a.h"
static int mode=METRIC;
static int distance;
static double fuel;
void set_mode(int m)
{
mode=m;
if((m==METRIC)||(m==US))
;
else
printf("Invalid mode specified. Mode %s used.\n", m == METRIC ? "0(METRIC)" : "1(US)");
}
void get_info()
{
if (mode==METRIC)
{
printf("enter distance traveled in kilometers:");
scanf("%d",&distance);
printf("enter fuel consumed in liters:");
scanf("%lf",&fuel);
//printf("%f\n",fuel);
//printf("%lf\n",fuel); 这里如果使用%lf,会出问题,打印0.000
}
else
{
printf("enter distance traveled in miles:");
scanf("%d",&distance);
printf("enter fuel consumed in gallons:");
scanf("%lf",&fuel);
}
}
void show_info()
{
// printf("%d\n",distance);
//printf("%f\n",fuel);
if(mode==METRIC)
printf("fuel consumption is %.2f liters per 100 km\n.",(fuel/distance)*100);
else
printf("fuel consumption is %.2f miles per gallon.\n",distance/fuel);
}</span>
<span style="font-size:18px;">//主函数:
#include <stdio.h>
#include "pel2-2a.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");
printf("(-1 to quit):");
scanf("%d",&mode);
}
printf("Done. \n");
return 0;
}</span>
第三题:
#include <stdio.h>
int set_mode();
void get_info(int m,int *d,double *f);
void show_info(int m,int d,double f);
int main()
{
int mode;
int distance;
double fuel;
mode=set_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");
return 0;
}
int set_mode()
{
int m;
printf("Enter 0 for metric mode, 1 for US mode: ");
scanf("%d",&m);
if (m==0||m==1);
else
printf("Invaild mode specificed. Mode %s used\n",m == 0 ? "0(METRIC)" : "1(US)");
return m;
}
void get_info(int m,int *d,double *f)
{
if(m==0)
{
printf("enter distance traveled in kilometers:");
scanf("%d",d);
printf("enter fuel consumed in liters:");
scanf("%lf",f);
}
else
{
printf("enter distance traveled in miles:");
scanf("%d",d);
printf("enter fuel consumed in gallons:");
scanf("%lf",f);
}
}
void show_info(int m,int d,double f)
{
if(m==0)
printf("fuel consumption is %.2f liters per 100 km\n.",(f/d*100));
else
printf("fuel consumption is %.2f miles per gallon.\n",(d/f));
}
4.编写一个函数,返回自身被调用的次数
<span style="font-size:18px;">#include<stdio.h>
int n;
int fun()
{
n++;
return n;
}
int main()
{
int i;
for(i=1;i<=10;i++)
{
printf("函数被调用%d次\n",fun());
}
return 0;
}</span>
5.编写产生100个1到10范围内的随机数的程序,并且以降序排序。
/*编写产生100个1到10范围内的随机数的程序,并且以降序排序*/
#include <stdio.h>
void funsort(int *);
int main()
{
int i;
int a[100];
for(i=0; i<100; i++)
{
a[i]=rand()%10+1;
printf("%3d",a[i]);
if(i%10==9)
printf("\n");
}
funsort(&a);
printf("\n");
printf("排序后的数组为:\n");
for(i=0; i<100; i++)
{
printf("%3d",a[i]);
if(i%10==9)
printf("\n");
}
return 0;
}
void funsort(int *a)
{
int k,t;
int temp;
for(k=0; k<100; k++)
{
for(t=k+1; t<100; t++)
{
if(a[k]<a[t])
{
temp=a[t];
a[t]=a[k];
a[k]=temp;
}
}
}
}
6.编写一个产生1000个1到10范围内的随机数的程序。不必保存或打印数字,仅打印数字被产生的次数。
<span style="font-size:18px;">#include <stdio.h>
int main()
{
int i,j;
int a[1000];
int k[11]= {0};
for(i=0; i<1000; i++)
{
a[i]=rand()%10+1;
//printf("%3d",a[i]);
k[a[i]]++;
if(i%10==9)
printf("\n");
}
for(j=1; j<11; j++)
{
printf("%d 被产生了 %d 次\n",j,k[j]);
}
printf("\n");
return 0;
}</span>
7.编写一个程序,输出如下:
Enter the number of sets; enter q to stop.
18
How many sides and how many dice?
6 3
Here are 18 sets of 3 6-side throws.
12 10 6 9 8 14 8 15 9 14 12 17 11 7 10
13 8 14
How many sets? Enter q to stop.
<span style="font-size:18px;">#include <stdio.h>
#include <stdlib.h> /* for srand() */
#include <time.h> /* for time() */</span>
<span style="font-size:18px;">
int rollem(int);</span>
<span style="font-size:18px;">
int main()
{
int dice, count, roll;
int sides;
int set, sets;
srand((unsigned int) time(0)); /* randomize rand() */
printf("Enter the number of sets; enter q to stop.\n");
while ( scanf("%d", &sets) == 1)
{
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 (set=0;set<sets;set++)
{
for (roll=0,count=0;count<dice;count++)
roll+=rollem(sides);
/* running total of dice pips */
printf("%4d ",roll);
if (set%15==14)
putchar('\n');
}
if (set%15!=0)
putchar('\n');
printf("How many sets? Enter q to stop.\n");
}
return 0;
}</span>
<span style="font-size:18px;">
int rollem(int sides)
{
int roll;
roll=rand()%sides+1;
return roll;
}
</span>