-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch01.c
192 lines (169 loc) · 4.32 KB
/
ch01.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* Chapter 1: A Tutorial Introduction
*/
#include<stdio.h>
void hello_world()
{
// the standard library ensures that an input text stream appears as a sequence of lines
printf("hello world!\n");
printf("%6.2f\n", -3.33333333); // 长度包括小数点也包括负号
}
// instead of magic number, use macro to make it more readable
#define LOW 0
#define HIGH 300
#define STEP 20
void degree_convert()
{
float c, f;
f = LOW;
while (f < HIGH) {
c = (5.0/9.0)*(f-32);
printf("%6.1f => %3.1f\n", f, c);
f += STEP;
}
}
void for_degree_convert()
{
float f;
for (f = LOW; f < HIGH; f += STEP) {
printf("%6.1f => %3.1f\n", f, (5.0/9.0)*(f-32));
}
}
void file_copy()
{
int c; // must be int to cover EOF
while ((c = getchar()) != EOF) {
putchar(c);
}
// putchar(c);
}
void char_counter()
{
// 'A' is to be preferred over 65: its meaning is obvious, and it is independent of a particular character set.
// '\n' != "\n"
long c; // for bigger number, use double
for (c = 0; getchar() != EOF; c++);
printf("%ld characters counted!\n", c); // ld for long integer
}
void line_counter()
{
int c;
long counter = 0;
while ((c = getchar()) != EOF) {
if (c == '\n') {
counter++;
}
}
printf("%ld lines counted!\n", counter);
}
#define IN 0
#define OUT 1
void word_counter()
{
int c, nl, nw, nc, state;
nl = nw = nc = 0;
state = OUT;
while ((c = getchar()) != EOF) {
nc++;
if (c == '\n') {
nl++;
}
if (c == ' ' || c == '\n' || c == '\t') {
state = OUT;
} else {
if (state == OUT) {
state = IN;
nw++;
}
}
}
}
void digit_counter()
{
int c, i, nwhite, nother;
int digits[10];
nwhite = nother = 0;
for (i = 0; i < 10; i++) {
digits[i] = 0;
}
while ((c = getchar()) != EOF) {
if (c >= '0' && c <= '9') {
digits[c-'0']++;
} else if (c == ' ' || c == '\t' || c == '\n') {
nwhite++;
} else {
nother++;
}
}
printf("Digits = ");
for (i = 0; i < 10; i++) {
printf(" %d", digits[i]);
}
printf("; White = %d; Other = %d\n", nwhite, nother);
}
int power(int base, int n)
{
// in old-style function, the params and return value are assumed to be int
// all args are passed by value; call by ref could be made by passsing address (including array name)
int i;
int result = 1;
for (i = 0; i < n; i++) {
result *= base;
}
return result;
}
#define MAXLEN 1000
// But for compatibility with older C programs the standard takes an empty list as an old-style declaration, and turns off all argument list checking; the word void must be used for an explicitly empty list.
int mygetline(char s[], int lim);
void copy(char from[], char to[]);
// An external variable must be defined, exactly once, outside of any function; this sets aside storage for it.
// The variable must also be declared in each function that wants to access it; this states the type of the variable. The declaration may be an explicit extern statement or may be implicit from context.
// The usual practice is to collect extern declarations of variables and functions in a separate file, historically called a header.
void longest_line()
{
int len;
char line[MAXLEN];
int max = 0;
char longest[MAXLEN];
while (len = mygetline(line, MAXLEN)) {
if (len > max) {
max = len;
copy(line, longest);
}
}
if (max > 0) {
printf("Longest: %s", longest);
}
}
// Every text line has at least one character; even a line containing only a newline has length 1.
int mygetline(char s[], int lim)
{
int c, i;
for (i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; i++) {
s[i] = c;
}
if (c == '\n') {
s[i] = c;
i++;
}
s[i] = '\0';
return i;
}
void copy(char from[], char to[])
{
int i = 0;
while ((to[i] = from[i]) != '\0')
i++;
}
int main()
{
hello_world();
// degree_convert();
// for_degree_convert();
// file_copy();
// char_counter();
// line_counter();
// digit_counter();
// printf("%d\n", power(2, 3));
longest_line();
return 0;
}