码迷,mamicode.com
首页 > 其他好文 > 详细

C Primer Plus 第十二章课后习题……2015.5.10

时间:2015-05-11 08:55:00      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:存储类链接和内存管理   c语言   

第十二章课后习题
1、自动存储 寄存器存储 静态空连接
2、静态空连接 内部链接 外部链接
3、静态外部链接  静态内部链接
4、空连接
5、在声明中使用表面这个变量或函数已经定义过
6、都分配一个具有100个int值的数组,calloc还把每个元素设置为零。
7、daisy全局变量  lily局部变量
8、#include<stdio.h>
char color=‘B‘;
void first(void);
void second(void);
int main(void)
{
extern char color;
printf("color in main() is %c\n",color);
first();
printf("color in main()is %c\n",color);
second();
printf("color in main() is %c\n",color);
return 0;
}
void first(void)
{
char color;
color=‘R‘;
printf("color in first() is %c\n",color);
}
void second(void)
{
 
color=‘G‘;
printf("color in second() is %c\n",color);
}
9、a.plink静态变量只用于该文件的下面部分 b.不会保护 函数改
变不了它的值  只能说不能n++计算变量了


编程练习
1、
#include<stdio.h>
//int units=0;
int critic(void );
int main(void)
{
int units;
printf("how many pounds to a firkin of butter?
\n");
scanf("%d",&units);
while(units!=56)
 units= critic();
printf("You must have looked it up! %d 
\n",units);
return 0;
}
int critic(void)
{
int u;
printf("Noluck\n");
scanf("%d",&u );
return u;
}




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.1for us mode:");
printf("(-1 to quit);");
scanf("%d",&mode);
}
printf("Done.\n");
return 0;
}






static int distance;
static int mode1;
int set_mode(int mode)
{  
   if(mode%2==0)
   {
    return mode1=0;
   }
      
   else
      return mode1=1;
}
void get_info()
{
extern mode1;
  if(mode1)
    printf("Enter distance traveled in miles: ");
    else
       printf("Enter distance traveled in kilometers: ");
scanf("%d",&distance);
}
void show_info()
{   
extern int distance;
extern mode1;
double fuel;
if(!mode1)
    {
    printf("Enter fuel consumed in liter: ");
    scanf("%lf",&fuel);
    printf("Fuel consumption is %lf liters per 100 


km\n",fuel/distance*100);
    }
    else
       {
        printf("Enter fuel consumed in gallons: ");
        scanf("%lf",&fuel);
    printf("Fuel consumption is %lf miles per gallon\n ",fuel/distance*1168.72);
       }


}


3
#include<stdio.h>
int set_mode(int mode);
int get_info(int n);
void show_info(int mode1,int distance);
int main(void)
{
int mode;
int mode1;
int distance;
printf("Enter 0 for metric mode.1 for us mode:");
scanf("%d",&mode);
while(mode>=0)
{
 mode1=set_mode(mode);
distance=get_info(mode1);
show_info(mode1,distance);
printf("Enter 0 for metric mode.1for us 


mode:");
printf("(-1 to quit);");
scanf("%d",&mode);
}
printf("Done.\n");
return 0;
}


int set_mode(int mode)
{  
   if(mode%2==0)
   {
    return  0;
   }
      
   else
      return  1;
}
int get_info(int n)
{
  int distance;
  if(n)
    printf("Enter distance traveled in miles: ");
    else
       printf("Enter distance traveled in kilometers: ");
scanf("%d",&distance);
return distance;
}
void show_info(int mode1,int distance)
{   
double fuel;
if(!mode1)
    {
    printf("Enter fuel consumed in liter: ");
    scanf("%lf",&fuel);
    printf("Fuel consumption is %lf liters per 100 


km\n",fuel/distance*100);
    }
    else
       {
        printf("Enter fuel consumed in gallons: ");
        scanf("%lf",&fuel);
    printf("Fuel consumption is %lf miles per gallon\n ",fuel/distance*1168.72);
       }


}


4\
#include<stdio.h>
int recurrence_num();
int main(void)
{
int number;
while((scanf("%d",&number)))
{
recurrence_num();
}
printf("%d",recurrence_num());
return 0;
}
int recurrence_num()
{
static int count=0;
count++;
return count;
}
5\
#include<stdio.h>
#define MAX 100
int rand(void);
int main(void)
{
int arr[MAX];
int i=0,j=0;
int temp;
for(i=0;i<MAX;i++)
  arr[i]=rand()%100;
for(i=0;i<MAX-1;i++)
 for(j=i+1;j<MAX;j++)
 {
if(arr[i]<arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
      }
for(i=0;i<MAX;i++)
{
 if(i%10==0)
 printf("\n");
 printf("%2d ",arr[i]);
}
  
}
int rand(void)
{
static next=1;
next=next*1103515245+12345;
return (unsigned int)(next/65536)%32768;

}
6\
#include<stdio.h>
#include<stdlib.h>
#define MAX 1000
//int rand(void);
int main(void)
{
int arr[MAX];
int i=0 ;
int number_1=0;
int number_2=0;
int number_3=0;
int number_4=0;
int number_5=0;
int number_6=0;
int number_7=0;
int number_8=0;
int number_9=0;
int number_10=0;
for(i=0;i<MAX;i++)
  arr[i]=rand()%10;
for(i=0;i<MAX-1;i++)
 {
  switch(arr[i])
  {
  case 0:number_10++;
        break;
  case 1:number_1++;
        break;
  case 2:number_2++;
        break;
  case 3:number_3++;
        break;
  case 4:number_4++;
        break;
  case 5:number_5++;
        break;
  case 6:number_6++;
        break;
  case 7:number_7++;
        break;
  case 8:number_8++;
        break;
  case 9:number_9++;
        break;
  }
 
 }
 printf("number_1 has %d times\n",number_1);
 printf("number_2 has %d times\n",number_2);
 printf("number_3 has %d times\n",number_3);
 printf("number_4 has %d times\n",number_4);
 printf("number_5 has %d times\n",number_5);
 printf("number_6 has %d times\n",number_6);
 printf("number_7 has %d times\n",number_7);
 printf("number_8 has %d times\n",number_8);
 printf("number_9 has %d times\n",number_9);
 printf("number_10 has %d times\n",number_10);

  return 0;
}
/*
int rand(void)
{
static next=1;
next=next*1103515245+12345;
return (unsigned int)(next/65536)%32768;

}*/
7
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
 int roll_n_dice( int sides);
 int rollem(int sides);
int main(void)
{
int dice,roll;
int sides;
srand((unsigned int)time(0));
printf("Enter the number of sides per die.0 to 


stop.\n");
while(scanf("%d",&sides)==1&&sides>0)
{
// printf("How many dice?\n");
// scanf("%d",&dice);
roll=roll_n_dice( sides);
printf("You have rolled a %d using %d %d 


sided dice.\n",roll,dice,sides);
printf("How many sides?\n");
}
// printf("The rollem %d times.\n",roll_count);
printf("GOOD");
return 0;
}
  int rollem(int sides)
{
int roll;
roll=rand()%sides+1;
 
return roll;
}
int roll_n_dice( int sides)
{
int d;
int total=0;
if(sides<2)
{
printf("Need at least 2 sides.\n");
return -2;
}
/*
if(dice<1)
{
printf("Need at least 1 die.\n");
return -1;
}
*/
// for(d=0;d<dice;d++)
  total=rollem(sides)+rollem(sides);
return total;
}


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("\n");
printf("Enter the number of elements(<1 


to quit):");
scanf("%d",&size);
}
printf("Done.\n");
return 0;

}
int *make_array(int elem,int val)
{
int i=0;
int *ptr=(int*)malloc(elem*sizeof(int));
for(i=0;i<elem;i++)
*(ptr+i)=val;
return ptr;
}
void show_array(const int ar[],int n)
{  
     int i;
     for(i=0;i<n;i++)
     {
   if(i%8==0)
      printf("\n");
printf("%d",ar[i]);
     }

}

C Primer Plus 第十二章课后习题……2015.5.10

标签:存储类链接和内存管理   c语言   

原文地址:http://blog.csdn.net/syl100252/article/details/45629253

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!