11589
备注:
|
15177
|
删除的内容标记成这样。 | 加入的内容标记成这样。 |
行号 17: | 行号 17: |
使用公式℃=(5/9)(℉-32) 打印下列华氏温度与摄氏温度对照表 | 使用公式℃=(5/9)(℉-32) 打印华氏温度与摄氏温度对照表 |
行号 214: | 行号 214: |
== 数组 == | == 数字字符计数 == |
行号 520: | 行号 520: |
= 控制流 = == 折半查找 == {{{#!cplusplus /* binsearch: find x in v[0] <= v[1] <= ... <= v[n-1] */ int binsearch(int x, int v[], int n) { int low, high, mid; low = 0; high = n - 1; while (low <= high) { mid = (low+high)/2; if (x < v[mid]) high = mid + 1; else if (x > v[mid]) low = mid + 1; else /* found match */ return mid; } return -1; /* no match */ } }}} == 数字字符计数第2版 == {{{#!cplusplus #include <stdio.h> main() /* count digits, white space, others */ { int c, i, nwhite, nother, ndigit[10]; nwhite = nother = 0; for (i = 0; i < 10; i++) ndigit[i] = 0; while ((c = getchar()) != EOF) { switch (c) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': ndigit[c-'0']++; break; case ' ': case '\n': case '\t': nwhite++; break; default: nother++; break; } } printf("digits ="); for (i = 0; i < 10; i++) printf(" %d", ndigit[i]); printf(", white space = %d, other = %d\n", nwhite, nother); return 0; } }}} == 字符串转数值第2版 == {{{#!cplusplus #include <ctype.h> /* atoi: convert s to integer; version 2 */ int atoi(char s[]) { int i, n, sign; for (i = 0; isspace(s[i]); i++) /* skip white space */ ; sign = (s[i] == '-') ? -1 : 1; if (s[i] == '+' || s[i] == '-') /* skip sign */ i++; for (n = 0; isdigit(s[i]); i++) n = 10 * n + (s[i] - '0'); return sign * n; } }}} == 希尔排序 == {{{#!cplusplus /* shellsort: sort v[0]...v[n-1] into increasing order */ void shellsort(int v[], int n) { int gap, i, j, temp; for (gap = n/2; gap > 0; gap /= 2) for (i = gap; i < n; i++) for (j=i-gap; j>=0 && v[j]>v[j+gap]; j-=gap) { temp = v[j]; v[j] = v[j+gap]; v[j+gap] = temp; } } }}} == 字符串翻转 == {{{#!cplusplus #include <string.h> /* reverse: reverse string s in place */ void reverse(char s[]) { int c, i, j; for (i = 0, j = strlen(s)-1; i < j; i++, j--) { c = s[i]; s[i] = s[j]; s[j] = c; } } }}} == 整数转字符串 == {{{#!cplusplus /* itoa: convert n to characters in s */ void itoa(int n, char s[]) { int i, sign; if ((sign = n) < 0) /* record sign */ n = -n; /* make n positive */ i = 0; do { /* generate digits in reverse order */ s[i++] = n % 10 + '0'; /* get next digit */ } while ((n /= 10) > 0); /* delete it */ if (sign < 0) s[i++] = '-'; s[i] = '\0'; reverse(s); } }}} == 字符串裁剪 == {{{#!cplusplus /* trim: remove trailing blanks, tabs, newlines */ int trim(char s[]) { int n; for (n = strlen(s)-1; n >= 0; n--) if (s[n] != ' ' && s[n] != '\t' && s[n] != '\n') break; s[n+1] = '\0'; return n; } }}} |
1. 入门
1.1. 第一个C语言程序
写一个程序,在屏幕上输出hello, world
1.2. 摄氏华氏温度对照表
使用公式℃=(5/9)(℉-32) 打印华氏温度与摄氏温度对照表
1 #include <stdio.h>
2
3 /* print Fahrenheit-Celsius table
4 for fahr = 0, 20, ..., 300 */
5 main()
6 {
7 int fahr, celsius;
8 int lower, upper, step;
9
10 lower = 0; /* lower limit of temperature scale */
11 upper = 300; /* upper limit */
12 step = 20; /* step size */
13
14 fahr = lower;
15 while (fahr <= upper) {
16 celsius = 5 * (fahr-32) / 9;
17 printf("%d\t%d\n", fahr, celsius);
18 fahr = fahr + step;
19 }
20 }
1.3. 温度对照表第2版
上述的温度转换程序存在两个问题。比较简单的问题是,由于输出的数不是右对齐的,所以输出的结果不是很美观。另一个较为严重的问题是,由于我们使用的是整型算术运算,因此经计算得到的摄氏温度值不太精确,例如,与0℉对应的精确的摄氏温度应该为-17.8℃,而不是-17℃。
1 #include <stdio.h>
2
3 /* print Fahrenheit-Celsius table
4 for fahr = 0, 20, ..., 300; floating-point version */
5 main()
6 {
7 float fahr, celsius;
8 float lower, upper, step;
9
10 lower = 0; /* lower limit of temperatuire scale */
11 upper = 300; /* upper limit */
12 step = 20; /* step size */
13
14 fahr = lower;
15 while (fahr <= upper) {
16 celsius = (5.0/9.0) * (fahr-32.0);
17 printf("%3.0f %6.1f\n", fahr, celsius);
18 fahr = fahr + step;
19 }
20 }
1.4. 温度对照表第3版
对于某个特定任务我们可以采用多种方法来编写程序。
1.5. 温度对照表第4版
1 #include <stdio.h>
2
3 #define LOWER 0 /* lower limit of table */
4 #define UPPER 300 /* upper limit */
5 #define STEP 20 /* step size */
6
7 /* print Fahrenheit-Celsius table */
8 main()
9 {
10 int fahr;
11
12 for (fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP)
13 printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
14 }
1.6. 文件复制
把输入一次一个字符地复制到输出
1.7. 文件复制第2版
对于经验比较丰富的C语言程序员,可以把这个字符复制程序编写得更精炼一些。
1.8. 字符计数
下列程序用于对字符进行计数,它与上面的复制程序类似。
1.9. 字符计数第2版
1.10. 行计数
接下来的这个程序用于统计输入中的行数。
1.11. 单词计数
写一个程序用于统计行数、单词数与字符数
1 #include <stdio.h>
2
3 #define IN 1 /* inside a word */
4 #define OUT 0 /* outside a word */
5
6 /* count lines, words, and characters in input */
7 main()
8 {
9 int c, nl, nw, nc, state;
10
11 state = OUT;
12 nl = nw = nc = 0;
13 while ((c = getchar()) != EOF) {
14 ++nc;
15 if (c == '\n')
16 ++nl;
17 if (c == ' ' || c == '\n' || c = '\t')
18 state = OUT;
19 else if (state == OUT) {
20 state = IN;
21 ++nw;
22 }
23 }
24 printf("%d %d %d\n", nl, nw, nc);
25 }
1.12. 数字字符计数
编写一个程序,以统计各个数字、空白符(包括空格符、制表符及换行符)以及所有其他字符出现的次数
1 #include <stdio.h>
2
3 /* count digits, white space, others */
4 main()
5 {
6 int c, i, nwhite, nother;
7 int ndigit[10];
8
9 nwhite = nother = 0;
10 for (i = 0; i < 10; ++i)
11 ndigit[i] = 0;
12
13 while ((c = getchar()) != EOF)
14 if (c >= '0' && c <= '9')
15 ++ndigit[c-'0'];
16 else if (c == ' ' || c == '\n' || c == '\t')
17 ++nwhite;
18 else
19 ++nother;
20
21 printf("digits =");
22 for (i = 0; i < 10; ++i)
23 printf(" %d", ndigit[i]);
24 printf(", white space = %d, other = %d\n",
25 nwhite, nother);
26 }
1.13. 函数
写一个求幂的函数power(m,n)
1 #include <stdio.h>
2
3 int power(int m, int n);
4
5 /* test power function */
6 main()
7 {
8 int i;
9
10 for (i = 0; i < 10; ++i)
11 printf("%d %d %d\n", i, power(2,i), power(-3,i));
12 return 0;
13 }
14
15 /* power: raise base to n-th power; n >= 0 */
16 int power(int base, int n)
17 {
18 int i, p;
19
20 p = 1;
21 for (i = 1; i <= n; ++i)
22 p = p * base;
23 return p;
24 }
1.14. power函数第2版
1.15. 最长的行
写一个程序读入一组文本行,并把最长的文本行打印出来。
1 #include <stdio.h>
2 #define MAXLINE 1000 /* maximum input line length */
3
4 int getline(char line[], int maxline);
5 void copy(char to[], char from[]);
6
7 /* print the longest input line */
8 main()
9 {
10 int len; /* current line length */
11 int max; /* maximum length seen so far */
12 char line[MAXLINE]; /* current input line */
13 char longest[MAXLINE]; /* longest line saved here */
14
15 max = 0;
16 while ((len = getline(line, MAXLINE)) > 0)
17 if (len > max) {
18 max = len;
19 copy(longest, line);
20 }
21 if (max > 0) /* there was a line */
22 printf("%s", longest);
23 return 0;
24 }
25
26 /* getline: read a line into s, return length */
27 int getline(char s[],int lim)
28 {
29 int c, i;
30
31 for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
32 s[i] = c;
33 if (c == '\n') {
34 s[i] = c;
35 ++i;
36 }
37 s[i] = '\0';
38 return i;
39 }
40
41 /* copy: copy 'from' into 'to'; assume to is big enough */
42 void copy(char to[], char from[])
43 {
44 int i;
45
46 i = 0;
47 while ((to[i] = from[i]) != '\0')
48 ++i;
49 }
1.16. 最长的行第2版
使用全局变量来实现
1 #include <stdio.h>
2
3 #define MAXLINE 1000 /* maximum input line size */
4
5 int max; /* maximum length seen so far */
6 char line[MAXLINE]; /* current input line */
7 char longest[MAXLINE]; /* longest line saved here */
8
9 int getline(void);
10 void copy(void);
11
12 /* print longest input line; specialized version */
13 main()
14 {
15 int len;
16 extern int max;
17 extern char longest[];
18
19 max = 0;
20 while ((len = getline()) > 0)
21 if (len > max) {
22 max = len;
23 copy();
24 }
25 if (max > 0) /* there was a line */
26 printf("%s", longest);
27 return 0;
28 }
29
30 /* getline: specialized version */
31 int getline(void)
32 {
33 int c, i;
34 extern char line[];
35
36 for (i = 0; i < MAXLINE - 1
37 && (c=getchar)) != EOF && c != '\n'; ++i)
38 line[i] = c;
39 if (c == '\n') {
40 line[i] = c;
41 ++i;
42 }
43 line[i] = '\0';
44 return i;
45 }
46
47 /* copy: specialized version */
48 void copy(void)
49 {
50 int i;
51 extern char line[], longest[];
52
53 i = 0;
54 while ((longest[i] = line[i]) != '\0')
55 ++i;
56 }
2. 类型、运算符和表达式
2.1. 字符串长度
写一个函数求字符串的长度
2.2. 字符串转整数
写一个函数将包含一串数字的字符串转换成整数
2.3. 大写变小写
写一个函数,将大写字母变成小写字母,其他字符不变。
2.4. 随机函数
写一个函数,用来产生随机的整数
2.5. 字符串删除
写一个函数squeeze(s, c),删除字符串s中所有的字符c。
2.6. 字符串连接
写一个函数strcat(s,t),它将字符串t连接到字符串s的尾部
2.7. 位运算
写一个函数getbits(x,p,n),它返回x中从右边数第p位开始向右数n位的字段
2.8. 位计数
函数bitcount统计其整型参数的值为1的二进制位的个数
3. 控制流
3.1. 折半查找
1 /* binsearch: find x in v[0] <= v[1] <= ... <= v[n-1] */
2 int binsearch(int x, int v[], int n)
3 {
4 int low, high, mid;
5
6 low = 0;
7 high = n - 1;
8 while (low <= high) {
9 mid = (low+high)/2;
10 if (x < v[mid])
11 high = mid + 1;
12 else if (x > v[mid])
13 low = mid + 1;
14 else /* found match */
15 return mid;
16 }
17 return -1; /* no match */
18 }
3.2. 数字字符计数第2版
1 #include <stdio.h>
2
3 main() /* count digits, white space, others */
4 {
5 int c, i, nwhite, nother, ndigit[10];
6
7 nwhite = nother = 0;
8 for (i = 0; i < 10; i++)
9 ndigit[i] = 0;
10 while ((c = getchar()) != EOF) {
11 switch (c) {
12 case '0': case '1': case '2': case '3': case '4':
13 case '5': case '6': case '7': case '8': case '9':
14 ndigit[c-'0']++;
15 break;
16 case ' ':
17 case '\n':
18 case '\t':
19 nwhite++;
20 break;
21 default:
22 nother++;
23 break;
24 }
25 }
26 printf("digits =");
27 for (i = 0; i < 10; i++)
28 printf(" %d", ndigit[i]);
29 printf(", white space = %d, other = %d\n",
30 nwhite, nother);
31 return 0;
32 }
3.3. 字符串转数值第2版
1 #include <ctype.h>
2
3 /* atoi: convert s to integer; version 2 */
4 int atoi(char s[])
5 {
6 int i, n, sign;
7
8 for (i = 0; isspace(s[i]); i++) /* skip white space */
9 ;
10 sign = (s[i] == '-') ? -1 : 1;
11 if (s[i] == '+' || s[i] == '-') /* skip sign */
12 i++;
13 for (n = 0; isdigit(s[i]); i++)
14 n = 10 * n + (s[i] - '0');
15 return sign * n;
16 }
3.4. 希尔排序
1 /* shellsort: sort v[0]...v[n-1] into increasing order */
2 void shellsort(int v[], int n)
3 {
4 int gap, i, j, temp;
5
6 for (gap = n/2; gap > 0; gap /= 2)
7 for (i = gap; i < n; i++)
8 for (j=i-gap; j>=0 && v[j]>v[j+gap]; j-=gap) {
9 temp = v[j];
10 v[j] = v[j+gap];
11 v[j+gap] = temp;
12 }
13 }
3.5. 字符串翻转
3.6. 整数转字符串
1 /* itoa: convert n to characters in s */
2 void itoa(int n, char s[])
3 {
4 int i, sign;
5
6 if ((sign = n) < 0) /* record sign */
7 n = -n; /* make n positive */
8 i = 0;
9 do { /* generate digits in reverse order */
10 s[i++] = n % 10 + '0'; /* get next digit */
11 } while ((n /= 10) > 0); /* delete it */
12 if (sign < 0)
13 s[i++] = '-';
14 s[i] = '\0';
15 reverse(s);
16 }