-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch03.c
236 lines (209 loc) · 4.93 KB
/
ch03.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include<stdio.h>
#include<ctype.h>
#include<string.h>
// 字符串反转
void reverse(char s[])
{
int i, j, len = strlen(s);
int temp;
// A pair of expressions separated by a comma is evaluated left to right,
// and the type and value of the result are the type and value of the right operand.
// not for function arguments, variables in declarations
for(i = 0, j = len-1; i < j; i++, j--) {
if(s[i] != s[j]) {
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
// 去掉字符串尾部的空白
// You should verify that this is correct behavior even when the string is empty or contains only white space characters.
int trim(char s[])
{
int len = strlen(s);
int i;
for(i = len-1; i >= 0; i--) {
if(s[i] != ' ' && s[i] != '\t' && s[i] != '\n')
break;
}
s[i+1] = '\0';
return i;
}
// 数字转为字符串
void itoa(int n, char s[])
{
int sign = (n<0) ? -1 : 1;
int i = 0;
do {
s[i++] = n%10 + '0';
} while((n = n/10) > 0);
if (sign < 0) {
s[i++] = '-';
}
s[i] = '\0';
reverse(s);
}
// Exercise 3-3: Expand chars like a-b
// Exercise 3-2: 打印转义字符
void escape(char s[], char t[])
{
int len = strlen(t);
int i, j = 0;
for(i = 0, j = 0; i < len; i++, j++) {
switch(t[i]) {
case '\t':
s[j++] = '\\';
s[j] = 't';
break;
case '\n':
s[j++] = '\\';
s[j] = 'n';
break;
default:
s[j] = t[i];
break;
}
}
s[j] = '\0';
}
// 字符串转为数字
int atoi(char s[])
{
int i;
int len = strlen(s);
for(i = 0; i < len && isspace(s[i]); i++)
;
if(i == len)
return 0;
int sign = s[i] == '-' ? -1 : 1;
if(s[i] == '+' || s[i] == '-') {
i++;
}
int num = 0;
while(i < len) {
if(isdigit(s[i])) {
num = num*10 + s[i]-'0';
i++;
} else {
break;
}
}
return num;
}
// 二分查找(三个测试分支)
int binary_search(int* arr, int n, int v)
{
int low = 0;
int high = n-1;
int mid;
while (low <= high) {
mid = (low+high)/2;
printf("Low: %d, High: %d, Mid: %d\n", low, high, mid);
if(arr[mid] < v) {
low = mid + 1;
} else if(arr[mid] > v) {
high = mid - 1;
} else {
return mid;
}
}
return -1;
}
// Exercise 3-1: 二分查找(两个测试分支)
int binary_search_with_one_test(int* arr, int n, int v)
{
int low = 0;
int high = n-1;
int mid;
while (low < high) {
mid = (low+high)/2;
printf("Low: %d, High: %d, Mid: %d\n", low, high, mid);
if (arr[mid] < v) {
low = mid + 1;
} else {
high = mid;
}
}
if (arr[mid] == v) {
return mid;
} else {
return -1;
}
}
// 统计输入的数字个数
void count_digits()
{
int i;
char c;
int digits[10];
for (i = 0; i < 10; i++) {
digits[i] = 0;
}
int white = 0, others = 0;
while ((c = getchar()) != EOF) {
// printf("Char: %c\n", c);
// Switch Case: Integer value constant or constant expr
switch (c) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
digits[c-'0']++;
break;
case ' ':
case '\t':
case '\n':
white++;
break;
default:
others++;
break;
}
}
for (i = 0; i < 10; i++) {
printf("Digit %d: %d\n", i, digits[i]);
}
printf("Whitespace: %d\n", white);
printf("Non-white: %d\n", others);
}
int main(int argc, char* argv[])
{
// Notice: dangling else
char s[15] = "abcdefg ";
printf("After Trim: %sEND\n", s);
trim(s);
printf("After Trim: %sEND\n", s);
int i = 12345;
itoa(i, s);
printf("ITOA: %sEND\n", s);
char t[] = "abcdefg\tabcdefg";
printf("ESCAPE: %sEND\n", t);
char s0[20];
escape(s0, t);
printf("ESCAPE: %sEND\n", s0);
char s1[] = "12345";
printf("ATOI: %d\n", atoi(s1));
// count_digits();
/*
const int len = 10;
int arr[10] = {1, 3, 5, 7, 9, 10, 12, 14, 16, 20};
int idx;
printf("Binary Search...\n");
idx = binary_search(arr, len, 7);
printf("Index: %d\n", idx);
idx = binary_search(arr, len, 8);
printf("Index: %d\n", idx);
printf("Binary Search (One Test)...\n");
idx = binary_search_with_one_test(arr, len, 7);
printf("Index: %d\n", idx);
idx = binary_search_with_one_test(arr, len, 8);
printf("Index: %d\n", idx);
*/
}