Skip to content

Commit 6b8c8b5

Browse files
committed
5.10
1 parent 45b04d7 commit 6b8c8b5

File tree

11 files changed

+358
-0
lines changed

11 files changed

+358
-0
lines changed
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <stdio.h>
2+
3+
main(int argc, char *argv[])
4+
{
5+
int i;
6+
7+
for (i = 1; i < argc; i++)
8+
printf("%s%s", argv[i], (i < argc-1) ? " " : "");
9+
printf("\n");
10+
11+
return 0;
12+
}
13+
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <stdio.h>
2+
3+
main(int argc, char *argv[])
4+
{
5+
while (--argc > 0)
6+
printf("%s%s", *++argv, (argc > 1) ? " " : "");
7+
//printf((argc > 1) ? "%s " : "%s", *++argv);
8+
printf("\n");
9+
10+
return 0;
11+
}
12+
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#define MAXLINE 1000
4+
5+
int mygetline(char *line, int max);
6+
7+
main(int argc, char *argv[])
8+
{
9+
char line[MAXLINE];
10+
int found = 0;
11+
12+
if (argc != 2)
13+
printf("Usage: find pattern\n");
14+
else
15+
while (mygetline(line, MAXLINE) > 0)
16+
if (strstr(line, argv[1]) != NULL) {
17+
printf("%s", line);
18+
found++;
19+
}
20+
return found;
21+
}
22+
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
4+
#define MAXLINE 1000
5+
6+
int mygetline(char *line, int max);
7+
8+
main(int argc, char *argv[])
9+
{
10+
char line[MAXLINE];
11+
long lineno = 0;
12+
int c, except = 0, number = 0, found = 0;
13+
14+
while (--argc > 0 && (*++argv)[0] == '-')
15+
while (c = *++argv[0])
16+
switch(c) {
17+
case 'x':
18+
except = 1;
19+
break;
20+
case 'n':
21+
number = 1;
22+
break;
23+
default:
24+
printf("find: illegal option %c\n", c);
25+
argc = 0;
26+
found = -1;
27+
break;
28+
}
29+
if (argc != 1)
30+
printf("Usage: find -x -n pattern\n");
31+
else
32+
while (mygetline(line, MAXLINE) > 0) {
33+
lineno++;
34+
if ((strstr(line, *argv) != NULL) != except) {
35+
if (number)
36+
printf("%ld:", lineno);
37+
printf("%s", line);
38+
found++;
39+
}
40+
}
41+
return found;
42+
}
43+
+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
#define MAXOP 100
5+
#define NUMBER '0'
6+
7+
int getop(char [], int, char **);
8+
void push(double);
9+
double pop(void);
10+
11+
main(int argc, char **argv)
12+
{
13+
int type;
14+
double op2;
15+
char s[MAXOP];
16+
17+
if (argc == 1) {
18+
printf("Usage: expr 1 1 +\n");
19+
return 1;
20+
}
21+
22+
while ((type = getop(s, --argc, ++argv)) != EOF) {
23+
switch (type) {
24+
case NUMBER:
25+
push(atof(s));
26+
break;
27+
case '+':
28+
push(pop() + pop());
29+
break;
30+
case '*':
31+
push(pop() * pop());
32+
break;
33+
case '-':
34+
op2 = pop();
35+
push(pop() - op2);
36+
break;
37+
case '/':
38+
op2 = pop();
39+
if (op2 != 0.0)
40+
push(pop() / op2);
41+
else
42+
printf("error: zero divisor\n");
43+
break;
44+
default:
45+
printf("error: unknown command %s\n", s);
46+
break;
47+
}
48+
}
49+
50+
printf("Result: %g\n", pop());
51+
52+
return 0;
53+
}
54+
55+
56+
#define MAXVAL 100
57+
58+
int sp = 0;
59+
double val[MAXVAL];
60+
61+
void push(double f)
62+
{
63+
if (sp < MAXVAL)
64+
val[sp++] = f;
65+
else
66+
printf("error: stack full, can't push %g\n", f);
67+
}
68+
69+
double pop(void)
70+
{
71+
if (sp > 0)
72+
return val[--sp];
73+
else {
74+
printf("error: stack empty\n");
75+
return 0.0;
76+
}
77+
}
78+
79+
int getop(char *s, int argc, char **argv)
80+
{
81+
if (argc == 0)
82+
return EOF;
83+
84+
char *param = *argv;
85+
86+
int c;
87+
88+
while ((*s = c = *param++) == ' ' || c == '\t')
89+
;
90+
91+
*++s = '\0';
92+
93+
if (!isdigit(c) && c != '.')
94+
return c;
95+
96+
if (isdigit(c))
97+
while (isdigit(*s++ = c = *param++))
98+
;
99+
100+
101+
if (c == '.')
102+
while (isdigit(*s++ = c = *param++))
103+
;
104+
105+
*s = '\0';
106+
107+
return NUMBER;
108+
}
109+
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <stdio.h>
2+
3+
main(int argc, char *argv[])
4+
{
5+
int c, n, i;
6+
int cur;
7+
8+
if (argc == 1)
9+
n = 8;
10+
else if (argc == 2)
11+
n = atoi(*++argv);
12+
else {
13+
printf("Usage: detab (8)(tab stop)\n");
14+
return 1;
15+
}
16+
17+
cur = 0;
18+
19+
while ((c = getchar()) != EOF) {
20+
if (c == '\t') {
21+
for (i = n - cur; i > 0; --i)
22+
putchar(' ');
23+
24+
cur = 0;
25+
} else {
26+
putchar(c);
27+
++cur;
28+
if (cur >= n || c == '\n')
29+
cur = 0;
30+
}
31+
}
32+
}
33+
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <stdio.h>
2+
3+
#define MAXLINE 1000
4+
5+
int myGetline(char line[], int maxline);
6+
7+
main(int argc, char *argv[])
8+
{
9+
int c, len;
10+
int i;
11+
char line[MAXLINE];
12+
int tabstop;
13+
int spaceCount;
14+
15+
if (argc == 1)
16+
tabstop = 8;
17+
else if (argc == 2)
18+
tabstop = atoi(*++argv);
19+
else {
20+
printf("Usage entab (8)(tabstop)\n");
21+
return 1;
22+
}
23+
24+
while ((len = myGetline(line, MAXLINE)) > 0) {
25+
spaceCount = 0;
26+
27+
for (i = 0; i < len; ++i) {
28+
if (line[i] == ' ')
29+
spaceCount++;
30+
else
31+
spaceCount = 0;
32+
33+
if (spaceCount == tabstop) {
34+
}
35+
}
36+
printf("%s", line);
37+
}
38+
}
39+
40+
int myGetline(char s[], int lim)
41+
{
42+
int c, i;
43+
44+
for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
45+
s[i] = c;
46+
if (c == '\n') {
47+
s[i] = c;
48+
++i;
49+
}
50+
s[i] = '\0';
51+
return i;
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <stdio.h>
2+
3+
main(int argc, char *argv[])
4+
{
5+
}
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <stdio.h>
2+
3+
main(int argc, char *argv[])
4+
{
5+
}
6+
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
4+
#define MAXLINES 50000
5+
#define MAXLEN 10000
6+
7+
int mygetline(char *line, int max);
8+
9+
char *alloc(int);
10+
void afree(char*);
11+
12+
main(int argc, char *argv[])
13+
{
14+
int n;
15+
16+
if (argc == 1)
17+
n = 10;
18+
else if (argc == 2 && (*++argv)[0] == '-') {
19+
n = atoi(*argv + 1);
20+
if (n < 1)
21+
n = 10;
22+
} else {
23+
printf("Usage: tail -n\n");
24+
return 1;
25+
}
26+
27+
char line[MAXLEN], *lineptr[MAXLINES];
28+
int len;
29+
char *p;
30+
int lines = 0;
31+
32+
while ((len = mygetline(line, MAXLEN)) > 0) {
33+
p = alloc(len + 1);
34+
strcpy(p, line);
35+
lineptr[lines++] = p;
36+
}
37+
38+
39+
int i = lines - n;
40+
i = (i >= 0) ? i : 0;
41+
42+
while (n-- > 0 && i <= lines) {
43+
printf("%s", lineptr[i++]);
44+
}
45+
46+
return 0;
47+
}
48+
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <stdio.h>
2+
3+
4+
int mygetline(char s[], int lim)
5+
{
6+
int c, i;
7+
i = 0;
8+
while (--lim > 0 && (c = getchar()) != EOF && c != '\n')
9+
s[i++] = c;
10+
if (c == '\n')
11+
s[i++] = c;
12+
s[i] = '\0';
13+
return i;
14+
}

0 commit comments

Comments
 (0)