习题十二



1、不使用全局变量,重写程序清单12.4中的程序。

#include <stdio.h>

void critic(int*);

 

int main(void)

{

 int units = 0;

    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、在美国通常是以英里每加仑来计算油耗,在欧洲是以升每百公里来计算。下面是某程序的一部分该程序让用户选择一个模式(公制的或美制的),然后收集数据来计算油耗。

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

 

  return 0;

}

 

 

下面是一些输出示例:

Enter 0 for metric mode, 1 for US mode: 0

Enter distance traveled in kilometers: 600

Enter fuel consumed in liters: 78.8

Fuel consumption is 13.13 liters per 100 km.

Enter 0 for metric mode, 1 for US mode (-1 to quit): 1

Enter distance traveled in miles: 434

Enter fuel consumed  in gallons: 12.7

Fuel consumption is 34.2 miles per gallon.

Enter 0 for metric mode, 1 for US mode (-1 to quit): 3

Invalid mode specified. Mode 1(US) used.

Enter distance traveled in miles: 388

Enter fuel consumed  in gallons: 15.3

Fuel consumption is 25.4 miles per gallon.

Enter 0 for metric mode, 1 for US mode (-1 to quit): -1

Done.

    如果用户键入了不正确的模式,程序向用户给出提示信息并选取最接近的模式。请提供一个头文件pe12-2a.h和源代码文件pe12-2a.c,来使程序可以运行。源代码文件应定义3个具有文件作用域、内部链接的变量。一个代表模式,一个代表距离,还有一个代表消耗的燃料。函数get_info()根据模式设置提示输入相应的数据,并将用户的回答存入文件作用域变量。函数show_info()根据所选的模式计算并显示燃料消耗值。

    //pe12-2a.c

#include <stdio.h>

#include "pe12-2a.h"

 

static int present_mode = METRIC;

static double distance;

static double fuel;

 

void set_mode(int 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(void)

{

 if (present_mode==METRIC)

 {

  printf ("Enter distance traveled in kilometers:");

  scanf ("%lf",&distance);

  printf ("Enter fuel consumed in liters:");

  scanf ("%lf",&fuel);

 }

 else

 {

  printf ("Enter distance traveled 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 mode.1 for US mode:");

  printf(" (-1 to quit): ");

  scanf ("%d",&mode);

 }

 printf ("Done.\n");

    return 0;

}

 

3、重新设计练习2中的程序,使它仅使用自动变量。程序提供相同的用户界面,也就是说,要提示用户输入模式等等。然而,您还必须给出一组不同的函数调用。

//pe12-3a.c

#include <stdio.h>

#include "pe12-3a.h"

 

void set_mode(int mode, int *p_present_mode)

{

 if (mode==METRIC || mode==US)

  *p_present_mode = mode;

 else

  printf("Invalid mode specified. Mode %s used.\n", *p_present_mode == METRIC? "0(METRIC)" : "1(US)");

}

 

void get_info(int mode, int present_mode, double *p_disance, double *p_fuel)

{

 if (present_mode==METRIC)

 {

  printf ("Enter distance traveled in kilometers:");

  scanf ("%lf",p_disance);

  printf ("Enter fuel consumed in liters:");

  scanf ("%lf",p_fuel);

 }

 else

 {

  printf ("Enter distance traveled in miles:");

  scanf ("%lf",p_disance);

  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 100 km.\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;

}

 

 //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_disance, double *p_fuel);

void show_info(int present_mode, double distance, double fuel);

 

4、编写—个函数,它返回函数自身被调用的次数,并在一个循环中测试之。

    #include <stdio.h>

int Fibonacci(int n);

int count;

 

int main(void)

{

 int n;

 printf("input the max term 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:");

 }

 puts("Quit");

 

 return 0;

}

 

int Fibonacci(int n)

{

 count++;

 if( n>2 )

  return Fibonacci(n-1) + Fibonacci(n-2);

 else

  return 1;

}

 

5、编写产生100个l到10范围内的随机数的程序,并且以降序排序(可以将11章中的排序算法稍加改动来对整数进行排序。这里,对数字本身进行排序即可)。

#include <stdio.h>

#include <stdlib.h>

#define WIDTH 100

 

int main(void)

{

    char num[WIDTH],i,j,temp;

 for(i=0; i<WIDTH; i++)

  num[i] = rand() % 10 + 1;

 printf("Original\n");

 for(i=0; i<WIDTH; i++)

 {

  printf("%d\t",num[i]);

 // if(i%10 == 9)   printf("\n");

 }

 printf("Sort:\n");

 for(i=0; i<WIDTH-1; i++)

  for(j=0; j<WIDTH-1-i; j++)

   if (num[j] < num[j+1])

   {

    temp = num[j];

    num[j] = num[j+1];

    num[j+1] = temp;

   }

 

 for(i=0; i<WIDTH; i++)

 {

  printf("%d\t",num[i]);

 // if(i%10 == 9)   printf("\n");

 }

 

    return 0;

}

 

6、编写一个产生1000个l到10范围内的随机数的程序。不必保存或打印数字,仅打印每个数被产生了多少次。让程序对10个不同的种子值进行计算。数字出现的次数相同吗?可以使用本章中的函数或ANSIC中的函数rand()和srand(),它们与我们的函数具有相同的形式。这是一个测试特定随机数发生器的随机性的方法。

#include <stdio.h>

#include <stdlib.h>

 

int main(void)

{

 char i,seed[10]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

 int j,count[10];//存放1~10的10个统计量

 for (i=0; i<10;i++)

 {

  for (j=0; j<10; j++)

   count[j] = 0;

  srand(seed[i]);

  for (j=0; j<1000; j++)

   count[ rand()%10 ]++;

  printf("seed = %d:\n",seed[i]);

  for (j=0; j<10; j++)

   printf("%d: %d times:\t",j+1,count[j]);

  printf("\n");

 }

 

    return 0;

}

 

7、编写一个程序,该程序与我们在显示程序清单12.13的输出之后所讨论的修改版程序具有相同表现。也就是说,输出应像下面这样:

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-sided 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.

q

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

 

int main(void)

{

 char set,side,dice,i,sum;

 puts("Enter the number of sets:enter q to stop");

 while(scanf("%d",&set) == 1)

 {

  srand( time(0) );

  puts("How many sides and how many dice?");

  while(scanf("%d%d", &side, &dice) != 2)

  {

   scanf("%*s");//滤除非法输入

   printf("input error! input again:");

  }

  printf ("Here are %d sets of %d %d-sided throws.\n",set, dice, side);

  while (set--)

  {

   for (i=0,sum=0; i<dice; i++)

    sum += rand() % side +1;

   printf("\t%d",sum);

  }

  puts("\nHow many sets?enter q to stop");

 }

    return 0;

}

 

下面是某程序的一部分:

// pe12-8.c

#include <stdio.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");

 

  return 0;

}

 

8、给出函数make_array()和show_array()的定义以使程序完整。函数make_array()接受两个参数。第一个是int数组的元素个数,第二个是要赋给每个元素的值。函数使用malloc()来创建一个适当大小的数组,把每个元素设定为指定的值,并返回一个数组指针。函数show_array()以8个数一行的格式显示数组内容。

#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("\nEnter the number of elements (<1 to quit):");

  scanf("%d", &size);

 }

 printf("Done.\n");

    return 0;

}

 

int * make_array (int elem, int val)

{

 int *p;

 p = (int *)malloc( elem * sizeof(int) );

 while(elem--)

  *(p + elem) = val;

 return p;

}

 

void show_array (const int ar[], int n)

{

 int i;

 for (i=0;i<n;i++)

 {

  printf("%d\t",ar[i]);

  if(i%8==7) printf("\n");

 }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值