//5.1
#include
#define ADJUST 7.31
int main(void)
{
const double SCALE = 0.333;
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",shoe,foot);
return 0;
}
//5.2
#include
#define ADJUST 7.31
int main(void)
{
const double SCALE = 0.333;
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",shoe, foot);
shoe = shoe + 1.0;
}
printf("If the shoe fits, wear it.\n");
return 0;
}
//5.3
#include
int main(void)
{
int jane, tarzan, cheeta;
cheeta = tarzan = jane = 68;
printf(" cheeta tarzan jane\n");
printf("First round score %4d %8d %8d\n",cheeta, tarzan, jane);
return 0;
}
//5.4
#include
int main(void)
{
int num = 1;
while (num < 21)
{
printf("%4d %6d\n",num, num * num);
num = num + 1;
}
return 0;
}
//5.5
#include
#define SQUARES 64
int main(void)
{
const double CROP = 2E16;
double current, total;
int count = 1;
printf("square grains total ");
printf("fraction of \n");
printf(" added grains ");
printf("world total\n");
total = current = 1.0;
printf("%4d %13.2e %12.2e %12.2e\n",count, current,
total, total/CROP);
while (count < SQUARES)
{
count = count + 1;
current = 2.0 * current;
total = total + current;
printf("%4d %13.2e %12.2e %12.2e\n",count, current,
total,total/CROP);
}
printf("That's all.\n");
return 0;
}
//5.6
#include
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("floating division: 7./4. is %1.2f \n",7./4);
printf("mixed division: 7./4 is %1.2f \n",7./4);
return 0;
}
//5.7
#include
int main(void)
{
int top, score;
top = score = -(2 + 5) * 6 + (4 + 3 * (2 + 3));
printf("top = %d, score = %d\n",top, score);
return 0;
}
//5.8
#include
int main(void)
{
int n = 0;
size_t intsize;
intsize = sizeof(int);
printf("n = %d, n has %zd bytes; all ints have %zd bytes.\n",n,sizeof n,intsize);
return 0;
}
//5.9
#include
#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; // 可以通过 a - (a/b)*b 来计算 a%b .
printf("%d seconds is %d minutes,%d seconds.\n",sec,min,left);
printf("Enter next value(<=0 to quit):\n");
scanf("%d",&sec);
}
printf("Done!\n");
return 0;
}
//5.10
#include
int main(void)
{
int ultra = 0,super = 0;
while (super < 5)
{
super++;
++ultra;
printf("super = %d, ultra = %d \n",super, ultra);
}
return 0;
}
//5.11
#include
int main(void)
{
int a = 1, b = 1;
int a_post, pre_b;
a_post = a++;
pre_b = ++b;
printf("a a_post b pre_b \n");
printf("%ld %5d %5d %5d\n",a,a_post,b,pre_b);
return 0;
}
//5.12
#include
#define MAX 100
int main(void)
{
int count = MAX + 1;
while(--count > 0) {
printf("%d bottles of spring water on the wall, "
"%d bottles of spring water!\n",count,count);
printf("Take one down and pass it aroud,\n");
printf("%d bottles of spring water!\n\n",count - 1);
}
return 0;
}
//5.13
#include
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
int main(void)
{
char ch;
int i;
float fl;
fl = i = ch = 'C';
printf("ch = %c, i = %d, fl = %2.2f\n",ch,i,fl);
ch = ch + 1;
i = fl + 2 * ch;
fl = 2.0 * ch + i;
printf("ch = %c, i = %d, fl = %2.2f\n",ch,i,fl);
ch = 1107;
printf("Now ch = %c\n",ch);
ch = 80.89;
printf("Now ch = %c\n",ch);
return 0;
}
//5.15
#include
void pound(int n);
int main(void)
{
int times = 5;
char ch = '!';
float f = 6.0f;
pound(times);
pound(ch);
pound(f);
return 0;
}
void pound(int n)
{
while(n-- > 0)
printf("#");
printf("\n");
}
//5.16
#include
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 is program 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 seconds.\n");
printf("Begin by entering the minutes.\n");
scanf("%d",&min);
printf("Now enter the seconds.\n");
scanf("%d", &sec);
time = S_PER_M * min + sec; //把时间转化成秒
distm = M_PER_K * distk; //把公里转换成英里
rate = distm / time * S_PER_H; //英里/秒 * 秒/小时 = 英里/小时
mtime = (double)time / distm; //时间/距离 = 跑1英里所用的时间
mmin = (int)mtime / S_PER_M; //求出分钟数
msec = (int)mtime % S_PER_M; //求出剩余的秒数
printf("You ran %1.2f km (%1.2f miles) in %d min, %d sec.\n",distk, distm, min, sec);
printf("That pace corresponds to running a mile in %d min, ",mmin);
printf("%d sec.\nYour avergae speed was %1.2f mph.\n",msec, rate);
return 0;
}