/
5-10.C
84 lines (78 loc) · 1.89 KB
/
5-10.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
/* Copyright (c) 1992 by AT&T Bell Laboratories. */
/* Advanced C++ Programming Styles and Idioms */
/* James O. Coplien */
/* All rights reserved. */
class String {
// . . . .
public:
char operator[](int);
int length();
String operator()(int,int);
String();
String(const char *const);
String operator+(const String&);
};
class Atom {
};
class NumericAtom: public Atom {
public:
NumericAtom(): sum(0) { }
NumericAtom(String &s) {
sum = 0;
for (int i = 0; s[i] >= '0' && s[i] <= '9'; i++) {
sum = (sum*10) + s[i] - '0';
}
s = s(i, s.length()-i);
}
NumericAtom(const NumericAtom &n) { sum = n.value(); }
~NumericAtom() { }
long value() const { return sum; }
Atom *copy() {
NumericAtom *retval = new NumericAtom;
retval->sum = sum;
return retval;
}
private:
long sum;
};
class Name : public Atom {
public:
Name(): n("") { }
Name(String& s) {
for (int i=0; s[i] >= 'a' && s[i] <= 'z'; i++) {
n = n + s(i,1);
}
s = s(i, s.length()-i);
}
Name(const Name& m) { n = m.name(); }
~Name() {}
String name() const { return n; }
Atom *copy() { Name *retval = new Name;
retval->n = n; return retval; }
private:
String n;
};
class Punct : public Atom {
public:
Punct(): c('\e0') { }
Punct(String& s) { c = s[0]; s = s(1,s.length()-1); }
Punct(const Punct& p) { c = char(p); }
operator char() const { return c; }
~Punct() {}
Atom *copy() { Punct *retval = new Punct;
retval->c = c; return retval; }
private:
char c;
};
class Oper : public Atom {
public:
Oper(): c('\e0') { }
Oper(String& s) { c = s[0]; s = s(1,s.length()-1); }
Oper(Oper& o) { c = char(o); }
~Oper() {}
operator char() const { return c; }
Atom *copy() { Oper *retval = new Oper;
retval->c = c; return retval; }
private:
char c;
};