-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex5_19.c
84 lines (75 loc) · 1.4 KB
/
ex5_19.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
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAXTOKEN 100
enum { NAME, PARENS, BRACKETS };
char token[MAXTOKEN];
char out[1000];
int gettoken(void)
{
int c;
char *p = token;
while ((c = getc(stdin)) == ' ' || c == '\t')
;
if (c == '(') {
if ((c = getc(stdin)) == ')') {
strcpy(token, "()");
return PARENS;
}
else {
ungetc(c, stdin);
return '(';
}
}
else if (c == '[') {
for (*p++ = c; (*p++ = getc(stdin)) != ']'; )
;
*p = '\0';
return BRACKETS;
}
else if (isalpha(c)) {
for (*p++ = c; isalnum(c = getc(stdin)); )
*p++ = c;
ungetc(c, stdin);
*p = '\0';
return NAME;
}
else
return c;
}
int main()
{
int type, prev_type;
char temp[1400];
while ((prev_type = gettoken()) != EOF) {
strcpy(out, token);
while ((type = gettoken()) != '\n') {
if (type == PARENS || type == BRACKETS) {
if (prev_type == '*') {
sprintf(temp, "(%s)", out);
strcat(temp, token);
strcpy(out, temp);
}
else {
strcat(out, token);
}
prev_type = type;
}
else if (type == '*') {
prev_type = type;
sprintf(temp, "*%s", out);
strcpy(out, temp);
}
else if (type == NAME) {
prev_type = type;
sprintf(temp, "%s %s", token, out);
strcpy(out, temp);
}
else {
printf("Invalid input at %s\n", token);
}
}
printf("%s\n", out);
}
return 0;
}