-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter1.c
More file actions
369 lines (294 loc) · 5.2 KB
/
chapter1.c
File metadata and controls
369 lines (294 loc) · 5.2 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include "./cslib/genlib.h"
#include "./cslib/simpio.h"
// Question 1
void CelsiusToFahrenheit(void)
{
double C = GetReal();
double F = 9 / 5 * C + 32;
printf("Fahrenheit = %lf\n", F);
return;
}
// Question 2
void MeterToInchesAndFeet(void)
{
#define FEET 0.0254
double meter = GetReal();
double feet = meter / FEET;
double inches = feet / 12;
double remain = inches - (int)inches;
double remainfeet = remain * 12;
printf("%lf meter = %d inches, %lf feet",
meter, (int)inches, remainfeet);
return;
}
// Question 3
void GaussSum(void)
{
int n = 100;
int ret = (1 + n) * n / 2;
printf("ret = %d\n", ret);
return;
}
// Question 4
void FrontNOddSum(void)
{
int N = GetInteger();
int ret = (1 + (2 * (N-1) + 1)) * N / 2;
printf("ret = %d\n", ret);
}
// Question 5
void FindLargestValue(void)
{
int Monitor = 1;
int Max = 0;
// int temp = 0;
printf("This program finds the largest integer in a list.\n");
printf("Enter 0 to signal the end of the list.\n");
while (Monitor != 0)
{
Monitor = GetInteger();
if (Monitor > Max)
{
Max = Monitor;
}
}
printf("The largest value is %d\n", Max);
}
// Question 6:模仿1.5.5小节 “while” 中介绍的DigitSum函数编写一个程序,读入一个整数,显示由该整数,显示该证书数字反向的整数。
void ReverseDigit(void)
{
printf("This program reverse the digits in an integer.\n");
printf("Enter a positive integer: ");
long integer = GetLong();
printf("The reverse number is : ");
while (integer > 0)
{
int i = integer % 10;
printf("%d", i);
integer /= 10;
}
printf("\n");
}
// Question 7
bool isPerfect(int n)
{
int monitor = 0;
for (int i = 1; i <= n/2; i++)
{
if (n % i == 0)
{
monitor += i;
}
}
// Even perfect = 2^(n-1) * (2^n - 1) n = 1, 2, 3...
// We haven't know if Odd perfect exist
return (monitor == n);
}
// Question 8
void PrimeFactored(unsigned int n)
{
printf("Enter number to be factored: ");
int n = GetInteger();
int i = 2;
while (i < n)
{
if (n % i == 0)
{
printf("%d * ", i);
n = n / i;
}
else
{
i = i + 1;
}
}
printf("%d\n", n);
}
// Question 9
int Round(double x)
{
int ret = 0;
if (x >= 0)
{
double n = x - (int)x;
if (n >= 0.5)
{
ret = (int)x + 1;
}
else
{
ret = (int)x;
}
}
else
{
double n = x - (int)x;
if (n <= -0.5)
{
ret = (int)x - 1;
}
else
{
ret = (int)x;
}
}
return ret;
}
// Question 10
double ApproxiamatePi(int N)
{
double pi = 1.0;
int n = 3;
for (int i = 1; i <= N; i++)
{
pi = pi - (1.0 / n) + 1.0 / (n+2);
n = n + 4;
}
pi = pi * 4;
return pi;
}
// Question 11
// R = 2 fixed
double ApproxiamateArea(int N)
{
#define R 2
double w = (double)R / N;
double Area = 0;
for (int i = 0; i < N; i++)
{
double x = w * i;
double h = sqrt((double)R * R - (x + w / 2) * (x + w / 2));
Area = Area + h * w;
}
return Area;
}
// Question 12
void printTens(int x)
{
switch (x)
{
case 2: printf("twenty"); return;
case 3: printf("thirty"); return;
case 4: printf("fourty"); return;
case 5: printf("fifty"); return;
case 6: printf("sixty"); return;
case 7: printf("seventy"); return;
case 8: printf("eighty"); return;
case 9: printf("ninety"); return;
}
}
void printOneDigit(int x)
{
switch (x)
{
case 1: printf("one"); return;
case 2: printf("two"); return;
case 3: printf("three"); return;
case 4: printf("four"); return;
case 5: printf("five"); return;
case 6: printf("six"); return;
case 7: printf("seven"); return;
case 8: printf("eight"); return;
case 9: printf("nine"); return;
}
}
int hundreds(int x)
{
return x / 100;
}
int tens(int x)
{
return (x / 10) % 10;
}
int ones(int x)
{
return x % 10;
}
int thousands(int x)
{
return x / 1000;
}
int lastThree(int x)
{
return x % 1000;
}
int lastTwo(int x)
{
return x % 100;
}
void printNumber(int x)
{
if (x >= 0 && x < 10)
{
printOneDigit(x);
return;
}
if (hundreds(x) > 0)
{
printOneDigit(hundreds(x));
printf(" hundred ");
}
if ((lastTwo(x) >= 10 && lastTwo(x) <= 13) || lastTwo(x) == 15 || lastTwo(x) == 18)
{
switch (lastTwo(x))
{
case 10: printf("ten"); return;
case 11: printf("eleven"); return;
case 12: printf("twelve"); return;
case 13: printf("thirteen"); return;
case 15: printf("fifteen"); return;
case 18: printf("eighteen"); return;
}
}
if (lastTwo(x) == 14 || lastTwo(x) == 16 || lastTwo(x) == 17 || lastTwo(x) == 19)
{
//Yikes, I know. But this is the reusable functions question.
printOneDigit(x % 10);
printf("teen");
return;
}
if (tens(x) > 1)
{
printTens(tens(x));
if (ones(x) > 0)
{
printf("-");
printOneDigit(ones(x));
}
return;
}
printOneDigit(ones(x));
return;
}
void printTotal(int x) {
if (x > 999999)
{
printf("That's too large.");
return;
}
if (x == 0)
{
printf("zero");
return;
}
if (thousands(x) > 0)
{
printNumber(thousands(x));
printf(" thousand ");
}
printNumber(lastThree(x));
return;
}
int main(void)
{
printf("Enter numbers in their decimal form.\n");
printf("To stop, enter a negative value.");
int input;
do
{
printf("\nnumber: ");
input = GetInteger();
printTotal(input);
} while (input >= 0);
return 0;
}