// chapter 5
//example 5.1
//#include <stdio.h>
//#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;
//
//}
//example 5.2
//#include <stdio.h>
//#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) //without";" true or false?
// {
// foot=SCALE*shoe+ADJUST;
// printf("%10.1f %15.2f inches\n",shoe,foot);
// shoe = shoe+1.0;
// } //true
// printf("If the shoe fits,wear it.\n"); //false
// return 0;
//}
//5.2.1"="
//assignment is performed from right to left
//example5.3
//#include <stdio.h>
//int main(void)
//{
// int jane,tarzan,cheeta;
// cheeta=tarzan=jane=68;
// printf(" cheeta tarzan jane\n");
// printf("Firest round score %4d %8d %8d\n",cheeta,tarzan,jane);
// return 0;
//}
//example 5.4
//#include <stdio.h>
//int main(void)
//{
// int num=1;
// while (num<21)
// {
// printf("%4d %6d\n",num,num*num);
// num=num+1;
//
// }
// return 0;
//}
try5.5
//#include <stdio.h>
//int main(void)
//{
// int n;
// double grain,total;
// n=1;
// grain=1;
// total=1;
// printf("n new total\n");
// while (n<65)
// {
// printf("%d %10.2e %13.2e\n",n,grain,total);
// n=n+1;
// grain=grain*2;
// total=total+grain;
//
//
// }
// return 0;
//}
//example5.5
//#include <stdio.h>
//#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; /* 从1颗谷粒开始 */
//printf("%4d %13.2e %12.2e %12.2e\n", count, current,
//total, total / CROP);
//while (count < SQUARES)
//{
//253
//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;
//}
//example 5.6
//#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("floating division: 7./4. is %1.2f \n", 7. / 4.);
// printf("mixed division: 7./4 is %1.2f \n", 7. / 4);
// return 0;
//}
//example5.7
//#include <stdio.h>
//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;
//}
//example5.8
//#include <stdio.h>
//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;
//}
//to creat an alia for existing types:
//typedef double real
//%
//#include <stdio.h>
//#define SEC_PER_MIN 60
//int main(void)
//{
// int sec,min,left;
// printf("Convert seconds to mintutes 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);
// printf("Enter next value (<=0 to quit):\n");
// scanf("%d",&sec);
// }
// printf("Done!\n");
// return 0;
//}
//++
//example 5.3.3
//#include <stdio.h>
//int main(void)
//{
// int ultra=0,super=0;
// while (super<5)
// {
// super++;
// ++ultra;
// printf("super=%d,ultra=%d \n",super,ultra);
//
// }
// return 0;
//}
//example 5.2
//#include <stdio.h>
//#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) //without";" true or false?
// {
// foot=SCALE*shoe+ADJUST;
// printf("%10.1f %15.2f inches\n",shoe,foot);
// shoe = shoe+1.0;
// } //true
// printf("If the shoe fits,wear it.\n"); //false
// return 0;
//}
//example 5.2
//#include <stdio.h>
//#define ADJUST 7.31
//int main(void)
//{
// const double SCALE = 0.333;
// double shoe,foot;
// printf("Shoe size (men's) foot length\n");
// shoe=2.0;
// while (++shoe<18.5) //without";" true or false?
// {
// foot=SCALE*shoe+ADJUST;
// printf("%10.1f %15.2f inches\n",shoe,foot);
// } //true
// printf("If the shoe fits,wear it.\n"); //false
// return 0;
//}
//example5.11
//#include <stdio.h>
//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;
//}
/*a_post = a++; // 后缀:使用a的值赋值后,递增a
b_pre= ++b; // 前缀:使用b的值赋值前,递增b */
//--
//example 5.12
//#include <stdio.h>
//#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 around,\n");
// printf("%d bottles of spring water!\n\n",count-1);
// }
// return 0;
// }
//examole5.13
//#include <stdio.h>
//int main(void)
//{
// int count,sum;
// count=0;
// sum=0;
// while (++count<21)
// {
// sum=count+sum;
// }
// printf("sum = %d",sum);
// return 0;
//}
//#include <stdio.h>
//int main(void) /* 计算前20个整数的和 */
//{
//int count, sum; /* 声明[1] */
//count = 0; /* 表达式语句 */
//sum = 0; /* 表达式语句 */
//while (count++ < 20) /* 迭代语句 */
//sum = sum + count;
//printf("sum = %d\n", sum); /* 表达式语句[2] */
//return 0; /* 跳转语句 */
//}
//???5.5
//example 5.14
//#include <stdio.h>
//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;
//}
//
//example 5.15
//#include <stdio.h>
//void pound(int n); //define a function with parameters
//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");
//
//}
//对于actual argument或actual parameter使用术语argument(译为实参);
//对于formal argument或formal parameter使用术语parameter(译为形参)。
//example 5.16
//#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 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);// %lf表示读取一个double类型的值
// 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 average speed was %1.2f mph.\n", msec,rate);
// return 0;
//}
/*exercise
1.
30,108,1,9
2.
6,52,0,13
3.
37.5,1.5,35,37,37.5,1.4
4.
#include <stdio.h>
int main(void)
{
int i=1;
float n;
printf("Watch out!Here come a bunch of fractions!\n");
while (i++<30)
{
n=1.0/i;
printf("%f\n",n);
}
printf("That's all,folks!\n");
return 0;
}
5.
sec 无初始值;sec 最后的“0”值也会被打印
6.
#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;
}
结果:
%s! C is cool!
! C is cool! //悟到这一条的妙处了吗,写成这样会更明显:
//printf("%s! C is cool!\n",FORMAT);
11
11
12
11
7.
#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;
}
result:
SOS:4 4.00
8.
#include <stdio.h>
#define TEN 10
int main(void)
{
int n = 0;
while (n++ < TEN)
printf("%5d", n);
printf("\n");
return 0;
}
result:
1 2 3 4 5 6 7 8 9 10
9.
#include <stdio.h>
int main(void)
{
char n='a';
while (n <= 'g')
printf("%5c", n++);
printf("\n");
return 0;
}
10.
1 2;
101
102
103
104;
stuvw;
11.
COMPUTER BYTES DOG
COMPUTER BYTES DOG
COMPUTER BYTES DOG
COMPUTER BYTES DOG
COMPUTER BYTES DOG
COMPUTER BYTES DOG
COMPUTER BYTES DOG
COMPUTER BYTES DOG
COMPUTER BYTES DOG
...
12.
x=x+10;
x=x+1;
c=a+b;
c=(a+b)*2;
13.
x=x-1;
m=n%k;
p=q/b-a;
x=(a+b) / (c*d); */
//exercise 5.11
//1
//#include <stdio.h>
//#define M 60
//int main(void)
//{
// int m,th,tm;
// printf("Please input the minutes:(0 to exit)\n");
// scanf("%d",&m);
//
// while (m)
// {
// th=m/M;
// tm=m%M;
// printf("%d minutes are equal to %d hours and %d minutes.\n",m,th,tm);
// printf("Please input the minutes:(0 to exit)\n");
// scanf("%d",&m);
// }
// printf("Bye!");
// return 0;
//}
//2
//#include <stdio.h>
//int main(void)
//{
// int n,m;
// printf("Please input a integer.(0 to exit)\n");
// scanf("%d",&n);
// while (n)
// {
//
// m=n+10;
// while (n<=m)
// {
// printf("%d\n",n);
// n++;
// }
//
// printf("Please input a integer.(0 to exit)\n");
// scanf("%d",&n);
//
// }
// printf("Down!");
// return 0;
//}
3
//#include <stdio.h>
//#define W 60
//int main(void)
//{
// int d,tw,td;
// printf("Please input the days:(0 to exit)\n");
// scanf("%d",&d);
//
// while (d>0)
// {
// tw=d/7;
// td=d%7;
// printf("%d minutes are %d weeks,%d days.\n",d,tw,td);
// printf("Please input the days:(0 to exit)\n");
// scanf("%d",&d);
// }
// printf("Bye!");
// return 0;
//}
//4
//#include <stdio.h>
//int main(void)
//{
// float h;
// float inch;
// int ft;
// printf("Enter a height in centimeters:");
// scanf("%f",&h);
// while (h>0)
// {
// inch=h/2.54;
// ft=inch/12;
// inch=inch-12*ft;
// printf("%.1f cm = %d feet,%.1f inches.\n",h,ft,inch);
// printf("Enter a height in centimeters (<=0 to quit):");
// scanf("%f",&h);
//
// }
// printf("bye");
// return 0;
//}
//5
//#include <stdio.h>
//int main(void) /* 计算前20个整数的和 */
//{
//int count, sum; /* 声明[1] */
//int n;
//count = 0; /* 表达式语句 */
//sum = 0; /* 表达式语句 */
//printf("Please enter days.");
//scanf("%d",&n);
//while (n>0)
//{
//
//while (count++ < n) /* 迭代语句 */
//sum = sum + count;
//printf("sum = %d\n", sum); /* 表达式语句[2] */
//printf("Please enter days.");
//scanf("%d",&n);
//}
//return 0; /* 跳转语句 */
//}
//6
//#include <stdio.h>
//int main(void)
//{
//int count, sum; /* 声明[1] */
//int n;
//printf("Please enter days.");
//scanf("%d",&n);
//while (n>0)
//{
// count = 0; /* 表达式语句 */
// sum = 0; /* 表达式语句 */
//while (count++ < n) /* 迭代语句 */
//{
//sum = sum + count*count;
// /* 表达式语句[2] */
//}
// printf("sum = %d\n", sum);
// printf("Please enter days.");
// scanf("%d",&n);
//}
//return 0; /* 跳转语句 */
//}
//7
//#include <stdio.h>
//void cube(double x);
//int main(void)
//{
// double n;
// printf("Please input a number:\n");
// scanf("%lf",&n);
// while (n)
// {
// cube(n);
// printf("Please input a number:\n");
// scanf("%lf",&n);
// }
// printf("bye");
// return 0;
//}
//
//void cube(double x)
//{
// printf("The cube of %g is %g.\n", x, x * x * x);
//}
//8
//#include <stdio.h>
//int main(void)
//{
// int m,n;
// printf("This program computes moduli.\n");
// printf("Enter an integer to serve as the second operand:");
// scanf("%d",&n);
// printf("Now enter the first operand:");
// scanf("%d",&m);
// printf("%d %% %d is %d\n",m,n,m%n);
// while (m)
// {
// printf("Enter next number for first operand (<= 0 to quit):");
// scanf("%d",&m);
// printf("%d %% %d is %d\n",m,n,m%n);
//
// }
// printf("Done");
// return 0;
//}
//9 类构思 废弃
//#include <stdio.h>
//void Temperatures(double x)
//int main(void)
//{
// double Ft;
// printf("Please input the temperature in F:");
// scanf("%lf",Ft);
// while (Ft)
// {
// Temperatures(Ft);
// printf("Please input the temperature in F:");
// scanf("%lf",Ft);
//
// }
//
// return 0;
//}
//
//void Temperatures(double x)
//{
// const K_F 273.16;
//
//}
//9 正解。
//#include <stdio.h>
//
//void Temperatures(double fahrenheit);
//
//int main(void) {
// double input;
//
// // 提示用户输入一个华氏温度
// printf("Enter a number of fahrenheit temperature:");
// // 判断用户输入的数字是否为q或非数字
// while (scanf("%lf", &input) == 1) {
// // 打印温度转换结果
// Temperatures(input);
// // 提示用户再次输入
// printf("Enter next fahrenheit temperature value (q or non-numeric to quit):");
// }
// printf("Done\n");
// return 0;
//}
//
//void Temperatures(double fahrenheit) {
// // 摄氏温度转换开氏温度的变量
// const double celsius_to_kelvin = 273.16;
// // 华氏温度转换摄氏温度的变量
// const double fahrenheit_to_celsius = 32.0;
//
// double celsius, kelvin;
// // 计算华氏温度转换成摄氏温度
// celsius = 5.0 / 9.0 * (fahrenheit - fahrenheit_to_celsius);
// // 计算开氏温度转换成摄氏温度
// kelvin = celsius + celsius_to_kelvin;
//
// // 打印3个温度值
// printf("%.2f. fahrenheit = %.2f celsius, and %.2f kelvin\n", fahrenheit, celsius, kelvin);
//}