Skip to content

Commit 4989b0d

Browse files
committed
5.2
1 parent eb3c2b8 commit 4989b0d

File tree

5 files changed

+215
-0
lines changed

5 files changed

+215
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
int main()
2+
{
3+
int x = 1, y = 2, z[10];
4+
int *ip;
5+
6+
ip = &x;
7+
y = *ip;
8+
*ip = 0;
9+
ip = &z[0];
10+
11+
return 0;
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
int main()
2+
{
3+
}
4+
5+
void swap(int *px, int *py)
6+
{
7+
int temp;
8+
9+
temp = *px;
10+
*px = *py;
11+
*py = temp;
12+
}
13+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <stdio.h>
2+
#include <ctype.h>
3+
4+
#define SIZE 10
5+
6+
int getch(void);
7+
void ungetch(int);
8+
9+
// compile with getch.c
10+
int main()
11+
{
12+
int n, array[SIZE], getint(int *);
13+
14+
for (n = 0; n < SIZE; n++)
15+
array[n] = 0;
16+
17+
for (n = 0; n < SIZE && getint(&array[n]) != EOF; n++)
18+
;
19+
20+
for (n = 0; n < SIZE; n++)
21+
printf("%d ", array[n]);
22+
23+
printf("\n");
24+
return 0;
25+
}
26+
27+
int getint(int *pn)
28+
{
29+
int c, sign;
30+
31+
while (isspace(c = getch()))
32+
;
33+
34+
if (!isdigit(c) && c != EOF && c != '+' && c != '-') {
35+
ungetch(c);
36+
return 0;
37+
}
38+
sign = (c == '-') ? -1 : 1;
39+
if (c == '+' || c == '-') {
40+
c = getch();
41+
if (!isdigit(c)) {
42+
ungetch(sign == 1 ? '+' : '-');
43+
return 0;
44+
}
45+
}
46+
for (*pn = 0; isdigit(c); c = getch())
47+
*pn = 10 * *pn + (c - '0');
48+
*pn *= sign;
49+
if (c != EOF)
50+
ungetch(c);
51+
return c;
52+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#include <stdio.h>
2+
#include <ctype.h>
3+
4+
#define SIZE 10
5+
6+
int getch(void);
7+
void ungetch(int);
8+
9+
// compile with getch.c
10+
int main()
11+
{
12+
int n;
13+
double array[SIZE], getfloat(double *);
14+
15+
for (n = 0; n < SIZE; n++)
16+
array[n] = 0.0;
17+
18+
for (n = 0; n < SIZE && getfloat(&array[n]) != EOF; n++)
19+
;
20+
21+
for (n = 0; n < SIZE; n++)
22+
printf("%g ", array[n]);
23+
24+
printf("\n");
25+
return 0;
26+
}
27+
28+
double getfloat(double *pn)
29+
{
30+
int c, sign;
31+
double power;
32+
33+
while (isspace(c = getch()))
34+
;
35+
36+
if (!isdigit(c) && c != EOF && c != '+' && c != '-' && c != '.') {
37+
ungetch(c);
38+
return 0.0;
39+
}
40+
sign = (c == '-') ? -1 : 1;
41+
if (c == '+' || c == '-') {
42+
c = getch();
43+
if (!isdigit(c) && c != '.') {
44+
ungetch(sign == 1 ? '+' : '-');
45+
return 0.0;
46+
}
47+
}
48+
for (*pn = 0.0; isdigit(c); c = getch())
49+
*pn = 10.0 * *pn + (c - '0');
50+
51+
if (c == '.') {
52+
c = getch();
53+
}
54+
55+
for (power = 1.0; isdigit(c); c = getch()) {
56+
*pn = 10.0 * *pn + (c - '0');
57+
power *= 10.0;
58+
}
59+
60+
int eSign = 1;
61+
if (c == 'e' || c == 'E') {
62+
c = getch();
63+
if (c == '-') {
64+
eSign = -1;
65+
c = getch();
66+
} else if (c == '+') {
67+
c = getch();
68+
}
69+
}
70+
71+
double ePower = 1.0;
72+
int eCount = 0;
73+
for (eCount = 0; isdigit(c); c = getch())
74+
eCount = 10.0 * eCount + (c - '0');
75+
76+
while (eCount-- > 0)
77+
ePower *= 10;
78+
79+
*pn = *pn / power * sign;
80+
81+
if (eSign > 0)
82+
*pn *= ePower;
83+
else
84+
*pn /= ePower;
85+
86+
if (c != EOF)
87+
ungetch(c);
88+
return c;
89+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
4+
#define BUFSIZE 100
5+
6+
int buf[BUFSIZE];
7+
int bufp = 0;
8+
9+
int value = EOF;
10+
11+
int getch(void)
12+
{
13+
return (bufp > 0) ? buf[--bufp] : getchar();
14+
}
15+
16+
void ungetch(int c)
17+
{
18+
if (bufp >= BUFSIZE)
19+
printf("ungetch: too many characters\n");
20+
else
21+
buf[bufp++] = c;
22+
}
23+
24+
void ungetchs(char s[])
25+
{
26+
int len = strlen(s);
27+
while (len > 0) {
28+
ungetch(s[--len]);
29+
}
30+
}
31+
32+
int getchOne(void)
33+
{
34+
if (value != EOF) {
35+
int temp = value;
36+
value = EOF;
37+
return temp;
38+
}
39+
else
40+
return getchar();
41+
}
42+
43+
void ungetchOne(int c)
44+
{
45+
if (value != EOF)
46+
printf("ungetchOne: cannot ungetch more char\n");
47+
else
48+
value = c;
49+
}

0 commit comments

Comments
 (0)