C Primer Plus 第五章 程序清单and习题——2015.4.21

第五章 程序清单及复习题


程序清单


程序清单5.1
/*shoes1.c 把一双鞋的尺码转换成英寸*/
#include<stdio.h>
#define ADJUST 7.64
#define SCALE 0.325
int main(void)
{
 double shoe,foot;
 shoe = 9.0;
 foot = SCALE*shoe+ADJUST;
 printf("Shoe size(men's)foot length\n");
 printf("%10.1f %15.2f inches\n",shoes,foot);
 return 0;
}

程序清单5.2
/* shoes.2 计算多个鞋 的尺码对应的英寸长度*/
#include<stdio.h>
#define ADJUST 7.64
#define SCALE 0.325
int main(void)
{
 double shoe,foot;
 printf("Shoe size(men's)  foot length\n");
 shoe=3.0;
 while(shoe<18.5)
 {
   foot = SCALE*shoe+ADJUST;
   printf("%10.1f %15.2f inches\n",shoes,foot);
   shoe=shoe+1.0;
  }
 printf("If the shoe fits,wear it.\n");
 return 0;
}

程序清单5.3
/*golf.c高尔夫锦标赛记分卡*/
#include<stdio.h>
int main(void)
{
 int jane,tarzan,cheeta;
 cheeta=tarzan=jane=6;
 printf("           cheeta tarzan jane\n");
 printf("First round score %4d %8d %8d\n",cheeta,tarzan,jane);
return 0;
}

程序清单5.4
/* squares.c---产生20个整数的平方表*/
#include<stdio.h>
int main(void)
{
 int num;
 num=1;
 while(num<=20)
 {
   
   printf("%d sruares is %d",num,num*num);
   num++;
  }
 return 0;
}
程序清单5.5
/*wheat.c      指数增长*/
#include<stdio.h>
#define SQUARES 64
#define CROP 1E15
int main(void)
{
 double current,total;
 int count=1;
 printf("square grains total fraction of\n");
 printf("    added     grain     ");
 printf("US total\n");
 total=current=1.0;
 printf("%d %13.2e %12.2e %12.2e\n",count,current,total,total/CROP);
 while(count<SQUARES)
 {
   count++
   current=2.0*current;
   total=total+current;
   printf("%d %13.2e %12.2e %12.2e\n",count,current,total,total/CROP);


 }
 return 0;
}
程序清单 5.6
/*divide.c我们所知的除法*/
#include<stdio.h>
int main(void)
{
  printf("integer division:5/4 is %d\n",5/4);
  printf("integer division:6/3 is %d\n",6/3);
  printf("integer division:7/4 is %d\n",7/4);
  printf("float disivion:7./4. is %f\n",7./4.);
  printf("mixed disivion: 7./4 is %f\n",7./4);
  return 0;
}
程序清单 5.7
#include<stdio.h>
int main(void)
{
int top,score;
top=score=-(2+5)*6+(4+3*(2+3));
printf("top = %d\n",top);
return 0;
}
程序清单5.8 
#include<stdio.h>
int main(void)
{


int n;
size_t intsize;
intsize=sizeof(int);
printf("n=%d, n has %d bytes:all ints have %d bytes.\n",n,sizeof n,intsize);
return 0;
}

程序清单 5.9
把秒转化分钟和秒
#include<stdio.h>
#define SEC_PER_MIN 60
int main(void)
{
int sec,min,left;
printf("Convert seconds to minutes and seconds!\n");
printf("Enter the number of seconds (<=0 to quit):\n");
scanf("%d",&sec);
while(sec>0)
{
min =sec/SEC_PER_MIN;
left=sec%SEC_PER_MIN;
printf(" %d seconds is %d minutes,%d seconds\n",sec,min,left);
scanf("%d",&sec);
}
return 0;
}

程序清单5.10
/*add_one.c   前缀增量*/
#include<stdio.h>
int main(void)
{
int ultra=0,super=0;
while(super<5)
{
printf("super = %d,ulter = %d  ",super++,++ultra);
printf("super = %d,ulter = %d\n",super,ultra);
}
return 0;
}

程序清单5.11
#include<stdio.h>
int main(void)
{
int a=1,b=1;
int aplus,plusb;
aplus=a++;
plusb=++b;
printf("a aplus b plusb\n");
printf("%1d %5d %5d %5d\n",a,aplus,b,plusb);
return 0;
}

程序清单5.12
#include<stdio.h>
#define MAX 2
int main(void)
{
int count =MAX+1;
while(--count>0)
{
printf("%d bottles of spring water on the wall,\
%d bottles of spring water on the wall!\n",count,count);
printf("Take one down and pass it around,\n");
printf("%d bottles ofspring water!\n\n",count-1);
}
return 0;
}

程序清单5.13
#include<stdio.h>
int main(void)
{
int count,sum;
count=0;
sum=0;
while(count++<20)
{
sum=sum+count;
printf("sum=%d\n",sum);
}

return 0;
}


程序清单5.14
数据类型自动转换
#include<stdio.h>
int main(void)
{
char ch;
int i;
float f1;
f1=i=ch='C';
printf("ch =%c,i=%d,f1=%f\n",ch,i,f1);
ch=ch+1;
i=f1+2*ch;
f1=2.0*ch+i;
printf("ch =%c,i=%d,f1=%f\n",ch,i,f1);
ch=70.17;
printf("ch=%c",ch);
return 0;
}

程序清单 5.15
#include<stdio.h>
void pound(int n);
int main(void)
{
int times=5;
char ch='!';
float f=6.0;
pound(times);
pound(ch);
pound((int)f);
return 0;
}
void pound(int n)
{
while(n-->0)
printf("#");
printf("\n");
}
#include<stdio.h>
const int S_PER_M =60;
const int S_PER_H =3600;
const double M_PER_K = 0.62137;
int main(void)
{
double distk,distm;
double rate;
int min,sec;
int time;
double mtime;
int mmin,msec;
printf("This pragram converts your time for a metric race\n");
printf("to a time for running a mile and to your average\n");
printf("speed in miles per hour.\n");
printf("please enter,in kilometers,the distance run.\n");
scanf("%lf",&distk);
printf("Next enter the time in minutes and secands.\n");
printf("Begin by entering the minutes\n");
scanf("%d",&min);
printf("Now enter secands\n");
scanf("%d",&sec);
time=S_PER_M*min+sec;
// printf("time = %d \n",time);
distm=M_PER_K*distk;
rate=distm/time*S_PER_H;
//printf("rate = %f\n",rate);
mtime=(double)time/distm;
mmin=(int)mtime/S_PER_M;
msec=(int)mtime%S_PER_M;
printf("You run  %1.2f KM (%1.2f miles) in %d min,%d sec\n",distk,distm,min,sec);
printf("That pace correspond to running a mile in %d min, ",mmin);
printf("%d sec,\nYour average speed was %1.2f mph.\n",sec,rate);
return 0;

}

复习题

1. 30  27  1  9
2. 6   50  0  13
3.
#define<stdio.h>
int main(void)
{
 int i=2;
 float n;
 printf("Watch out!Here come a bunch of  fractions!\n");
 while(i<30)
 n=1/i;
 printf("%f",n);
 printf("That's all,folks!");
 return 0;
}


4. 一个scanf(),没有对sec初始化,循环可能无法执行。针对此题,加入if语句,当输入错误时结束本程序
#include<stdio.h>
#define S_TO_M 60;
int main(void)
{
int sec,min,left;
printf("This program converts seconds to minutes and ");
printf("seconds.\n");
printf("Just enter the number of seconds.\n");
printf("Enter 0 to end the program.\n");
scanf("%d",&sec);
while(sec>0)
{
int num;
min=sec/S_TO_M;
left=sec%S_TO_M;
printf("%d sec is %d min,%d sec.\n",sec,min,left);
printf("Next input?\n");
num=scanf("%d",&sec);
if(!num)
sec=0;
}
printf("Bye!\n");
return 0;
}
5.


#include<stdio.h>
#define FORMAT "%s! C is cool!\n"
int main(void)
{
int num=10;
printf(FORMAT,FORMAT);
printf("%d\n",++num);
printf("%d\n",num++);
printf("%d\n",num--);
printf("%d\n",num);
return 0;
}




6#include<stdio.h>
int main(void)
{
char c1,c2;
int diff;
float num;
c1='S';
c2='O';
diff=c1-c2;
num=diff;
printf("%c%c%c: %d %3.2f\n",c1,c2,c1,diff,num);
return 0;
}
7
#include<stdio.h>
#define TEN 10
int main(void)
{
int n=0;
while(n++<TEN)
    printf("%5d",n);
printf("\n");
return 0;
}
8
#include<stdio.h>
#define TEN 103
int main(void)
{
int n=96;
while(n++<TEN)
    printf("%c",n);
printf("\n");
return 0;
}
9、
#include<stdio.h>
int main(void)
{
int x=0;
while(++x<3)
printf("%4d",x);
printf("\n");
x=100;
while(x++<103)
printf("%4d\n",x);
printf("%4d\n",x);
printf("\n");
char ch ='s';
while(ch<'w')
{
printf("%c",ch);
ch++;
}
return 0;
}




10
while条件一直满足,根本停不下来
#define MESG "COMPUTER BYTES DOG"
#include<stdio.h>
int main(void)
{
int n=0;
while(n<5)
{
printf("%s\n",MESG);
   n++;
}
printf("That's all.\n");
return 0;
}




11.


 int x=+10; ++x;c=(a+b)*2; c=a+2*b;


12.--x;m=n%k;p=q/(b-a); x=(a+b)/(c*d);


课后编程
1.
#include<stdio.h>
#define TIME 60
int main(void)
{
int min,hour,mmin;
printf("Please enter minutes\n");
scanf("%d",&min);
while(min>0)
{
hour=min/TIME;
mmin=min%TIME;
printf("%d minutes is %d hour and %d minutes\n",min,hour,mmin);
scanf("%d",&min);
}
return 0;
}


2.
#include<stdio.h>
int main(void)
{
int num,limit;
printf("Please enter a number\n");
scanf("%d",&num);
limit=num+10;
while(num++<limit)
{
printf("%d\n",num);
}
return 0;
}
3.
#include<stdio.h>
#define WEEK 7
int main(void)
{
int days,week,day;
printf("Please enter days\n");
scanf("%d",&days);
week=days/WEEK;
day=days%WEEK;
printf("%d days is %d week and %d day!\n",days,week,day);
return 0;
}
不断输入


#include<stdio.h>
#define WEEK 7
int main(void)
{
int days,week,day;
printf("Please enter days\n");
scanf("%d",&days);
while(days)
{
week=days/WEEK;
day=days%WEEK;
printf("%d days is %d week and %d day!\n",days,week,day);
scanf("%d",&days);
}
return 0;
}
4.
#include<stdio.h>
#define INCH 2.54
#define FEET 12
int main(void)
{
float cm,inch,inchs;
int feet;
printf("Please enter a height in centimeters \n");
scanf("%f",&cm);
while(cm)
{
inchs=cm*INCH;
feet=(int)inchs/FEET;
inch=inchs-feet*FEET;;
printf("%f cm is %f inch\n %f inch is %d feet and %f inch\n",cm,inchs,inchs,feet,inch);
scanf("%f",&cm);
}
return 0;
}


#include<stdio.h>
int main(void)
{
float num;
scanf("%f",&num);
while(num)
{
   printf("%f\n",num);
scanf("%f",&num);
}

return 0;
}


#include<stdio.h>
int main(void)
{
int num;
printf("输入一个整数,输出整型后缀加L形式(如 6L):\n");
scanf("%d",&num);
printf("%Ld\n",num);
printf("%ld\n",num);
return 0;
}
5.
#include<stdio.h>
int main(void)
{
int count,money,day;
count=0;
money=0;
printf("Please enter days\n");
scanf("%d",&day);
while(count++<day)
{
money=money+count;
}
printf("%d days all %d money\n",day,money);
return 0;
}
6.
#include<stdio.h>
int main(void)
{
long long count,money,day;
count=0;
money=0;
printf("Please enter days\n");
scanf("%lld",&day);
while(day)
{
    while(count++<day)
    {
   money=money+count*count;
    }
 printf("%lld days all %lld money\n",day,money);
 scanf("%lld",&day);
 count=0;
 money=0;
}

return 0;
}
7.
#include<stdio.h>
void CUBE(float num);
int main(void)
{   
    float n;
    scanf("%f",&n);
CUBE(n);
return 0;
}
void CUBE(float num)
{
float num1;
num1=num*num*num;
printf("%f cube is %f!",num,num1);
 
}


#include<stdio.h>
void Temperatures(float n);
int main(void)
{
float Fahrenheit;
printf("Please enter temperature:\n");

while(scanf("%f",&Fahrenheit))
{
Temperatures(Fahrenheit);
}
return 0;
}
void Temperatures(float n)
{
const float temp1=1.8;
const float temp2=32.0;
const float temp3=273.16;
float Celsius,Kelvin;
Celsius = temp1*n+temp2;
Kelvin = Celsius+temp3;
printf("%f Fahrenheit is %f Celsius and is %fKelvin\n",n,Celsius,Kelvin);
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值