1 shoes1.c
/* shoes1.c -- 把鞋码转换成英寸 */
#include <stdio.h>
#define ADJUST 7.31 // 字符常量
int main(void)
{
const double SCALE = 0.333; //const变量
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;
}
2 shoes2.c
/* shoes2.c --计算多个不一样鞋码对应的脚长 */
#include <stdio.h>
#define ADJUST 7.31 // 字符常量
int main(void)
{
const double SCALE = 0.333; // const变量
double shoe, foot;
printf("Shoe size (men's) foot length\n");
shoe = 3.0;
while (shoe < 18.5) /* while循环开始 */
{ /* 块开始*/
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;
}
3 golf.c
/* golf.c -- golf锦标赛计分卡 */
#include <stdio.h>
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;
}
4 squares.c
/* squares.c -- 计算1-20的平方 */
#include <stdio.h>
int main(void)
{
int num = 1;
while (num < 21)
{
printf("%4d %6d\n", num, num * num);
num = num + 1;
}
return 0;
}
5 wheat.c
/* wheat.c -- 指数增加 */
#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; /*从第一粒谷开始 */
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;
}
6 divide.c
/* 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("floating division: 7./4. is %1.2f \n", 7./4.);
printf("mixed division: 7./4 is %1.2f \n", 7./4);
return 0;
}
7 rules.c
/* rules.c -- 优先级测试 */
#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;
}
8 sizeof.c
// sizeof.c -- 使用sizeof运算符
// 使用 C99 %z 转换 -- 若是编译器不支持 %zd将其改为%u or %lu
#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;
}
9 min_sec.c
// min_sec.c -- 把秒数转换成分和秒
#include <stdio.h>
#define SEC_PER_MIN 60 // 1分钟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);
printf("Enter next value (<=0 to quit):\n");
scanf("%d", &sec);
}
printf("Done!\n");
return 0;
}
10 add_one.c
/* add_one.c -- 递增:前缀和后缀 */
#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;
}
11 post_pre.c
/* post_pre.c -- 前缀和后缀 */
#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("%1d %5d %5d %5d\n", a, a_post, b, pre_b);
return 0;
}
12 bottles.c
#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;
}
13 addemup.c
/* addemup.c -- 几种常见的语句 */
#include <stdio.h>
int main(void) /* 计算前20个整数的和*/
{
int count, sum; /* 声明 */
count = 0; /* 表达式语句 */
sum = 0; /* 表达式语句 */
while (count++ < 20) /* 迭代语句 */
sum = sum + count; /* */
printf("sum = %d\n", sum);/* 表达式语句*/
return 0; /* 跳转语句*/
}
14 convert.c
/* convert.c -- 自动类型转换 */
#include <stdio.h>
int main(void)
{
char ch;
int i;
float fl;
fl = i = ch = 'C'; /* line 9 */
printf("ch = %c, i = %d, fl = %2.2f\n", ch, i, fl); /* line 10 */
ch = ch + 1; /* line 11 */
i = fl + 2 * ch; /* line 12 */
fl = 2.0 * ch + i; /* line 13 */
printf("ch = %c, i = %d, fl = %2.2f\n", ch, i, fl); /* line 14 */
ch = 1107; /* line 15 */
printf("Now ch = %c\n", ch); /* line 16 */
ch = 80.89; /* line 17 */
printf("Now ch = %c\n", ch); /* line 18 */
return 0;
}
15 pound.c
/* pound.c -- 定义一个带一个参数的函数 */
#include <stdio.h>
void pound(int n); // ANSI 函数原型声明
int main(void)
{
int times = 5;
char ch = '!'; // ASCII 码是 33
float f = 6.0f;
pound(times); // int 类型参数
pound(ch); // 和 pound((int)ch);相同
pound(f); //和 pound((int)f);相同
return 0;
}
void pound(int n) // ANSI-风格函数头
{ // 代表该函数接受一个int类型的参数
while (n-- > 0)
printf("#");
printf("\n");
}
16 running.c
// running.c -- A useful program for runners
#include <stdio.h>
const int S_PER_M = 60; // 1分钟的秒数
const int S_PER_H = 3600; // 1小时的秒数
const double M_PER_K = 0.62137; //1千米的英里数
int main(void)
{
double distk, distm; // 跑过的距离
double rate; // 平均速度
int min, sec; //跑步用时(以分钟和秒为单位)
int time; // 跑步用时(以秒为单位)
double mtime; // 跑1英里须要的时间(以秒为单位)
int mmin, msec; // 跑1英里须要的时间(以分钟和秒为单位)
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 for type 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;
// miles per sec x sec per hour = mph
rate = distm / time * S_PER_H;
// time/distance = time per mile
mtime = (double) time / distm;
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;
}